microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
teams.net/Libraries/Microsoft.Teams.Extensions/Microsoft.Teams.Extensions.Configuration/Microsoft.Teams.Apps.Extensions
Libraries/Microsoft.Teams.Extensions/Microsoft.Teams.Extensions.Configuration/Microsoft.Teams.Apps.Extensions/TeamsSettings.cs
85lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.Teams.Api.Auth; |
| 5 | |
| 6 | namespace Microsoft.Teams.Apps.Extensions; |
| 7 | |
| 8 | public class TeamsSettings |
| 9 | { |
| 10 | public string? ClientId { get; set; } |
| 11 | public string? ClientSecret { get; set; } |
| 12 | public string? TenantId { get; set; } |
| 13 | public string? Cloud { get; set; } |
| 14 | |
| 15 | /// <summary>Override the Azure AD login endpoint.</summary> |
| 16 | public string? LoginEndpoint { get; set; } |
| 17 | |
| 18 | /// <summary>Override the default login tenant.</summary> |
| 19 | public string? LoginTenant { get; set; } |
| 20 | |
| 21 | /// <summary>Override the Bot Framework OAuth scope.</summary> |
| 22 | public string? BotScope { get; set; } |
| 23 | |
| 24 | /// <summary>Override the Bot Framework token service URL.</summary> |
| 25 | public string? TokenServiceUrl { get; set; } |
| 26 | |
| 27 | /// <summary>Override the OpenID metadata URL for token validation.</summary> |
| 28 | public string? OpenIdMetadataUrl { get; set; } |
| 29 | |
| 30 | /// <summary>Override the token issuer for Bot Framework tokens.</summary> |
| 31 | public string? TokenIssuer { get; set; } |
| 32 | |
| 33 | /// <summary>Override the Microsoft Graph token scope.</summary> |
| 34 | public string? GraphScope { get; set; } |
| 35 | |
| 36 | public bool Empty |
| 37 | { |
| 38 | get { return ClientId == "" || ClientSecret == ""; } |
| 39 | } |
| 40 | |
| 41 | /// <summary> |
| 42 | /// Resolves the <see cref="CloudEnvironment"/> by starting from <paramref name="programmaticCloud"/> |
| 43 | /// (or the <see cref="Cloud"/> setting, or <see cref="CloudEnvironment.Public"/>), then applying |
| 44 | /// any per-endpoint overrides from settings. |
| 45 | /// </summary> |
| 46 | public CloudEnvironment ResolveCloud(CloudEnvironment? programmaticCloud = null) |
| 47 | { |
| 48 | var baseCloud = programmaticCloud |
| 49 | ?? (!string.IsNullOrWhiteSpace(Cloud) ? CloudEnvironment.FromName(Cloud) : null) |
| 50 | ?? CloudEnvironment.Public; |
| 51 | |
| 52 | return baseCloud.WithOverrides( |
| 53 | loginEndpoint: LoginEndpoint, |
| 54 | loginTenant: LoginTenant, |
| 55 | botScope: BotScope, |
| 56 | tokenServiceUrl: TokenServiceUrl, |
| 57 | openIdMetadataUrl: OpenIdMetadataUrl, |
| 58 | tokenIssuer: TokenIssuer, |
| 59 | graphScope: GraphScope |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | public AppOptions Apply(AppOptions? options = null) |
| 64 | { |
| 65 | options ??= new AppOptions(); |
| 66 | |
| 67 | var cloud = ResolveCloud(options.Cloud); |
| 68 | options.Cloud = cloud; |
| 69 | |
| 70 | if (ClientId is not null && ClientSecret is not null && !Empty) |
| 71 | { |
| 72 | var credentials = new ClientCredentials(ClientId, ClientSecret, TenantId) |
| 73 | { |
| 74 | Cloud = cloud |
| 75 | }; |
| 76 | options.Credentials = credentials; |
| 77 | } |
| 78 | else if (options.Credentials is ClientCredentials existingCredentials) |
| 79 | { |
| 80 | existingCredentials.Cloud = cloud; |
| 81 | } |
| 82 | |
| 83 | return options; |
| 84 | } |
| 85 | } |
| 86 | |