Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Commit 3f59610

Browse files
committed
#690 OIDC & JWT event refactoring.
1 parent 6a0e58e commit 3f59610

File tree

26 files changed

+277
-489
lines changed

26 files changed

+277
-489
lines changed

samples/JwtBearerSample/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"commandName": "IISExpress",
1313
"launchBrowser": true,
1414
"environmentVariables": {
15-
"ASPNET_ENVIRONMENT": "Development"
15+
"ASPNETCORE_ENVIRONMENT": "Development"
1616
}
1717
},
1818
"web": {

samples/JwtBearerSample/Startup.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ public void Configure(IApplicationBuilder app)
6161

6262
app.UseJwtBearerAuthentication(new JwtBearerOptions
6363
{
64-
AutomaticAuthenticate = true,
65-
AutomaticChallenge = true,
6664
// You also need to update /wwwroot/app/scripts/app.js
6765
Authority = Configuration["jwt:authority"],
6866
Audience = Configuration["jwt:audience"]
@@ -74,14 +72,14 @@ public void Configure(IApplicationBuilder app)
7472
// Use this if options.AutomaticAuthenticate = false
7573
// var user = await context.Authentication.AuthenticateAsync(JwtBearerDefaults.AuthenticationScheme);
7674

77-
var user = context.User; // We can do this because of options.AutomaticAuthenticate = true; above.
75+
var user = context.User; // We can do this because of options.AutomaticAuthenticate = true;
7876
if (user?.Identity?.IsAuthenticated ?? false)
7977
{
8078
await next();
8179
}
8280
else
8381
{
84-
// We can do this because of options.AutomaticChallenge = true; above
82+
// We can do this because of options.AutomaticChallenge = true;
8583
await context.Authentication.ChallengeAsync();
8684
}
8785
});

samples/JwtBearerSample/project.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"web": "JwtBearerSample"
1616
},
1717
"frameworks": {
18-
"dnx451": {},
18+
"dnx451": { },
1919
"netstandardapp1.5": {
2020
"imports": [
2121
"dnxcore50"
@@ -30,5 +30,9 @@
3030
"**.user",
3131
"**.vspscc"
3232
],
33+
"content": [
34+
"project.json",
35+
"wwwroot"
36+
],
3337
"userSecretsId": "aspnet5-JwtBearerSample-20151210102827"
3438
}

samples/OpenIdConnect.AzureAdSample/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"commandName": "IISExpress",
1313
"launchBrowser": true,
1414
"environmentVariables": {
15-
"Hosting:Environment": "Development"
15+
"ASPNETCORE_ENVIRONMENT": "Development"
1616
}
1717
},
1818
"web": {

samples/OpenIdConnect.AzureAdSample/Startup.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory)
6464

6565
app.UseIISPlatformHandler();
6666

67-
app.UseCookieAuthentication(new CookieAuthenticationOptions
68-
{
69-
AutomaticAuthenticate = true
70-
});
67+
app.UseCookieAuthentication(new CookieAuthenticationOptions());
7168

7269
var clientId = Configuration["oidc:clientid"];
7370
var clientSecret = Configuration["oidc:clientsecret"];

samples/OpenIdConnect.AzureAdSample/project.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,8 @@
1818
"commands": {
1919
"web": "OpenIdConnect.AzureAdSample"
2020
},
21+
"content": [
22+
"project.json"
23+
],
2124
"userSecretsId": "aspnet5-OpenIdConnectSample-20151210110318"
2225
}

samples/OpenIdConnectSample/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"commandName": "IISExpress",
1313
"launchBrowser": true,
1414
"environmentVariables": {
15-
"ASPNET_ENV": "Development"
15+
"ASPNETCORE_ENVIRONMENT": "Development"
1616
}
1717
},
1818
"web": {

samples/OpenIdConnectSample/Startup.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,7 @@ public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory)
5959

6060
app.UseIISPlatformHandler();
6161

62-
app.UseCookieAuthentication(new CookieAuthenticationOptions
63-
{
64-
AutomaticAuthenticate = true
65-
});
62+
app.UseCookieAuthentication(new CookieAuthenticationOptions());
6663

6764
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
6865
{

samples/OpenIdConnectSample/project.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"Microsoft.NETCore.Platforms": "1.0.1-*"
1010
},
1111
"frameworks": {
12-
"dnx451": {},
12+
"dnx451": { },
1313
"netstandardapp1.5": {
1414
"imports": [
1515
"dnxcore50"
@@ -22,5 +22,8 @@
2222
"commands": {
2323
"web": "OpenIdConnectSample"
2424
},
25+
"content": [
26+
"project.json"
27+
],
2528
"userSecretsId": "aspnet5-OpenIdConnectSample-20151210110318"
2629
}

src/Microsoft.AspNetCore.Authentication.JwtBearer/Events/IJwtBearerEvents.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,12 @@ public interface IJwtBearerEvents
1818
/// <summary>
1919
/// Invoked when a protocol message is first received.
2020
/// </summary>
21-
Task ReceivingToken(ReceivingTokenContext context);
22-
23-
/// <summary>
24-
/// Invoked with the security token that has been extracted from the protocol message.
25-
/// </summary>
26-
Task ReceivedToken(ReceivedTokenContext context);
21+
Task MessageReceived(MessageReceivedContext context);
2722

2823
/// <summary>
2924
/// Invoked after the security token has passed validation and a ClaimsIdentity has been generated.
3025
/// </summary>
31-
Task ValidatedToken(ValidatedTokenContext context);
26+
Task TokenValidated(TokenValidatedContext context);
3227

3328
/// <summary>
3429
/// Invoked to apply a challenge sent back to the caller.

src/Microsoft.AspNetCore.Authentication.JwtBearer/Events/JwtBearerEvents.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,12 @@ public class JwtBearerEvents : IJwtBearerEvents
1919
/// <summary>
2020
/// Invoked when a protocol message is first received.
2121
/// </summary>
22-
public Func<ReceivingTokenContext, Task> OnReceivingToken { get; set; } = context => Task.FromResult(0);
23-
24-
/// <summary>
25-
/// Invoked with the security token that has been extracted from the protocol message.
26-
/// </summary>
27-
public Func<ReceivedTokenContext, Task> OnReceivedToken { get; set; } = context => Task.FromResult(0);
22+
public Func<MessageReceivedContext, Task> OnMessageReceived { get; set; } = context => Task.FromResult(0);
2823

2924
/// <summary>
3025
/// Invoked after the security token has passed validation and a ClaimsIdentity has been generated.
3126
/// </summary>
32-
public Func<ValidatedTokenContext, Task> OnValidatedToken { get; set; } = context => Task.FromResult(0);
27+
public Func<TokenValidatedContext, Task> OnTokenValidated { get; set; } = context => Task.FromResult(0);
3328

3429
/// <summary>
3530
/// Invoked before a challenge is sent back to the caller.
@@ -38,11 +33,9 @@ public class JwtBearerEvents : IJwtBearerEvents
3833

3934
public virtual Task AuthenticationFailed(AuthenticationFailedContext context) => OnAuthenticationFailed(context);
4035

41-
public virtual Task ReceivingToken(ReceivingTokenContext context) => OnReceivingToken(context);
42-
43-
public virtual Task ReceivedToken(ReceivedTokenContext context) => OnReceivedToken(context);
36+
public virtual Task MessageReceived(MessageReceivedContext context) => OnMessageReceived(context);
4437

45-
public virtual Task ValidatedToken(ValidatedTokenContext context) => OnValidatedToken(context);
38+
public virtual Task TokenValidated(TokenValidatedContext context) => OnTokenValidated(context);
4639

4740
public virtual Task Challenge(JwtBearerChallengeContext context) => OnChallenge(context);
4841
}

src/Microsoft.AspNetCore.Authentication.JwtBearer/Events/ReceivingTokenContext.cs renamed to src/Microsoft.AspNetCore.Authentication.JwtBearer/Events/MessageReceivedContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
namespace Microsoft.AspNetCore.Authentication.JwtBearer
88
{
9-
public class ReceivingTokenContext : BaseJwtBearerContext
9+
public class MessageReceivedContext : BaseJwtBearerContext
1010
{
11-
public ReceivingTokenContext(HttpContext context, JwtBearerOptions options)
11+
public MessageReceivedContext(HttpContext context, JwtBearerOptions options)
1212
: base(context, options)
1313
{
1414
}

src/Microsoft.AspNetCore.Authentication.JwtBearer/Events/ReceivedTokenContext.cs

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/Microsoft.AspNetCore.Authentication.JwtBearer/Events/TokenValidatedContext.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33

44
using Microsoft.AspNetCore.Builder;
55
using Microsoft.AspNetCore.Http;
6+
using Microsoft.IdentityModel.Tokens;
67

78
namespace Microsoft.AspNetCore.Authentication.JwtBearer
89
{
9-
public class ValidatedTokenContext : BaseJwtBearerContext
10+
public class TokenValidatedContext : BaseJwtBearerContext
1011
{
11-
public ValidatedTokenContext(HttpContext context, JwtBearerOptions options)
12+
public TokenValidatedContext(HttpContext context, JwtBearerOptions options)
1213
: base(context, options)
1314
{
1415
}
16+
17+
public SecurityToken SecurityToken { get; set; }
1518
}
1619
}

src/Microsoft.AspNetCore.Authentication.JwtBearer/JwtBearerHandler.cs

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,21 @@ internal class JwtBearerHandler : AuthenticationHandler<JwtBearerOptions>
2828
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
2929
{
3030
string token = null;
31+
AuthenticateResult result = null;
3132
try
3233
{
3334
// Give application opportunity to find from a different location, adjust, or reject token
34-
var receivingTokenContext = new ReceivingTokenContext(Context, Options);
35+
var messageReceivedContext = new MessageReceivedContext(Context, Options);
3536

3637
// event can set the token
37-
await Options.Events.ReceivingToken(receivingTokenContext);
38-
if (receivingTokenContext.HandledResponse)
38+
await Options.Events.MessageReceived(messageReceivedContext);
39+
if (messageReceivedContext.CheckEventResult(out result))
3940
{
40-
return AuthenticateResult.Success(receivingTokenContext.Ticket);
41-
}
42-
if (receivingTokenContext.Skipped)
43-
{
44-
return AuthenticateResult.Skip();
41+
return result;
4542
}
4643

4744
// If application retrieved token from somewhere else, use that.
48-
token = receivingTokenContext.Token;
45+
token = messageReceivedContext.Token;
4946

5047
if (string.IsNullOrEmpty(token))
5148
{
@@ -69,22 +66,6 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
6966
}
7067
}
7168

72-
// notify user token was received
73-
var receivedTokenContext = new ReceivedTokenContext(Context, Options)
74-
{
75-
Token = token,
76-
};
77-
78-
await Options.Events.ReceivedToken(receivedTokenContext);
79-
if (receivedTokenContext.HandledResponse)
80-
{
81-
return AuthenticateResult.Success(receivedTokenContext.Ticket);
82-
}
83-
if (receivedTokenContext.Skipped)
84-
{
85-
return AuthenticateResult.Skip();
86-
}
87-
8869
if (_configuration == null && Options.ConfigurationManager != null)
8970
{
9071
_configuration = await Options.ConfigurationManager.GetConfigurationAsync(Context.RequestAborted);
@@ -138,20 +119,18 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
138119
Logger.TokenValidationSucceeded();
139120

140121
var ticket = new AuthenticationTicket(principal, new AuthenticationProperties(), Options.AuthenticationScheme);
141-
var validatedTokenContext = new ValidatedTokenContext(Context, Options)
122+
var tokenValidatedContext = new TokenValidatedContext(Context, Options)
142123
{
143-
Ticket = ticket
124+
Ticket = ticket,
125+
SecurityToken = validatedToken,
144126
};
145127

146-
await Options.Events.ValidatedToken(validatedTokenContext);
147-
if (validatedTokenContext.HandledResponse)
148-
{
149-
return AuthenticateResult.Success(validatedTokenContext.Ticket);
150-
}
151-
if (validatedTokenContext.Skipped)
128+
await Options.Events.TokenValidated(tokenValidatedContext);
129+
if (tokenValidatedContext.CheckEventResult(out result))
152130
{
153-
return AuthenticateResult.Skip();
131+
return result;
154132
}
133+
ticket = tokenValidatedContext.Ticket;
155134

156135
if (Options.SaveToken)
157136
{
@@ -173,13 +152,9 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
173152
};
174153

175154
await Options.Events.AuthenticationFailed(authenticationFailedContext);
176-
if (authenticationFailedContext.HandledResponse)
177-
{
178-
return AuthenticateResult.Success(authenticationFailedContext.Ticket);
179-
}
180-
if (authenticationFailedContext.Skipped)
155+
if (authenticationFailedContext.CheckEventResult(out result))
181156
{
182-
return AuthenticateResult.Skip();
157+
return result;
183158
}
184159

185160
return AuthenticateResult.Fail(authenticationFailedContext.Exception);
@@ -197,13 +172,9 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
197172
};
198173

199174
await Options.Events.AuthenticationFailed(authenticationFailedContext);
200-
if (authenticationFailedContext.HandledResponse)
201-
{
202-
return AuthenticateResult.Success(authenticationFailedContext.Ticket);
203-
}
204-
if (authenticationFailedContext.Skipped)
175+
if (authenticationFailedContext.CheckEventResult(out result))
205176
{
206-
return AuthenticateResult.Skip();
177+
return result;
207178
}
208179

209180
throw;

src/Microsoft.AspNetCore.Authentication.JwtBearer/JwtBearerOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public class JwtBearerOptions : AuthenticationOptions
2525
public JwtBearerOptions() : base()
2626
{
2727
AuthenticationScheme = JwtBearerDefaults.AuthenticationScheme;
28+
AutomaticAuthenticate = true;
29+
AutomaticChallenge = true;
2830
}
2931

3032
/// <summary>

src/Microsoft.AspNetCore.Authentication.OpenIdConnect/Events/AuthorizationResponseReceivedContext.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)