|
| 1 | +# OBO Auth Sample |
| 2 | + |
| 3 | +This Agent has been created using [Microsoft 365 Agents SDK](https://github.com/microsoft/agents-for-net), it shows how to use authorization in your Agent using OAuth and OBO. |
| 4 | + |
| 5 | +- The sample uses the Agent SDK User Authorization capabilities in [Azure Bot Service](https://docs.botframework.com), providing features to make it easier to develop an Agent that authorizes users with various identity providers such as Azure AD (Azure Active Directory), GitHub, Uber, etc. |
| 6 | +- This sample shows how to use an OBO Exchange to communicate with Microsoft Copilot Studio using the [CopilotClient class](https://learn.microsoft.com/python/api/microsoft-agents-copilotstudio-client/microsoft.agents.copilotstudio.client.copilotclient). |
| 7 | + |
| 8 | +## Prerequisites |
| 9 | + |
| 10 | +- [Python](https://www.python.org/) version 3.9 or higher |
| 11 | +- [dev tunnel](https://learn.microsoft.com/azure/developer/dev-tunnels/get-started?tabs=windows) (for local development) |
| 12 | +- Access to CopilotStudio to [create an Agent](https://learn.microsoft.com/microsoft-copilot-studio/fundamentals-get-started?tabs=web) |
| 13 | + |
| 14 | +## Local Setup |
| 15 | + |
| 16 | +### Configuration |
| 17 | + |
| 18 | +1. Create an Agent in Copilot Studio. |
| 19 | + 1. Publish your newly created Agent |
| 20 | + 1. Got to Settings => Advanced => Metadata and copy the following values. You will need them later: |
| 21 | + 1. Schema name |
| 22 | + 1. Environment ID |
| 23 | + |
| 24 | +1. [Create an Azure Bot](https://aka.ms/AgentsSDK-CreateBot) |
| 25 | + - Record the Application ID, the Tenant ID, and the Client Secret for use below |
| 26 | + |
| 27 | +1. Open the `env.TEMPLATE` file in the root of the sample project, rename it to `.env` and configure the following values: |
| 28 | + 1. Set the **CONNECTIONS__SERVICE_CONNECTION__SETTINGS__CLIENTID** to the AppId of the bot identity. |
| 29 | + 2. Set the **CONNECTIONS__SERVICE_CONNECTION__SETTINGS__CLIENTSECRET** to the Secret that was created for your identity. *This is the `Secret Value` shown in the AppRegistration*. |
| 30 | + 3. Set the **CONNECTIONS__SERVICE_CONNECTION__SETTINGS__TENANTID** to the Tenant Id where your application is registered. |
| 31 | + |
| 32 | +1. Setting up OAuth for an exchangeable token |
| 33 | + 1. Create a new App Registration |
| 34 | + 1. SingleTenant |
| 35 | + 1. Give it a name and click **Register** |
| 36 | + 1. **Authentication** tab |
| 37 | + 1. **Add Platform**, then **Web**, Set `Redirect URI` to `Web` and `https://token.botframework.com/.auth/web/redirect` |
| 38 | + 1. **Add Platform**, then **Mobile and desktop applications**, and add an additional `http://localhost` Uri. |
| 39 | + 1. **API Permissions** tab |
| 40 | + 1. **Dynamics CRM** with **user_impersonation** |
| 41 | + 1. **Graph** with **User.Read** |
| 42 | + 1. **Power Platform API** with **CopilotStudio.Copilots.Invoke** |
| 43 | + 1. Grant Admin Consent for your tenant. |
| 44 | + 1. **Expose an API** tab |
| 45 | + 1. Click **Add a Scope** |
| 46 | + 1. **Application ID URI** should be: `api://botid-{{appid}}` |
| 47 | + 1. **Scope Name** is "defaultScope" |
| 48 | + 1. **Who can consent** is **Admins and users** |
| 49 | + 1. Enter values for the required Consent fields |
| 50 | + 1. **Certificates & secrets** |
| 51 | + 1. Create a new secret and record the value. This will be used later. |
| 52 | + |
| 53 | +1. Create Azure Bot **OAuth Connection** |
| 54 | + 1. On the Azure Bot created in Step #2, Click **Configuration** tab then the **Add OAuth Connection Settings** button. |
| 55 | + 1. Enter a **Name**. This will be used later. |
| 56 | + 1. For **Service Provider** select **Azure Active Directory v2** |
| 57 | + 1. **Client id** and **Client Secret** are the values created in step #4. |
| 58 | + 1. Enter the **Tenant ID** |
| 59 | + 1. **Scopes** is `api://botid-{{appid}}/defaultScope`. appid is the **Client id** value from #4. |
| 60 | + |
| 61 | +1. Configure the authorization handlers |
| 62 | + 1. Open the `.env` file and update: |
| 63 | + 1. Set the **CONNECTIONS__MCS__SETTINGS__CLIENTID** to the **Client id** from #4 |
| 64 | + 1. Set the **CONNECTIONS__MCS__SETTINGS__CLIENTSECRET** to the **Client Secret** value from #4. |
| 65 | + 1. Set the **CONNECTIONS__MCS__SETTINGS__TENANTID** to the Tenant Id. |
| 66 | + 1. Keep **AGENTAPPLICATION__USERAUTHORIZATION__HANDLERS__MCS__SETTINGS__OBOCONNECTIONNAME=MCS** |
| 67 | + 1. Set **AGENTAPPLICATION__USERAUTHORIZATION__HANDLERS__MCS__SETTINGS__AZUREBOTOAUTHCONNECTIONNAME={{Name}}** where **Name** is the name of your OAuth connection created in step 5. |
| 68 | + |
| 69 | +1. Configure the sample to use your CopilotStudio Agent by modifying the following variables in the `.env` file based on the values obtained in step #1: |
| 70 | + |
| 71 | +```bash |
| 72 | + COPILOTSTUDIOAGENT__ENVIRONMENTID="" # Environment ID of the Agent from MCS |
| 73 | + COPILOTSTUDIOAGENT__SCHEMANAME="" # Schema Name of the Agent from MCS |
| 74 | +``` |
| 75 | + |
| 76 | +1. Run `dev tunnels`. See [Create and host a dev tunnel](https://learn.microsoft.com/azure/developer/dev-tunnels/get-started?tabs=windows) and host the tunnel with anonymous user access command as shown below: |
| 77 | + |
| 78 | + ```bash |
| 79 | + devtunnel host -p 3978 --allow-anonymous |
| 80 | + ``` |
| 81 | + |
| 82 | +1. Take note of the url shown after `Connect via browser:` |
| 83 | + |
| 84 | +1. On the Azure Bot, select **Settings**, then **Configuration**, and update the **Messaging endpoint** to `{tunnel-url}/api/messages` |
| 85 | + |
| 86 | +### Running the Agent |
| 87 | + |
| 88 | +1. Open this folder from your IDE or Terminal of preference |
| 89 | +1. (Optional but recommended) Set up virtual environment and activate it. |
| 90 | +1. Install dependencies |
| 91 | + |
| 92 | +```sh |
| 93 | +pip install -r requirements.txt |
| 94 | +``` |
| 95 | + |
| 96 | +### Run in localhost, anonymous mode |
| 97 | + |
| 98 | +1. Start the application |
| 99 | + |
| 100 | +```sh |
| 101 | +python -m src.main |
| 102 | +``` |
| 103 | + |
| 104 | +At this point you should see the message |
| 105 | + |
| 106 | +```text |
| 107 | +======== Running on http://localhost:3978 ======== |
| 108 | +``` |
| 109 | + |
| 110 | +The agent is ready to accept messages. |
| 111 | + |
| 112 | +## Accessing the Agent |
| 113 | + |
| 114 | +### Using the Agent in WebChat |
| 115 | + |
| 116 | +1. Go to your Azure Bot Service resource in the Azure Portal and select **Test in WebChat** |
| 117 | + |
| 118 | +## Further reading |
| 119 | + |
| 120 | +To learn more about building Bots and Agents, see our [Microsoft 365 Agents SDK](https://github.com/microsoft/agents) repo. |
| 121 | + |
| 122 | +For more information on logging configuration, see the logging section in the Quickstart Agent sample README. |
0 commit comments