-
Notifications
You must be signed in to change notification settings - Fork 41
/
ApiKeyHandlerBase.cs
240 lines (205 loc) · 8.36 KB
/
ApiKeyHandlerBase.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
// Copyright (c) Mihir Dilip. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for license information.
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Net.Http.Headers;
using System;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
namespace AspNetCore.Authentication.ApiKey
{
/// <summary>
/// Inherited from <see cref="AuthenticationHandler{TOptions}"/> for api key authentication.
/// </summary>
public abstract class ApiKeyHandlerBase : AuthenticationHandler<ApiKeyOptions>
{
#if NET8_0_OR_GREATER
protected ApiKeyHandlerBase(IOptionsMonitor<ApiKeyOptions> options, ILoggerFactory logger, UrlEncoder encoder)
: base(options, logger, encoder)
{
}
[Obsolete("ISystemClock is obsolete, use TimeProvider on AuthenticationSchemeOptions instead.")]
#endif
protected ApiKeyHandlerBase(IOptionsMonitor<ApiKeyOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
: base(options, logger, encoder, clock)
{
}
private string Challenge => $"{GetWwwAuthenticateSchemeName()} realm=\"{Options.Realm}\", charset=\"UTF-8\", in=\"{GetWwwAuthenticateInParameter()}\", key_name=\"{Options.KeyName}\"";
/// <summary>
/// Get or set <see cref="ApiKeyEvents"/>.
/// </summary>
protected new ApiKeyEvents Events { get => (ApiKeyEvents)base.Events; set => base.Events = value; }
/// <summary>
/// Create an instance of <see cref="ApiKeyEvents"/>.
/// </summary>
/// <returns></returns>
protected override Task<object> CreateEventsAsync() => Task.FromResult<object>(new ApiKeyEvents());
protected abstract Task<string> ParseApiKeyAsync();
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
{
if (IgnoreAuthenticationIfAllowAnonymous())
{
Logger.LogDebug("AllowAnonymous found on the endpoint so request was not authenticated.");
return AuthenticateResult.NoResult();
}
var apiKey = string.Empty;
try
{
apiKey = await ParseApiKeyAsync().ConfigureAwait(false);
}
catch (Exception exception)
{
Logger.LogError(exception, "Error parsing api key.");
return AuthenticateResult.Fail("Error parsing api key." + Environment.NewLine + exception.Message);
}
if (string.IsNullOrWhiteSpace(apiKey))
{
Logger.LogDebug("No Api Key found in the request.");
return AuthenticateResult.NoResult();
}
try
{
var validateCredentialsResult = await RaiseAndHandleEventValidateKeyAsync(apiKey).ConfigureAwait(false);
if (validateCredentialsResult != null)
{
// If result is set then return it.
return validateCredentialsResult;
}
// Validate using the implementation of IApiKeyProvider.
var validatedApiKey = await ValidateUsingApiKeyProviderAsync(apiKey).ConfigureAwait(false);
if (validatedApiKey == null
|| (!Options.ForLegacyIgnoreExtraValidatedApiKeyCheck && !string.Equals(validatedApiKey.Key, apiKey, StringComparison.OrdinalIgnoreCase))
)
{
Logger.LogError($"Invalid API Key provided by {nameof(IApiKeyProvider)}.");
return AuthenticateResult.Fail($"Invalid API Key provided by {nameof(IApiKeyProvider)}.");
}
return await RaiseAndHandleAuthenticationSucceededAsync(validatedApiKey).ConfigureAwait(false);
}
catch (Exception exception)
{
var authenticationFailedContext = new ApiKeyAuthenticationFailedContext(Context, Scheme, Options, exception);
await Events.AuthenticationFailedAsync(authenticationFailedContext).ConfigureAwait(false);
if (authenticationFailedContext.Result != null)
{
return authenticationFailedContext.Result;
}
throw;
}
}
/// <inheritdoc/>
protected override async Task HandleForbiddenAsync(AuthenticationProperties properties)
{
// Raise handle forbidden event.
var handleForbiddenContext = new ApiKeyHandleForbiddenContext(Context, Scheme, Options, properties);
await Events.HandleForbiddenAsync(handleForbiddenContext).ConfigureAwait(false);
if (handleForbiddenContext.IsHandled)
{
return;
}
await base.HandleForbiddenAsync(properties);
}
/// <summary>
/// Handles the un-authenticated requests.
/// Returns 401 status code in response.
/// If <see cref="ApiKeyOptions.SuppressWWWAuthenticateHeader"/> is not set then,
/// adds 'WWW-Authenticate' response header with KeyName as authentication scheme and 'Realm'
/// to let the client know which authentication scheme is being used by the system.
/// </summary>
/// <param name="properties"><see cref="AuthenticationProperties"/></param>
/// <returns>A Task.</returns>
protected override async Task HandleChallengeAsync(AuthenticationProperties properties)
{
// Raise handle challenge event.
var handleChallengeContext = new ApiKeyHandleChallengeContext(Context, Scheme, Options, properties);
await Events.HandleChallengeAsync(handleChallengeContext).ConfigureAwait(false);
if (handleChallengeContext.IsHandled)
{
return;
}
if (!Options.SuppressWWWAuthenticateHeader)
{
Response.Headers[HeaderNames.WWWAuthenticate] = Challenge;
}
await base.HandleChallengeAsync(properties);
}
private async Task<AuthenticateResult> RaiseAndHandleEventValidateKeyAsync(string apiKey)
{
var validateApiContext = new ApiKeyValidateKeyContext(Context, Scheme, Options, apiKey);
await Events.ValidateKeyAsync(validateApiContext).ConfigureAwait(false);
if (validateApiContext.Result != null)
{
return validateApiContext.Result;
}
if (validateApiContext.Principal?.Identity != null && validateApiContext.Principal.Identity.IsAuthenticated)
{
// If claims principal is set and is authenticated then build a ticket by calling and return success.
validateApiContext.Success();
return validateApiContext.Result;
}
return null;
}
private async Task<AuthenticateResult> RaiseAndHandleAuthenticationSucceededAsync(IApiKey apiKey)
{
// ..create claims principal.
var principal = ApiKeyUtils.BuildClaimsPrincipal(apiKey.OwnerName, Scheme.Name, ClaimsIssuer, apiKey.Claims);
// Raise authentication succeeded event.
var authenticationSucceededContext = new ApiKeyAuthenticationSucceededContext(Context, Scheme, Options, principal);
await Events.AuthenticationSucceededAsync(authenticationSucceededContext).ConfigureAwait(false);
if (authenticationSucceededContext.Result != null)
{
return authenticationSucceededContext.Result;
}
if (authenticationSucceededContext.Principal?.Identity != null && authenticationSucceededContext.Principal.Identity.IsAuthenticated)
{
// If claims principal is set and is authenticated then build a ticket by calling and return success.
authenticationSucceededContext.Success();
return authenticationSucceededContext.Result;
}
Logger.LogError("No authenticated prinicipal set.");
return AuthenticateResult.Fail("No authenticated prinicipal set.");
}
private async Task<IApiKey> ValidateUsingApiKeyProviderAsync(string apiKey)
{
IApiKeyProvider apiKeyProvider = null;
if (Options.ApiKeyProviderType != null)
{
apiKeyProvider = ActivatorUtilities.GetServiceOrCreateInstance(Context.RequestServices, Options.ApiKeyProviderType) as IApiKeyProvider;
}
if (apiKeyProvider == null)
{
throw new InvalidOperationException($"Either {nameof(Options.Events.OnValidateKey)} delegate on configure options {nameof(Options.Events)} should be set or use an extention method with type parameter of type {nameof(IApiKeyProvider)}.");
}
try
{
return await apiKeyProvider.ProvideAsync(apiKey).ConfigureAwait(false);
}
finally
{
if (apiKeyProvider is IDisposable disposableApiKeyProvider)
{
disposableApiKeyProvider.Dispose();
}
}
}
private string GetWwwAuthenticateSchemeName()
{
return Options.ForLegacyUseKeyNameAsSchemeNameOnWWWAuthenticateHeader
? Options.KeyName
: Scheme.Name;
}
protected abstract string GetWwwAuthenticateInParameter();
private bool IgnoreAuthenticationIfAllowAnonymous()
{
#if (NET461 || NETSTANDARD2_0)
return false;
#else
return Options.IgnoreAuthenticationIfAllowAnonymous
&& Context.GetEndpoint()?.Metadata?.GetMetadata<Microsoft.AspNetCore.Authorization.IAllowAnonymous>() != null;
#endif
}
}
}