Skip to content

Commit bc676f9

Browse files
Fix SimpleTokenAuthentication fo ASP.NET Core v1
1 parent 698df24 commit bc676f9

File tree

4 files changed

+64
-22
lines changed

4 files changed

+64
-22
lines changed

src/MakingSense.AspNetCore.Authentication.SimpleToken/SimpleTokenAppBuilderExtensions.cs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,45 @@
33
using Microsoft.Extensions.DependencyInjection;
44
using Microsoft.Framework.Internal;
55
using Microsoft.IdentityModel.Tokens;
6+
using Microsoft.Extensions.Options;
67

78
namespace Microsoft.AspNetCore.Builder
89
{
910
public static class SimpleTokenAppBuilderExtensions
1011
{
11-
public static IApplicationBuilder UseSimpleTokenAuthentication([NotNull] this IApplicationBuilder app, Action<SimpleTokenAuthenticationOptions> configureOptions = null, string authenticationScheme = "Bearer")
12+
public static IApplicationBuilder UseSimpleTokenAuthentication(this IApplicationBuilder app)
1213
{
13-
var options = new SimpleTokenAuthenticationOptions()
14+
if (app == null)
1415
{
15-
AuthenticationScheme = authenticationScheme
16-
};
16+
throw new ArgumentNullException(nameof(app));
17+
}
18+
19+
return UseSimpleTokenAuthentication(app, new SimpleTokenAuthenticationOptions());
20+
}
21+
22+
public static IApplicationBuilder UseSimpleTokenAuthentication(this IApplicationBuilder app, SimpleTokenAuthenticationOptions options)
23+
{
24+
if (app == null)
25+
{
26+
throw new ArgumentNullException(nameof(app));
27+
}
28+
29+
if (options == null)
30+
{
31+
throw new ArgumentNullException(nameof(options));
32+
}
1733

18-
if (configureOptions != null)
34+
if (string.IsNullOrEmpty(options.AuthenticationScheme))
1935
{
20-
configureOptions(options);
36+
options.AuthenticationScheme = "Bearer";
2137
}
2238

2339
if (options.SecurityTokenValidatorsFactory == null)
2440
{
2541
options.SecurityTokenValidatorsFactory = () => app.ApplicationServices.GetServices<ISecurityTokenValidator>();
2642
}
2743

28-
return app.UseMiddleware<SimpleTokenAuthenticationMiddleware>(options);
44+
return app.UseMiddleware<SimpleTokenAuthenticationMiddleware>(Options.Create(options));
2945
}
3046
}
3147
}

src/MakingSense.AspNetCore.Authentication.SimpleToken/SimpleTokenAuthenticationHandler.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ public class SimpleTokenAuthenticationHandler : AuthenticationHandler<SimpleToke
2121
/// It does not search in Form-Encoded Body Parameter (http://tools.ietf.org/html/rfc6750#section-2.2).
2222
/// </remarks>
2323
/// <returns>
24-
/// Returns Token if found, null otherwise
24+
/// Returns Token if found, null otherwise.
2525
/// </returns>
26+
/// <exception>
27+
/// Throws AuthenticationException when Authentication header is found, but id does not contains valid data format.
28+
/// </exception>
2629
public static string ExtractToken(HttpRequest request)
2730
{
2831
var authorizationHeader = (string)request.Headers[HeaderNames.Authorization];
@@ -55,7 +58,6 @@ public static string ExtractToken(HttpRequest request)
5558
throw new AuthenticationException("Authorization header exists but does not contains valid information.");
5659
}
5760

58-
// Search in URI Query Parameter (http://tools.ietf.org/html/rfc6750#section-2.3)
5961
var tokenFromQuery = (string)request.Query["access_token"] ?? request.Query["api_key"];
6062
if (tokenFromQuery != null)
6163
{
@@ -73,16 +75,12 @@ public static string ExtractToken(HttpRequest request)
7375
/// <returns></returns>
7476
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
7577
{
76-
// Ugly patch to make this method should to be async in order to allow result caching by caller
77-
await DoneTask;
78-
79-
string token = ExtractToken(Request);
78+
var token = ExtractToken(Request);
8079

8180
// If no token found, no further work possible
8281
if (string.IsNullOrEmpty(token))
8382
{
84-
// Not so nice, but AuthenticateResult.Fail has the same behavior
85-
return null;
83+
return AuthenticateResult.Skip();
8684
}
8785

8886
var validationParameters = Options.TokenValidationParameters.Clone();
@@ -99,6 +97,9 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
9997
}
10098
}
10199

100+
// Ugly patch to make this method should to be async in order to allow result caching by caller
101+
await DoneTask;
102+
102103
// Not so nice, but AuthenticateResult.Fail does not allow us to show the error
103104
throw new AuthenticationException("Authorization token has been detected but it cannot be read.");
104105
}

src/MakingSense.AspNetCore.Authentication.SimpleToken/SimpleTokenAuthenticationMiddleware.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,41 @@ public class SimpleTokenAuthenticationMiddleware : AuthenticationMiddleware<Simp
2323
/// </summary>
2424
public SimpleTokenAuthenticationMiddleware(
2525
[NotNull] RequestDelegate next,
26-
[NotNull] IOptions<SimpleTokenAuthenticationOptions> options,
2726
[NotNull] ILoggerFactory loggerFactory,
28-
[NotNull] UrlEncoder encoder)
27+
[NotNull] UrlEncoder encoder,
28+
[NotNull] IOptions<SimpleTokenAuthenticationOptions> options)
2929
: base(next, options, loggerFactory, encoder)
3030
{
31+
if (next == null)
32+
{
33+
throw new ArgumentNullException(nameof(next));
34+
}
35+
36+
if (loggerFactory == null)
37+
{
38+
throw new ArgumentNullException(nameof(loggerFactory));
39+
}
40+
41+
if (encoder == null)
42+
{
43+
throw new ArgumentNullException(nameof(encoder));
44+
}
45+
46+
if (options == null)
47+
{
48+
throw new ArgumentNullException(nameof(options));
49+
}
50+
51+
52+
if (string.IsNullOrEmpty(Options.AuthenticationScheme))
53+
{
54+
throw new ArgumentException(nameof(Options.AuthenticationScheme));
55+
}
56+
57+
if (Options.SecurityTokenValidatorsFactory == null)
58+
{
59+
throw new ArgumentException(nameof(Options.SecurityTokenValidatorsFactory));
60+
}
3161
}
3262

3363
/// <summary>

src/MakingSense.AspNetCore.Authentication.SimpleToken/SimpleTokenAuthenticationOptions.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ public class SimpleTokenAuthenticationOptions : AuthenticationOptions
2323
/// <exception cref="ArgumentNullException">if 'value' is null.</exception>
2424
public TokenValidationParameters TokenValidationParameters { get; [param: NotNull] set; } = new TokenValidationParameters();
2525

26-
/// <summary>
27-
/// Gets or sets the challenge to put in the "WWW-Authenticate" header.
28-
/// </summary>
29-
public string Challenge { get; set; } = "Bearer";
30-
3126
/// <summary>
3227
/// Creates an instance of SimpleToken authentication options with default values.
3328
/// </summary>

0 commit comments

Comments
 (0)