-
Notifications
You must be signed in to change notification settings - Fork 331
/
AuthorityInfo.cs
257 lines (220 loc) · 10 KB
/
AuthorityInfo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Globalization;
namespace Microsoft.Identity.Client
{
internal class AuthorityInfo
{
public AuthorityInfo(
AuthorityType authorityType,
string authority,
bool validateAuthority)
{
AuthorityType = authorityType;
ValidateAuthority = validateAuthority;
Host = new UriBuilder(authority).Host;
// TODO: can we simplify this and/or move validation/configuration logic to AbstractApplicationBuilder
// so that all authority mangling/management is in one place?
UserRealmUriPrefix = string.Format(CultureInfo.InvariantCulture, "https://{0}/common/userrealm/", Host);
if (AuthorityType == AuthorityType.B2C)
{
var authorityUri = new Uri(authority);
string[] pathSegments = authorityUri.AbsolutePath.Substring(1).Split(
new[]
{
'/'
},
StringSplitOptions.RemoveEmptyEntries);
if (pathSegments.Length < 3)
{
throw new ArgumentException(MsalErrorMessage.B2cAuthorityUriInvalidPath);
}
CanonicalAuthority = string.Format(
CultureInfo.InvariantCulture,
"https://{0}/{1}/{2}/{3}/",
authorityUri.Authority,
pathSegments[0],
pathSegments[1],
pathSegments[2]);
}
else
{
var authorityUri = new UriBuilder(authority);
CanonicalAuthority = string.Format(
CultureInfo.InvariantCulture,
"https://{0}/{1}/",
authorityUri.Uri.Authority,
GetFirstPathSegment(authority));
}
}
public string Host { get; }
public string CanonicalAuthority { get; set; }
public AuthorityType AuthorityType { get; }
public string UserRealmUriPrefix { get; }
public bool ValidateAuthority { get; }
internal static AuthorityInfo FromAuthorityUri(string authorityUri, bool validateAuthority)
{
string canonicalUri = CanonicalizeAuthorityUri(authorityUri);
ValidateAuthorityUri(canonicalUri);
var authorityType = Instance.Authority.GetAuthorityType(canonicalUri);
// If the authority type is B2C, validateAuthority must be false.
if (authorityType == AuthorityType.B2C)
{
validateAuthority = false;
}
return new AuthorityInfo(authorityType, canonicalUri, validateAuthority);
}
internal static AuthorityInfo FromAadAuthority(Uri cloudInstanceUri, Guid tenantId, bool validateAuthority)
{
#pragma warning disable CA1305 // Specify IFormatProvider
return FromAuthorityUri(string.Format(CultureInfo.InvariantCulture, "{0}/{1}/", cloudInstanceUri, tenantId.ToString("D")), validateAuthority);
#pragma warning restore CA1305 // Specify IFormatProvider
}
internal static AuthorityInfo FromAadAuthority(Uri cloudInstanceUri, string tenant, bool validateAuthority)
{
if (Guid.TryParse(tenant, out Guid tenantId))
{
return FromAadAuthority(cloudInstanceUri, tenantId, validateAuthority);
}
return FromAuthorityUri(string.Format(CultureInfo.InvariantCulture, "{0}/{1}/", cloudInstanceUri, tenant), validateAuthority);
}
internal static AuthorityInfo FromAadAuthority(
AzureCloudInstance azureCloudInstance,
Guid tenantId,
bool validateAuthority)
{
#pragma warning disable CA1305 // Specify IFormatProvider
string authorityUri = GetAuthorityUri(azureCloudInstance, AadAuthorityAudience.AzureAdMyOrg, tenantId.ToString("D"));
#pragma warning restore CA1305 // Specify IFormatProvider
return new AuthorityInfo(AuthorityType.Aad, authorityUri, validateAuthority);
}
internal static AuthorityInfo FromAadAuthority(
AzureCloudInstance azureCloudInstance,
string tenant,
bool validateAuthority)
{
if (Guid.TryParse(tenant, out Guid tenantIdGuid))
{
return FromAadAuthority(azureCloudInstance, tenantIdGuid, validateAuthority);
}
string authorityUri = GetAuthorityUri(azureCloudInstance, AadAuthorityAudience.AzureAdMyOrg, tenant);
return new AuthorityInfo(AuthorityType.Aad, authorityUri, validateAuthority);
}
internal static AuthorityInfo FromAadAuthority(
AzureCloudInstance azureCloudInstance,
AadAuthorityAudience authorityAudience,
bool validateAuthority)
{
string authorityUri = GetAuthorityUri(azureCloudInstance, authorityAudience);
return new AuthorityInfo(AuthorityType.Aad, authorityUri, validateAuthority);
}
internal static AuthorityInfo FromAadAuthority(AadAuthorityAudience authorityAudience, bool validateAuthority)
{
string authorityUri = GetAuthorityUri(AzureCloudInstance.AzurePublic, authorityAudience);
return new AuthorityInfo(AuthorityType.Aad, authorityUri, validateAuthority);
}
internal static AuthorityInfo FromAadAuthority(string authorityUri, bool validateAuthority)
{
return new AuthorityInfo(AuthorityType.Aad, authorityUri, validateAuthority);
}
internal static AuthorityInfo FromAdfsAuthority(string authorityUri, bool validateAuthority)
{
return new AuthorityInfo(AuthorityType.Adfs, authorityUri, validateAuthority);
}
internal static AuthorityInfo FromB2CAuthority(string authorityUri)
{
return new AuthorityInfo(AuthorityType.B2C, authorityUri, false);
}
internal static string GetCloudUrl(AzureCloudInstance azureCloudInstance)
{
switch (azureCloudInstance)
{
case AzureCloudInstance.AzurePublic:
return "https://login.microsoftonline.com";
case AzureCloudInstance.AzureChina:
return "https://login.chinacloudapi.cn";
case AzureCloudInstance.AzureGermany:
return "https://login.microsoftonline.de";
case AzureCloudInstance.AzureUsGovernment:
return "https://login.microsoftonline.us";
default:
throw new ArgumentException(nameof(azureCloudInstance));
}
}
internal static string GetAuthorityUri(
AzureCloudInstance azureCloudInstance,
AadAuthorityAudience authorityAudience,
string tenantId = null)
{
string cloudUrl = GetCloudUrl(azureCloudInstance);
string tenantValue = GetAadAuthorityAudienceValue(authorityAudience, tenantId);
return string.Format(CultureInfo.InvariantCulture, "{0}/{1}", cloudUrl, tenantValue);
}
internal static string GetAadAuthorityAudienceValue(AadAuthorityAudience authorityAudience, string tenantId)
{
switch (authorityAudience)
{
case AadAuthorityAudience.AzureAdAndPersonalMicrosoftAccount:
return "common";
case AadAuthorityAudience.AzureAdMultipleOrgs:
return "organizations";
case AadAuthorityAudience.PersonalMicrosoftAccount:
return "consumers";
case AadAuthorityAudience.AzureAdMyOrg:
if (string.IsNullOrWhiteSpace(tenantId))
{
throw new InvalidOperationException(MsalErrorMessage.AzureAdMyOrgRequiresSpecifyingATenant);
}
return tenantId;
default:
throw new ArgumentException(nameof(authorityAudience));
}
}
// TODO: consolidate this with the same method in Authority.cs
private static string GetFirstPathSegment(string authority)
{
var uri = new Uri(authority);
if (uri.Segments.Length >= 2)
{
return new Uri(authority).Segments[1]
.TrimEnd('/');
}
throw new InvalidOperationException(MsalErrorMessage.AuthorityDoesNotHaveTwoSegments);
}
internal static string CanonicalizeAuthorityUri(string uri)
{
if (!string.IsNullOrWhiteSpace(uri) && !uri.EndsWith("/", StringComparison.OrdinalIgnoreCase))
{
uri = uri + "/";
}
return uri.ToLowerInvariant();
}
private static void ValidateAuthorityUri(string authority)
{
if (string.IsNullOrWhiteSpace(authority))
{
throw new ArgumentNullException(nameof(authority));
}
if (!Uri.IsWellFormedUriString(authority, UriKind.Absolute))
{
throw new ArgumentException(MsalErrorMessage.AuthorityInvalidUriFormat, nameof(authority));
}
var authorityUri = new Uri(authority);
if (authorityUri.Scheme != "https")
{
throw new ArgumentException(MsalErrorMessage.AuthorityUriInsecure, nameof(authority));
}
string path = authorityUri.AbsolutePath.Substring(1);
if (string.IsNullOrWhiteSpace(path))
{
throw new ArgumentException(MsalErrorMessage.AuthorityUriInvalidPath, nameof(authority));
}
string[] pathSegments = authorityUri.AbsolutePath.Substring(1).Split('/');
if (pathSegments == null || pathSegments.Length == 0)
{
throw new ArgumentException(MsalErrorMessage.AuthorityUriInvalidPath);
}
}
}
}