microsoft/teams.net

Public

mirrored fromhttps://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
docs/update-release-process

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

Libraries/Microsoft.Teams.Api/Auth/CloudEnvironment.cs

171lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4namespace Microsoft.Teams.Api.Auth;
5
6/// <summary>
7/// Bundles all cloud-specific service endpoints for a given Azure environment.
8/// Use predefined instances (<see cref="Public"/>, <see cref="USGov"/>, <see cref="USGovDoD"/>, <see cref="China"/>)
9/// or construct a custom one.
10/// </summary>
11public class CloudEnvironment
12{
13 /// <summary>
14 /// The Azure AD login endpoint (e.g. "https://login.microsoftonline.com").
15 /// </summary>
16 public string LoginEndpoint { get; }
17
18 /// <summary>
19 /// The default multi-tenant login tenant (e.g. "botframework.com").
20 /// </summary>
21 public string LoginTenant { get; }
22
23 /// <summary>
24 /// The Bot Framework OAuth scope (e.g. "https://api.botframework.com/.default").
25 /// </summary>
26 public string BotScope { get; }
27
28 /// <summary>
29 /// The Bot Framework token service base URL (e.g. "https://token.botframework.com").
30 /// </summary>
31 public string TokenServiceUrl { get; }
32
33 /// <summary>
34 /// The OpenID metadata URL for token validation (e.g. "https://login.botframework.com/v1/.well-known/openidconfiguration").
35 /// </summary>
36 public string OpenIdMetadataUrl { get; }
37
38 /// <summary>
39 /// The token issuer for Bot Framework tokens (e.g. "https://api.botframework.com").
40 /// </summary>
41 public string TokenIssuer { get; }
42
43 /// <summary>
44 /// The Microsoft Graph token scope (e.g. "https://graph.microsoft.com/.default").
45 /// </summary>
46 public string GraphScope { get; }
47
48 public CloudEnvironment(
49 string loginEndpoint,
50 string loginTenant,
51 string botScope,
52 string tokenServiceUrl,
53 string openIdMetadataUrl,
54 string tokenIssuer,
55 string graphScope)
56 {
57 LoginEndpoint = loginEndpoint.TrimEnd('/');
58 LoginTenant = loginTenant;
59 BotScope = botScope;
60 TokenServiceUrl = tokenServiceUrl.TrimEnd('/');
61 OpenIdMetadataUrl = openIdMetadataUrl;
62 TokenIssuer = tokenIssuer;
63 GraphScope = graphScope;
64 }
65
66 /// <summary>
67 /// Microsoft public (commercial) cloud.
68 /// </summary>
69 public static readonly CloudEnvironment Public = new(
70 loginEndpoint: "https://login.microsoftonline.com",
71 loginTenant: "botframework.com",
72 botScope: "https://api.botframework.com/.default",
73 tokenServiceUrl: "https://token.botframework.com",
74 openIdMetadataUrl: "https://login.botframework.com/v1/.well-known/openidconfiguration",
75 tokenIssuer: "https://api.botframework.com",
76 graphScope: "https://graph.microsoft.com/.default"
77 );
78
79 /// <summary>
80 /// US Government Community Cloud High (GCCH).
81 /// </summary>
82 public static readonly CloudEnvironment USGov = new(
83 loginEndpoint: "https://login.microsoftonline.us",
84 loginTenant: "MicrosoftServices.onmicrosoft.us",
85 botScope: "https://api.botframework.us/.default",
86 tokenServiceUrl: "https://tokengcch.botframework.azure.us",
87 openIdMetadataUrl: "https://login.botframework.azure.us/v1/.well-known/openidconfiguration",
88 tokenIssuer: "https://api.botframework.us",
89 graphScope: "https://graph.microsoft.us/.default"
90 );
91
92 /// <summary>
93 /// US Government Department of Defense (DoD).
94 /// </summary>
95 public static readonly CloudEnvironment USGovDoD = new(
96 loginEndpoint: "https://login.microsoftonline.us",
97 loginTenant: "MicrosoftServices.onmicrosoft.us",
98 botScope: "https://api.botframework.us/.default",
99 tokenServiceUrl: "https://apiDoD.botframework.azure.us",
100 openIdMetadataUrl: "https://login.botframework.azure.us/v1/.well-known/openidconfiguration",
101 tokenIssuer: "https://api.botframework.us",
102 graphScope: "https://dod-graph.microsoft.us/.default"
103 );
104
105 /// <summary>
106 /// China cloud (21Vianet).
107 /// </summary>
108 public static readonly CloudEnvironment China = new(
109 loginEndpoint: "https://login.partner.microsoftonline.cn",
110 loginTenant: "microsoftservices.partner.onmschina.cn",
111 botScope: "https://api.botframework.azure.cn/.default",
112 tokenServiceUrl: "https://token.botframework.azure.cn",
113 openIdMetadataUrl: "https://login.botframework.azure.cn/v1/.well-known/openidconfiguration",
114 tokenIssuer: "https://api.botframework.azure.cn",
115 graphScope: "https://microsoftgraph.chinacloudapi.cn/.default"
116 );
117
118 /// <summary>
119 /// Creates a new <see cref="CloudEnvironment"/> by applying non-null overrides on top of this instance.
120 /// Returns the same instance if all overrides are null (no allocation).
121 /// </summary>
122 public CloudEnvironment WithOverrides(
123 string? loginEndpoint = null,
124 string? loginTenant = null,
125 string? botScope = null,
126 string? tokenServiceUrl = null,
127 string? openIdMetadataUrl = null,
128 string? tokenIssuer = null,
129 string? graphScope = null)
130 {
131 if (loginEndpoint is null && loginTenant is null && botScope is null &&
132 tokenServiceUrl is null && openIdMetadataUrl is null && tokenIssuer is null &&
133 graphScope is null)
134 {
135 return this;
136 }
137
138 return new CloudEnvironment(
139 loginEndpoint ?? LoginEndpoint,
140 loginTenant ?? LoginTenant,
141 botScope ?? BotScope,
142 tokenServiceUrl ?? TokenServiceUrl,
143 openIdMetadataUrl ?? OpenIdMetadataUrl,
144 tokenIssuer ?? TokenIssuer,
145 graphScope ?? GraphScope
146 );
147 }
148
149 /// <summary>
150 /// Resolves a cloud environment name (case-insensitive) to its corresponding instance.
151 /// Valid names: "Public", "USGov", "USGovDoD", "China".
152 /// </summary>
153 public static CloudEnvironment FromName(string name)
154 {
155 ArgumentNullException.ThrowIfNull(name);
156
157 if (string.IsNullOrWhiteSpace(name))
158 {
159 throw new ArgumentException("Cloud environment name cannot be empty or whitespace.", nameof(name));
160 }
161
162 return name.ToLowerInvariant() switch
163 {
164 "public" => Public,
165 "usgov" => USGov,
166 "usgovdod" => USGovDoD,
167 "china" => China,
168 _ => throw new ArgumentException($"Unknown cloud environment: '{name}'. Valid values are: Public, USGov, USGovDoD, China.", nameof(name))
169 };
170 }
171}
172