microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Bot.Core/Hosting/BotConfig.cs
108lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.Extensions.Configuration; |
| 5 | |
| 6 | namespace Microsoft.Teams.Bot.Core.Hosting; |
| 7 | |
| 8 | /// <summary> |
| 9 | /// Configuration model for bot authentication credentials. |
| 10 | /// </summary> |
| 11 | /// <remarks> |
| 12 | /// This class consolidates bot authentication settings from various configuration sources including |
| 13 | /// Bot Framework SDK configuration, Core configuration, and Azure AD configuration sections. |
| 14 | /// It supports multiple authentication modes: client secrets, system-assigned managed identities, |
| 15 | /// user-assigned managed identities, and federated identity credentials (FIC). |
| 16 | /// </remarks> |
| 17 | internal sealed class BotConfig |
| 18 | { |
| 19 | /// <summary> |
| 20 | /// Identifier used to specify system-assigned managed identity authentication. |
| 21 | /// When FicClientId equals this value, the system will use the system-assigned managed identity. |
| 22 | /// </summary> |
| 23 | public const string SystemManagedIdentityIdentifier = "system"; |
| 24 | |
| 25 | /// <summary> |
| 26 | /// Gets or sets the Azure AD tenant ID. |
| 27 | /// </summary> |
| 28 | public string TenantId { get; set; } = string.Empty; |
| 29 | |
| 30 | /// <summary> |
| 31 | /// Gets or sets the application (client) ID from Azure AD app registration. |
| 32 | /// </summary> |
| 33 | public string ClientId { get; set; } = string.Empty; |
| 34 | |
| 35 | /// <summary> |
| 36 | /// Gets or sets the client secret for client credentials authentication. |
| 37 | /// Optional if using managed identity or federated identity credentials. |
| 38 | /// </summary> |
| 39 | public string? ClientSecret { get; set; } |
| 40 | |
| 41 | /// <summary> |
| 42 | /// Gets or sets the client ID for federated identity credentials or user-assigned managed identity. |
| 43 | /// Use <see cref="SystemManagedIdentityIdentifier"/> to specify system-assigned managed identity. |
| 44 | /// </summary> |
| 45 | public string? FicClientId { get; set; } |
| 46 | |
| 47 | /// <summary> |
| 48 | /// Creates a BotConfig from Bot Framework SDK configuration format. |
| 49 | /// </summary> |
| 50 | /// <param name="configuration">Configuration containing MicrosoftAppId, MicrosoftAppPassword, and MicrosoftAppTenantId settings.</param> |
| 51 | /// <returns>A new BotConfig instance with settings from Bot Framework configuration.</returns> |
| 52 | /// <exception cref="ArgumentNullException">Thrown when <paramref name="configuration"/> is null.</exception> |
| 53 | public static BotConfig FromBFConfig(IConfiguration configuration) |
| 54 | { |
| 55 | ArgumentNullException.ThrowIfNull(configuration); |
| 56 | return new() |
| 57 | { |
| 58 | TenantId = configuration["MicrosoftAppTenantId"] ?? string.Empty, |
| 59 | ClientId = configuration["MicrosoftAppId"] ?? string.Empty, |
| 60 | ClientSecret = configuration["MicrosoftAppPassword"], |
| 61 | }; |
| 62 | } |
| 63 | |
| 64 | /// <summary> |
| 65 | /// Creates a BotConfig from Teams Bot Core environment variable format. |
| 66 | /// </summary> |
| 67 | /// <param name="configuration">Configuration containing TENANT_ID, CLIENT_ID, CLIENT_SECRET, and MANAGED_IDENTITY_CLIENT_ID settings.</param> |
| 68 | /// <returns>A new BotConfig instance with settings from Core configuration.</returns> |
| 69 | /// <exception cref="ArgumentNullException">Thrown when <paramref name="configuration"/> is null.</exception> |
| 70 | /// <remarks> |
| 71 | /// This format is typically used with environment variables in containerized deployments. |
| 72 | /// The MANAGED_IDENTITY_CLIENT_ID can be set to "system" for system-assigned managed identity. |
| 73 | /// </remarks> |
| 74 | public static BotConfig FromCoreConfig(IConfiguration configuration) |
| 75 | { |
| 76 | ArgumentNullException.ThrowIfNull(configuration); |
| 77 | return new() |
| 78 | { |
| 79 | TenantId = configuration["TENANT_ID"] ?? string.Empty, |
| 80 | ClientId = configuration["CLIENT_ID"] ?? string.Empty, |
| 81 | ClientSecret = configuration["CLIENT_SECRET"], |
| 82 | FicClientId = configuration["MANAGED_IDENTITY_CLIENT_ID"], |
| 83 | }; |
| 84 | } |
| 85 | |
| 86 | /// <summary> |
| 87 | /// Creates a BotConfig from Azure AD configuration section format. |
| 88 | /// </summary> |
| 89 | /// <param name="configuration">Configuration containing an Azure AD configuration section.</param> |
| 90 | /// <param name="sectionName">The name of the configuration section containing Azure AD settings. Defaults to "AzureAd".</param> |
| 91 | /// <returns>A new BotConfig instance with settings from the Azure AD configuration section.</returns> |
| 92 | /// <exception cref="ArgumentNullException">Thrown when <paramref name="configuration"/> is null.</exception> |
| 93 | /// <remarks> |
| 94 | /// This format is compatible with Microsoft.Identity.Web configuration sections in appsettings.json. |
| 95 | /// The section should contain TenantId, ClientId, and optionally ClientSecret properties. |
| 96 | /// </remarks> |
| 97 | public static BotConfig FromAadConfig(IConfiguration configuration, string sectionName = "AzureAd") |
| 98 | { |
| 99 | ArgumentNullException.ThrowIfNull(configuration); |
| 100 | IConfigurationSection section = configuration.GetSection(sectionName); |
| 101 | return new() |
| 102 | { |
| 103 | TenantId = section["TenantId"] ?? string.Empty, |
| 104 | ClientId = section["ClientId"] ?? string.Empty, |
| 105 | ClientSecret = section["ClientSecret"], |
| 106 | }; |
| 107 | } |
| 108 | } |
| 109 | |