The JavaScript SDK requires an AuthenticationProvider to obtain JWT tokens to send activities to the target channel. Learn more
The package @microsoft/agents-bot-hosting
provides a default authentication provider based on MSAL, that can be configured for the following types of credentials:
Note: MultiTenant requires the Azure Bot instance to be configured as Multi Tenant and the EntraID app registration to be configured as Accounts in any organizational directory (Any Microsoft Entra ID tenant - Multitenant), and only supports Client Secret or Client Certificate. Learn More
The configuration is obtained at runtime from environment variables, using the helper function loadBotAuthConfigFromEnv(): AuthConfiguration
. The CloudAdapter
instance requires to be initialized with the AuthConfiguration
.
Based on the provided variables the authentication type will be infered as described below.
tenantId={tenant-id-guid}
clientId={app-id-guid}
clientSecret={app-registration-secret}
This is the recommended configuration for local development.
tenantId={tenant-id-guid}
clientId={app-id-guid}
certPemFile={path-to-pem-file}
certKeyFile={path-to-key-file}
Note: The key file should not use any password.
tenantId={tenant-id-guid}
clientId={app-id-guid}
This is the recommended configuration for production scenarios. Learn more.
Note: The bot needs to run in any Azure service supporting Managed Identities (see which Azure services support manage identities here ), and the managed identity should match the one configured in EntraID. Learn more.
tenantId={tenant-id-guid}
clientId={app-id-guid}
FICClientId={client-id-of-the-FIC}
clientId={app-id-guid}
clientSecret={app-registration-secret}
clientId={app-id-guid}
certPemFile={path-to-pem-file}
certKeyFile={path-to-key-file}
To load the configuration using the same format as the BotFramework SDK, we provide another helper function loadBotAuthConfigFromEnv(): AuthConfiguration
MicrosoftAppTenantId={tenant-id-guid}
MicrosoftAppId={app-id-guid}
MicrosoftAppPassword={app-registration-secret}
Users requiring a customized authentication provider can implement the interface:
export interface AuthProvider {
getAccessToken: (authConfig: AuthConfiguration, scope: string) => Promise<string>
}
As an example, let’s implement the AuthProvider
using @azure/identity
:
import { EnvironmentCredential } from "@azure/identity"
import { AuthProvider, AuthConfiguration } from "@microsoft/agents-bot-hosting"
class DevTokenProvider implements AuthProvider {
async getAccessToken(authConfig: AuthConfiguration): Promise<string> {
const id = new EnvironmentCredential()
const tokenResponse = await id.getToken("https://api.botframework.com/.default")
return tokenResponse.token
}
To instantiate the CloudAdapter
using the DevTokenProvider
const adapter = new CloudAdapter(authConfig, new DevTokenProvider())