Skip to content

Commit d1d7fd0

Browse files
Fix SimpleTokenAuthenticationHandler for RC1
There were changes in failed authentication management, I have to test if my decisions were fine.
1 parent 600f468 commit d1d7fd0

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ namespace Microsoft.AspNet.Builder
88
{
99
public static class SimpleTokenAppBuilderExtensions
1010
{
11-
public static IApplicationBuilder UseSimpleTokenAuthentication([NotNull] this IApplicationBuilder app, Action<SimpleTokenAuthenticationOptions> configureOptions = null, string optionsName = "")
11+
public static IApplicationBuilder UseSimpleTokenAuthentication([NotNull] this IApplicationBuilder app, Action<SimpleTokenAuthenticationOptions> configureOptions = null, string authenticationScheme = "Bearer")
1212
{
1313
var options = new SimpleTokenAuthenticationOptions()
1414
{
15-
AuthenticationScheme = optionsName
15+
AuthenticationScheme = authenticationScheme
1616
};
1717

1818
if (configureOptions != null)

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public static string ExtractToken(HttpRequest request)
5050
}
5151
}
5252

53+
// Not so nice, but AuthenticateResult.Fail does not allow us to show the error
5354
throw new AuthenticationException("Authorization header exists but does not contains valid information.");
5455
}
5556

@@ -63,19 +64,24 @@ public static string ExtractToken(HttpRequest request)
6364
return null;
6465
}
6566

67+
static readonly Task DoneTask = Task.FromResult(0);
6668

6769
/// <summary>
6870
/// Searches the 'Authorization' header for a 'Bearer' token. If the 'Bearer' token is found, it is validated using <see cref="TokenValidationParameters"/> set in the options.
6971
/// </summary>
7072
/// <returns></returns>
71-
protected override Task<AuthenticationTicket> HandleAuthenticateAsync()
73+
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
7274
{
75+
// Ugly patch to make this method should to be async in order to allow result caching by caller
76+
await DoneTask;
77+
7378
string token = ExtractToken(Request);
7479

7580
// If no token found, no further work possible
7681
if (string.IsNullOrEmpty(token))
7782
{
78-
return Task.FromResult<AuthenticationTicket>(null);
83+
// Not so nice, but AuthenticateResult.Fail has the same behavior
84+
return null;
7985
}
8086

8187
var validationParameters = Options.TokenValidationParameters.Clone();
@@ -88,10 +94,11 @@ protected override Task<AuthenticationTicket> HandleAuthenticateAsync()
8894
{
8995
var principal = validator.ValidateToken(token, validationParameters, out validatedToken);
9096
var ticket = new AuthenticationTicket(principal, new AuthenticationProperties(), Options.AuthenticationScheme);
91-
return Task.FromResult(ticket);
97+
return AuthenticateResult.Success(ticket);
9298
}
9399
}
94100

101+
// Not so nice, but AuthenticateResult.Fail does not allow us to show the error
95102
throw new AuthenticationException("Authorization token has been detected but it cannot be read.");
96103
}
97104
}

0 commit comments

Comments
 (0)