-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
ChallengeBasedAuthenticationPolicy.cs
286 lines (238 loc) · 11.3 KB
/
ChallengeBasedAuthenticationPolicy.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using Azure.Core;
using Azure.Core.Pipeline;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading.Tasks;
namespace Azure.Security.KeyVault
{
internal class ChallengeBasedAuthenticationPolicy : HttpPipelinePolicy
{
private const string BearerChallengePrefix = "Bearer ";
private readonly TokenCredential _credential;
private AuthenticationChallenge _challenge = null;
private string _headerValue;
private DateTimeOffset _refreshOn;
public ChallengeBasedAuthenticationPolicy(TokenCredential credential)
{
_credential = credential;
}
public override void Process(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline)
{
ProcessCoreAsync(message, pipeline, false).EnsureCompleted();
}
public override ValueTask ProcessAsync(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline)
{
return ProcessCoreAsync(message, pipeline, true);
}
private async ValueTask ProcessCoreAsync(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline, bool async)
{
if (message.Request.Uri.Scheme != Uri.UriSchemeHttps)
{
throw new InvalidOperationException("Bearer token authentication is not permitted for non TLS protected (https) endpoints.");
}
RequestContent originalContent = message.Request.Content;
// if this policy doesn't have _challenge cached try to get it from the static challenge cache
AuthenticationChallenge challenge = _challenge ?? AuthenticationChallenge.GetChallenge(message);
// if we still don't have the challenge for the endpoint
// remove the content from the request and send without authentication to get the challenge
if (challenge == null)
{
message.Request.Content = null;
}
// otherwise if we already know the challenge authenticate the request
else
{
await AuthenticateRequestAsync(message, async, challenge).ConfigureAwait(false);
}
if (async)
{
await ProcessNextAsync(message, pipeline).ConfigureAwait(false);
}
else
{
ProcessNext(message, pipeline);
}
// if we get a 401
if (message.Response.Status == 401)
{
// set the content to the original content in case it was cleared
message.Request.Content = originalContent;
// update the cached challenge
challenge = AuthenticationChallenge.GetChallenge(message);
if (challenge != null)
{
// update the cached challenge if not yet set or different from the current challenge (e.g. moved tenants)
if (_challenge == null || !challenge.Equals(_challenge))
{
_challenge = challenge;
}
// authenticate the request and resend
await AuthenticateRequestAsync(message, async, challenge).ConfigureAwait(false);
if (async)
{
await ProcessNextAsync(message, pipeline).ConfigureAwait(false);
}
else
{
ProcessNext(message, pipeline);
}
}
}
}
private async Task AuthenticateRequestAsync(HttpMessage message, bool async, AuthenticationChallenge challenge)
{
if (_headerValue is null || DateTimeOffset.UtcNow >= _refreshOn)
{
AccessToken token = async ?
await _credential.GetTokenAsync(new TokenRequestContext(challenge.Scopes, message.Request.ClientRequestId), message.CancellationToken).ConfigureAwait(false) :
_credential.GetToken(new TokenRequestContext(challenge.Scopes, message.Request.ClientRequestId), message.CancellationToken);
_headerValue = BearerChallengePrefix + token.Token;
_refreshOn = token.ExpiresOn - TimeSpan.FromMinutes(2);
}
message.Request.Headers.SetValue(HttpHeader.Names.Authorization, _headerValue);
}
internal class AuthenticationChallenge
{
private static readonly Dictionary<string, AuthenticationChallenge> s_cache = new Dictionary<string, AuthenticationChallenge>();
private static readonly object s_cacheLock = new object();
private static readonly string[] s_challengeDelimiters = new string[] { "," };
private AuthenticationChallenge(string authority, string scope)
{
Authority = authority;
Scopes = new string[] { scope };
}
public string Authority { get; }
public string[] Scopes { get; }
public override bool Equals(object obj)
{
if (ReferenceEquals(this, obj))
{
return true;
}
// This assumes that Authority Scopes are always non-null and Scopes has a length of one.
// This is guaranteed by the way the AuthenticationChallenge cache is constructed.
if (obj is AuthenticationChallenge other)
{
return string.Equals(Authority, other.Authority, StringComparison.OrdinalIgnoreCase)
&& string.Equals(Scopes[0], other.Scopes[0], StringComparison.OrdinalIgnoreCase);
}
return false;
}
public override int GetHashCode()
{
// Currently the hash code is simply the hash of the authority and first scope as this is what is used to determine equality.
// This assumes that Authority Scopes are always non-null and Scopes has a length of one.
// This is guaranteed by the way the AuthenticationChallenge cache is constructed.
return HashCodeBuilder.Combine(Authority, Scopes[0]);
}
public static AuthenticationChallenge GetChallenge(HttpMessage message)
{
AuthenticationChallenge challenge = null;
if (message.HasResponse)
{
challenge = GetChallengeFromResponse(message.Response);
// if the challenge is non-null cache it
if (challenge != null)
{
string authority = GetRequestAuthority(message.Request);
lock (s_cacheLock)
{
s_cache[authority] = challenge;
}
}
}
else
{
// try to get the challenge from the cache
string authority = GetRequestAuthority(message.Request);
lock (s_cacheLock)
{
s_cache.TryGetValue(authority, out challenge);
}
}
return challenge;
}
internal static void ClearCache()
{
// try to get the challenge from the cache
lock (s_cacheLock)
{
s_cache.Clear();
}
}
private static AuthenticationChallenge GetChallengeFromResponse(Response response)
{
AuthenticationChallenge challenge = null;
if (response.Headers.TryGetValue("WWW-Authenticate", out string challengeValue) && challengeValue.StartsWith(BearerChallengePrefix, StringComparison.OrdinalIgnoreCase))
{
challenge = ParseBearerChallengeHeaderValue(challengeValue);
}
return challenge;
}
private static AuthenticationChallenge ParseBearerChallengeHeaderValue(string challengeValue)
{
string authority = null;
string scope = null;
// remove the bearer challenge prefix
var trimmedChallenge = challengeValue.Substring(BearerChallengePrefix.Length);
// Split the trimmed challenge into a set of name=value strings that
// are comma separated. The value fields are expected to be within
// quotation characters that are stripped here.
string[] pairs = trimmedChallenge.Split(s_challengeDelimiters, StringSplitOptions.RemoveEmptyEntries);
if (pairs.Length > 0)
{
// Process the name=value string
for (int i = 0; i < pairs.Length; i++)
{
string[] pair = pairs[i].Split('=');
if (pair.Length == 2)
{
// We have a key and a value, now need to trim and decode
string key = pair[0].AsSpan().Trim().Trim('\"').ToString();
string value = pair[1].AsSpan().Trim().Trim('\"').ToString();
if (!string.IsNullOrEmpty(key))
{
// Ordered by current likelihood.
if (string.Equals(key, "authorization", StringComparison.OrdinalIgnoreCase))
{
authority = value;
}
else if (string.Equals(key, "resource", StringComparison.OrdinalIgnoreCase))
{
scope = value + "/.default";
}
else if (string.Equals(key, "scope", StringComparison.OrdinalIgnoreCase))
{
scope = value;
}
else if (string.Equals(key, "authorization_uri", StringComparison.OrdinalIgnoreCase))
{
authority = value;
}
}
}
}
}
if (authority != null && scope != null)
{
return new AuthenticationChallenge(authority, scope);
}
return null;
}
private static string GetRequestAuthority(Request request)
{
Uri uri = request.Uri.ToUri();
string authority = uri.Authority;
if (!authority.Contains(":") && uri.Port > 0)
{
// Append port for complete authority
authority = uri.Authority + ":" + uri.Port.ToString(CultureInfo.InvariantCulture);
}
return authority;
}
}
}
}