-
Notifications
You must be signed in to change notification settings - Fork 345
/
Copy pathAuthority.cs
191 lines (158 loc) · 6.79 KB
/
Authority.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
namespace Microsoft.Identity.Client.Instance
{
/// <summary>
///
/// </summary>
internal abstract class Authority
{
protected Authority(AuthorityInfo authorityInfo)
{
if (authorityInfo == null)
{
throw new ArgumentNullException(nameof(authorityInfo));
}
// Don't reuse the same authority info, instead copy it
// to prevent objects updating each other's details
AuthorityInfo = new AuthorityInfo(authorityInfo);
}
public AuthorityInfo AuthorityInfo { get; }
#region Builders
/// <summary>
/// Figures out the authority based on the authority from the config and the authority from the request,
/// and optionally the homeAccountTenantId, which has an impact on AcquireTokenSilent
///
/// The algorithm is:
///
/// 1. If there is no request authority (i.e. no authority override), use the config authority.
/// 1.1. For AAD, if the config authority is "common" etc, try to use the tenanted version with the home account tenant ID
/// 2. If there is a request authority, try to use it.
/// 2.1. If the request authority is not "common", then use it
/// 2.2 If the request authority is "common", ignore it, and use 1.1
///
/// </summary>
public static Authority CreateAuthorityForRequest(
AuthorityInfo configAuthorityInfo,
AuthorityInfo requestAuthorityInfo,
string requestHomeAccountTenantId = null)
{
if (configAuthorityInfo == null)
{
throw new ArgumentNullException(nameof(configAuthorityInfo));
}
switch (configAuthorityInfo.AuthorityType)
{
// ADFS and B2C are tenantless, no need to consider tenant
case AuthorityType.Adfs:
return requestAuthorityInfo == null ?
new AdfsAuthority(configAuthorityInfo) :
new AdfsAuthority(requestAuthorityInfo);
case AuthorityType.B2C:
if (requestAuthorityInfo != null)
{
CheckB2CAuthorityHost(requestAuthorityInfo, configAuthorityInfo);
return new B2CAuthority(requestAuthorityInfo);
}
return new B2CAuthority(configAuthorityInfo);
case AuthorityType.Aad:
if (requestAuthorityInfo == null)
{
return CreateAuthorityWithTenant(configAuthorityInfo, requestHomeAccountTenantId);
}
var requestAuthority = new AadAuthority(requestAuthorityInfo);
if (!requestAuthority.IsCommonOrganizationsOrConsumersTenant())
{
return requestAuthority;
}
return CreateAuthorityWithTenant(configAuthorityInfo, requestHomeAccountTenantId);
default:
throw new MsalClientException(
MsalError.InvalidAuthorityType,
"Unsupported authority type");
}
}
public static Authority CreateAuthority(string authority, bool validateAuthority = false)
{
return CreateAuthority(AuthorityInfo.FromAuthorityUri(authority, validateAuthority));
}
public static Authority CreateAuthority(AuthorityInfo authorityInfo)
{
switch (authorityInfo.AuthorityType)
{
case AuthorityType.Adfs:
return new AdfsAuthority(authorityInfo);
case AuthorityType.B2C:
return new B2CAuthority(authorityInfo);
case AuthorityType.Aad:
return new AadAuthority(authorityInfo);
default:
throw new MsalClientException(
MsalError.InvalidAuthorityType,
"Unsupported authority type " + authorityInfo.AuthorityType);
}
}
internal static Authority CreateAuthorityWithTenant(AuthorityInfo authorityInfo, string tenantId)
{
var initialAuthority = CreateAuthority(authorityInfo);
if (string.IsNullOrEmpty(tenantId))
{
return initialAuthority;
}
string tenantedAuthority = initialAuthority.GetTenantedAuthority(tenantId);
return CreateAuthority(tenantedAuthority, authorityInfo.ValidateAuthority);
}
internal static Authority CreateAuthorityWithEnvironment(AuthorityInfo authorityInfo, string environment)
{
var uriBuilder = new UriBuilder(authorityInfo.CanonicalAuthority)
{
Host = environment
};
return CreateAuthority(uriBuilder.Uri.AbsoluteUri, authorityInfo.ValidateAuthority);
}
#endregion Builders
internal static string GetFirstPathSegment(string authority)
{
var uri = new Uri(authority);
if (uri.Segments.Length > 1)
{
return uri.Segments[1].TrimEnd('/');
}
return string.Empty;
}
internal static AuthorityType GetAuthorityType(string authority)
{
string firstPathSegment = GetFirstPathSegment(authority);
if (string.Equals(firstPathSegment, "adfs", StringComparison.OrdinalIgnoreCase))
{
return AuthorityType.Adfs;
}
else if (string.Equals(firstPathSegment, B2CAuthority.Prefix, StringComparison.OrdinalIgnoreCase))
{
return AuthorityType.B2C;
}
else
{
return AuthorityType.Aad;
}
}
internal abstract string GetTenantId();
/// <summary>
/// Gets a tenanted authority if the current authority is tenantless.
/// Returns the original authority on B2C and ADFS
/// </summary>
internal abstract string GetTenantedAuthority(string tenantId);
private static void CheckB2CAuthorityHost(AuthorityInfo requestAuthorityInfo, AuthorityInfo configAuthorityInfo)
{
if (configAuthorityInfo.Host != requestAuthorityInfo.Host)
{
throw new MsalClientException(MsalError.B2CAuthorityHostMismatch, MsalErrorMessage.B2CAuthorityHostMisMatch);
}
}
internal static string GetEnviroment(string authority)
{
return new Uri(authority).Host;
}
}
}