-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJwtBearerMiddlewareDiagnostics.cs
91 lines (77 loc) · 3.45 KB
/
JwtBearerMiddlewareDiagnostics.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
using Microsoft.AspNetCore.Authentication.JwtBearer;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
namespace oauthy
{
/// <summary>
/// Diagnostics for the JwtBearer middleware (used in Web APIs)
/// </summary>
public class JwtBearerMiddlewareDiagnostics
{
/// <summary>
/// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed.
/// </summary>
static Func<AuthenticationFailedContext, Task> onAuthenticationFailed;
/// <summary>
/// Invoked when a protocol message is first received.
/// </summary>
static Func<MessageReceivedContext, Task> onMessageReceived;
/// <summary>
/// Invoked after the security token has passed validation and a ClaimsIdentity has been generated.
/// </summary>
static Func<TokenValidatedContext, Task> onTokenValidated;
/// <summary>
/// Invoked before a challenge is sent back to the caller.
/// </summary>
static Func<JwtBearerChallengeContext, Task> onChallenge;
/// <summary>
/// Subscribes to all the JwtBearer events, to help debugging, while
/// preserving the previous handlers (which are called)
/// </summary>
/// <param name="events">Events to subscribe to</param>
public static JwtBearerEvents Subscribe(JwtBearerEvents events)
{
if (events == null)
{
events = new JwtBearerEvents();
}
onAuthenticationFailed = events.OnAuthenticationFailed;
events.OnAuthenticationFailed = OnAuthenticationFailed;
onMessageReceived = events.OnMessageReceived;
events.OnMessageReceived = OnMessageReceived;
onTokenValidated = events.OnTokenValidated;
events.OnTokenValidated = OnTokenValidated;
onChallenge = events.OnChallenge;
events.OnChallenge = OnChallenge;
return events;
}
static async Task OnMessageReceived(MessageReceivedContext context)
{
Debug.WriteLine($"1. Begin {nameof(OnMessageReceived)}");
// Place a breakpoint here and examine the bearer token (context.Request.Headers.HeaderAuthorization / context.Request.Headers["Authorization"])
// Use https://jwt.ms to decode the token and observe claims
await onMessageReceived(context);
Debug.WriteLine($"1. End - {nameof(OnMessageReceived)}");
}
static async Task OnAuthenticationFailed(AuthenticationFailedContext context)
{
Debug.WriteLine($"99. Begin {nameof(OnAuthenticationFailed)}");
// Place a breakpoint here and examine context.Exception
await onAuthenticationFailed(context);
Debug.WriteLine($"99. End - {nameof(OnAuthenticationFailed)}");
}
static async Task OnTokenValidated(TokenValidatedContext context)
{
Debug.WriteLine($"2. Begin {nameof(OnTokenValidated)}");
await onTokenValidated(context);
Debug.WriteLine($"2. End - {nameof(OnTokenValidated)}");
}
static async Task OnChallenge(JwtBearerChallengeContext context)
{
Debug.WriteLine($"55. Begin {nameof(OnChallenge)}");
await onChallenge(context);
Debug.WriteLine($"55. End - {nameof(OnChallenge)}");
}
}
}