-
Notifications
You must be signed in to change notification settings - Fork 218
/
AadIssuerValidator.cs
232 lines (199 loc) · 9.87 KB
/
AadIssuerValidator.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using Microsoft.Identity.Web.InstanceDiscovery;
using Microsoft.IdentityModel.JsonWebTokens;
using Microsoft.IdentityModel.Protocols;
using Microsoft.IdentityModel.Tokens;
namespace Microsoft.Identity.Web.Resource
{
/// <summary>
/// Generic class that validates token issuer from the provided Azure AD authority.
/// </summary>
public class AadIssuerValidator
{
// TODO: separate AadIssuerValidator creation logic from the validation logic in order to unit test it
private static readonly IDictionary<string, AadIssuerValidator> s_issuerValidators = new ConcurrentDictionary<string, AadIssuerValidator>();
private static readonly ConfigurationManager<IssuerMetadata> s_configManager = new ConfigurationManager<IssuerMetadata>(Constants.AzureADIssuerMetadataUrl, new IssuerConfigurationRetriever());
/// <summary>
/// A list of all Issuers across the various Azure AD instances.
/// </summary>
private readonly ISet<string> _issuerAliases;
internal /* internal for test */ AadIssuerValidator(IEnumerable<string> aliases)
{
_issuerAliases = new HashSet<string>(aliases, StringComparer.OrdinalIgnoreCase);
}
/// <summary>
/// Gets an <see cref="AadIssuerValidator"/> for an authority.
/// </summary>
/// <param name="aadAuthority">The authority to create the validator for, e.g. https://login.microsoftonline.com/. </param>
/// <returns>A <see cref="AadIssuerValidator"/> for the aadAuthority.</returns>
/// <exception cref="ArgumentNullException">if <paramref name="aadAuthority"/> is null or empty.</exception>
public static AadIssuerValidator GetIssuerValidator(string aadAuthority)
{
if (string.IsNullOrEmpty(aadAuthority))
{
throw new ArgumentNullException(nameof(aadAuthority));
}
Uri.TryCreate(aadAuthority, UriKind.Absolute, out Uri? authorityUri);
string authorityHost = authorityUri?.Authority ?? new Uri(Constants.FallbackAuthority).Authority;
if (s_issuerValidators.TryGetValue(authorityHost, out AadIssuerValidator? aadIssuerValidator))
{
return aadIssuerValidator;
}
// In the constructor, we hit the Azure AD issuer metadata endpoint and cache the aliases. The data is cached for 24 hrs.
IssuerMetadata issuerMetadata = s_configManager.GetConfigurationAsync().ConfigureAwait(false).GetAwaiter().GetResult();
// Add issuer aliases of the chosen authority to the cache
IEnumerable<string> aliases = issuerMetadata.Metadata
.Where(m => m.Aliases.Any(a => string.Equals(a, authorityHost, StringComparison.OrdinalIgnoreCase)))
.SelectMany(m => m.Aliases)
.Append(authorityHost) // For B2C scenarios, the alias will be the authority itself
.Distinct();
s_issuerValidators[authorityHost] = new AadIssuerValidator(aliases);
return s_issuerValidators[authorityHost];
}
/// <summary>
/// Validate the issuer for multi-tenant applications of various audiences (Work and School accounts, or Work and School accounts +
/// Personal accounts).
/// </summary>
/// <param name="actualIssuer">Issuer to validate (will be tenanted).</param>
/// <param name="securityToken">Received Security Token.</param>
/// <param name="validationParameters">Token Validation parameters.</param>
/// <remarks>The issuer is considered as valid if it has the same HTTP scheme and authority as the
/// authority from the configuration file, has a tenant ID, and optionally v2.0 (this web API
/// accepts both V1 and V2 tokens).
/// Authority aliasing is also taken into account.</remarks>
/// <returns>The <c>issuer</c> if it's valid, or otherwise <c>SecurityTokenInvalidIssuerException</c> is thrown.</returns>
/// <exception cref="ArgumentNullException"> if <paramref name="securityToken"/> is null.</exception>
/// <exception cref="ArgumentNullException"> if <paramref name="validationParameters"/> is null.</exception>
/// <exception cref="SecurityTokenInvalidIssuerException">if the issuer is invalid. </exception>
public string Validate(string actualIssuer, SecurityToken securityToken, TokenValidationParameters validationParameters)
{
if (string.IsNullOrEmpty(actualIssuer))
{
throw new ArgumentNullException(nameof(actualIssuer));
}
if (securityToken == null)
{
throw new ArgumentNullException(nameof(securityToken));
}
if (validationParameters == null)
{
throw new ArgumentNullException(nameof(validationParameters));
}
string tenantId = GetTenantIdFromToken(securityToken);
if (string.IsNullOrWhiteSpace(tenantId))
{
throw new SecurityTokenInvalidIssuerException(IDWebErrorMessage.TenantIdClaimNotPresentInToken);
}
if (validationParameters.ValidIssuers != null)
{
foreach (var validIssuerTemplate in validationParameters.ValidIssuers)
{
if (IsValidIssuer(validIssuerTemplate, tenantId, actualIssuer))
{
return actualIssuer;
}
}
}
if (IsValidIssuer(validationParameters.ValidIssuer, tenantId, actualIssuer))
{
return actualIssuer;
}
// If a valid issuer is not found, throw
// brentsch - todo, create a list of all the possible valid issuers in TokenValidationParameters
throw new SecurityTokenInvalidIssuerException(
string.Format(
CultureInfo.InvariantCulture,
IDWebErrorMessage.IssuerDoesNotMatchValidIssuers,
actualIssuer));
}
private bool IsValidIssuer(string validIssuerTemplate, string tenantId, string actualIssuer)
{
if (string.IsNullOrEmpty(validIssuerTemplate))
{
return false;
}
try
{
Uri issuerFromTemplateUri = new Uri(validIssuerTemplate.Replace("{tenantid}", tenantId));
Uri actualIssuerUri = new Uri(actualIssuer);
// Template authority is in the aliases
return _issuerAliases.Contains(issuerFromTemplateUri.Authority) &&
// "iss" authority is in the aliases
_issuerAliases.Contains(actualIssuerUri.Authority) &&
// Template authority ends in the tenant ID
IsValidTidInLocalPath(tenantId, issuerFromTemplateUri) &&
// "iss" ends in the tenant ID
IsValidTidInLocalPath(tenantId, actualIssuerUri);
}
catch
{
// if something faults, ignore
}
return false;
}
private static bool IsValidTidInLocalPath(string tenantId, Uri uri)
{
string trimmedLocalPath = uri.LocalPath.Trim('/');
return trimmedLocalPath == tenantId || trimmedLocalPath == $"{tenantId}/v2.0";
}
/// <summary>Gets the tenant ID from a token.</summary>
/// <param name="securityToken">A JWT token.</param>
/// <returns>A string containing the tenant ID, if found or <see cref="string.Empty"/>.</returns>
/// <remarks>Only <see cref="JwtSecurityToken"/> and <see cref="JsonWebToken"/> are acceptable types.</remarks>
private static string GetTenantIdFromToken(SecurityToken securityToken)
{
if (securityToken is JwtSecurityToken jwtSecurityToken)
{
if (jwtSecurityToken.Payload.TryGetValue(ClaimConstants.Tid, out object? tenantId))
{
return (string)tenantId;
}
// Since B2C doesn't have "tid" as default, get it from issuer
return GetTenantIdFromIss(jwtSecurityToken.Issuer);
}
if (securityToken is JsonWebToken jsonWebToken)
{
jsonWebToken.TryGetPayloadValue(ClaimConstants.Tid, out string? tid);
if (tid != null)
{
return tid;
}
// Since B2C doesn't have "tid" as default, get it from issuer
return GetTenantIdFromIss(jsonWebToken.Issuer);
}
return string.Empty;
}
// The AAD "iss" claims contains the tenant ID in its value.
// The URI can be
// - {domain}/{tid}/v2.0
// - {domain}/{tid}/v2.0/
// - {domain}/{tfp}/{tid}/{userFlow}/v2.0/
private static string GetTenantIdFromIss(string iss)
{
if (string.IsNullOrEmpty(iss))
{
return string.Empty;
}
var uri = new Uri(iss);
if (uri.Segments.Length == 3)
{
return uri.Segments[1].TrimEnd('/');
}
if (uri.Segments.Length == 5 && uri.Segments[1].TrimEnd('/') == ClaimConstants.Tfp)
{
throw new SecurityTokenInvalidIssuerException(
string.Format(
CultureInfo.InvariantCulture,
IDWebErrorMessage.B2CTfpIssuerNotSupported));
}
return string.Empty;
}
}
}