|
| 1 | +# Authentication Bot Utilizing MS Graph |
| 2 | + |
| 3 | +Bot Framework v4 bot authentication using Microsoft Graph sample |
| 4 | + |
| 5 | +This bot has been created using [Bot Framework](https://dev.botframework.com), is shows how to use the bot authentication capabilities of Azure Bot Service. In this sample we are assuming the OAuth 2 provider is Azure Active Directory v2 (AADv2) and are utilizing the Microsoft Graph API to retrieve data about the user. [Check here](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-authentication?view=azure-bot-service-4.0&tabs=csharp) for information about getting an AADv2 |
| 6 | +application setup for use in Azure Bot Service. The [scopes](https://developer.microsoft.com/en-us/graph/docs/concepts/permissions_reference) used in this sample are the following: |
| 7 | + |
| 8 | +- `openid` |
| 9 | +- `profile` |
| 10 | +- `User.Read` |
| 11 | + |
| 12 | +NOTE: Microsoft Teams currently differs slightly in the way auth is integrated with the bot. Refer to sample 46.teams-auth. |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +- Java 1.8+ |
| 17 | +- Install [Maven](https://maven.apache.org/) |
| 18 | +- An account on [Azure](https://azure.microsoft.com) if you want to deploy to Azure. |
| 19 | + |
| 20 | +## To try this sample locally |
| 21 | +- From the root of this project folder: |
| 22 | + - Build the sample using `mvn package` |
| 23 | + - Run it by using `java -jar .\target\bot-authentication-msgraph-sample.jar` |
| 24 | + |
| 25 | +- Test the bot using Bot Framework Emulator |
| 26 | + |
| 27 | + [Bot Framework Emulator](https://github.com/microsoft/botframework-emulator) is a desktop application that allows bot developers to test and debug their bots on localhost or running remotely through a tunnel. |
| 28 | + |
| 29 | + - Install the Bot Framework Emulator version 4.3.0 or greater from [here](https://github.com/Microsoft/BotFramework-Emulator/releases) |
| 30 | + |
| 31 | + - Connect to the bot using Bot Framework Emulator |
| 32 | + |
| 33 | + - Launch Bot Framework Emulator |
| 34 | + - File -> Open Bot |
| 35 | + - Enter a Bot URL of `http://localhost:3978/api/messages` |
| 36 | + |
| 37 | +## Deploy the bot to Azure |
| 38 | + |
| 39 | +As described on [Deploy your bot](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-deploy-az-cli), you will perform the first 4 steps to setup the Azure app, then deploy the code using the azure-webapp Maven plugin. |
| 40 | + |
| 41 | +### 1. Login to Azure |
| 42 | +From a command (or PowerShell) prompt in the root of the bot folder, execute: |
| 43 | +`az login` |
| 44 | + |
| 45 | +### 2. Set the subscription |
| 46 | +`az account set --subscription "<azure-subscription>"` |
| 47 | + |
| 48 | +If you aren't sure which subscription to use for deploying the bot, you can view the list of subscriptions for your account by using `az account list` command. |
| 49 | + |
| 50 | +### 3. Create an App registration |
| 51 | +`az ad app create --display-name "<botname>" --password "<appsecret>" --available-to-other-tenants` |
| 52 | + |
| 53 | +Replace `<botname>` and `<appsecret>` with your own values. |
| 54 | + |
| 55 | +`<botname>` is the unique name of your bot. |
| 56 | +`<appsecret>` is a minimum 16 character password for your bot. |
| 57 | + |
| 58 | +Record the `appid` from the returned JSON |
| 59 | + |
| 60 | +### 4. Create the Azure resources |
| 61 | +Replace the values for `<appid>`, `<appsecret>`, `<botname>`, and `<groupname>` in the following commands: |
| 62 | + |
| 63 | +#### To a new Resource Group |
| 64 | +`az deployment sub create --name "authenticationBotDeploy" --location "westus" --template-file ".\deploymentTemplates\template-with-new-rg.json" --parameters appId="<appid>" appSecret="<appsecret>" botId="<botname>" botSku=S1 newAppServicePlanName="authenticationGraphBotPlan" newWebAppName="authenticationGraphBot" groupLocation="westus" newAppServicePlanLocation="westus"` |
| 65 | + |
| 66 | +#### To an existing Resource Group |
| 67 | +`az deployment group create --resource-group "<groupname>" --template-file ".\deploymentTemplates\template-with-preexisting-rg.json" --parameters appId="<appid>" appSecret="<appsecret>" botId="<botname>" newWebAppName="authenticationGraphBot" newAppServicePlanName="authenticationGraphBotPlan" appServicePlanLocation="westus" --name "authenticationGraphBot"` |
| 68 | + |
| 69 | +### 5. Update app id and password |
| 70 | +In src/main/resources/application.properties update |
| 71 | + - `MicrosoftAppPassword` with the botsecret value |
| 72 | + - `MicrosoftAppId` with the appid from the first step |
| 73 | + |
| 74 | +### 6. Deploy the code |
| 75 | +- Execute `mvn clean package` |
| 76 | +- Execute `mvn azure-webapp:deploy -Dgroupname="<groupname>" -Dbotname="<botname>"` |
| 77 | + |
| 78 | +If the deployment is successful, you will be able to test it via "Test in Web Chat" from the Azure Portal using the "Bot Channel Registration" for the bot. |
| 79 | + |
| 80 | +After the bot is deployed, you only need to execute #6 if you make changes to the bot. |
| 81 | + |
| 82 | +## Interacting with the bot |
| 83 | + |
| 84 | +This sample uses the bot authentication capabilities of Azure Bot Service, providing features to make it easier to develop a bot that |
| 85 | +authenticates users to various identity providers such as Azure AD (Azure Active Directory), GitHub, Uber, and so on. These updates also |
| 86 | +take steps towards an improved user experience by eliminating the magic code verification for some clients and channels. |
| 87 | +It is important to note that the user's token does not need to be stored in the bot. When the bot needs to use or verify the user has a valid token at any point the OAuth prompt may be sent. If the token is not valid they will be prompted to login. |
| 88 | + |
| 89 | +## Microsoft Graph API |
| 90 | + |
| 91 | +This sample demonstrates using Azure Active Directory v2 as the OAuth2 provider and utilizes the Microsoft Graph API. |
| 92 | +Microsoft Graph is a Microsoft developer platform that connects multiple services and devices. Initially released in 2015, |
| 93 | +the Microsoft Graph builds on Office 365 APIs and allows developers to integrate their services with Microsoft products including Windows, Office 365, and Azure. |
| 94 | + |
| 95 | +## GraphError 404: ResourceNotFound, Resource could not be discovered |
| 96 | + |
| 97 | +This error may confusingly present itself if either of the following are true: |
| 98 | + |
| 99 | +- You're using an email ending in `@microsoft.com`, and/or |
| 100 | +- Your OAuth AAD tenant is `microsoft.onmicrosoft.com`. |
| 101 | + |
| 102 | +## Further reading |
| 103 | + |
| 104 | +- [Bot Framework Documentation](https://docs.botframework.com) |
| 105 | +- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0) |
| 106 | +- [Dialogs](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-dialog?view=azure-bot-service-4.0) |
| 107 | +- [Gathering Input Using Prompts](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-prompts?view=azure-bot-service-4.0&tabs=csharp) |
| 108 | +- [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0) |
| 109 | +- [Microsoft Graph API](https://developer.microsoft.com/en-us/graph) |
| 110 | +- [MS Graph Docs](https://developer.microsoft.com/en-us/graph/docs/concepts/overview) and [SDK](https://github.com/microsoftgraph/msgraph-sdk-dotnet) |
| 111 | +- [Azure Bot Service Introduction](https://docs.microsoft.com/azure/bot-service/bot-service-overview-introduction?view=azure-bot-service-4.0) |
| 112 | +- [Azure Bot Service Documentation](https://docs.microsoft.com/azure/bot-service/?view=azure-bot-service-4.0) |
| 113 | +- [Azure CLI](https://docs.microsoft.com/cli/azure/?view=azure-cli-latest) |
| 114 | +- [Azure Portal](https://portal.azure.com) |
| 115 | +- [Channels and Bot Connector Service](https://docs.microsoft.com/en-us/azure/bot-service/bot-concepts?view=azure-bot-service-4.0) |
| 116 | +- [Maven Plugin for Azure App Service](https://docs.microsoft.com/en-us/java/api/overview/azure/maven/azure-webapp-maven-plugin/readme?view=azure-java-stable) |
| 117 | +- [Spring Boot](https://spring.io/projects/spring-boot) |
| 118 | +- [Azure for Java cloud developers](https://docs.microsoft.com/en-us/azure/java/?view=azure-java-stable) |
0 commit comments