-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding diagnostics #35
Merged
jmprieur
merged 3 commits into
aspnetcore2-2-signInAndCallGraph
from
jmprieur/addOpenIdConnectDiagnostics
Jan 8, 2019
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
using Microsoft.AspNetCore.Authentication.OpenIdConnect; | ||
using Microsoft.IdentityModel.Protocols.OpenIdConnect; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.AspNetCore.Authentication | ||
{ | ||
public class OpenIdConnectMiddlewareDiagnostics | ||
{ | ||
// | ||
// Summary: | ||
// Invoked before redirecting to the identity provider to authenticate. This can | ||
// be used to set ProtocolMessage.State that will be persisted through the authentication | ||
// process. The ProtocolMessage can also be used to add or customize parameters | ||
// sent to the identity provider. | ||
static Func<RedirectContext, Task> onRedirectToIdentityProvider; | ||
// | ||
// Summary: | ||
// Invoked when a protocol message is first received. | ||
static Func<MessageReceivedContext, Task> onMessageReceived; | ||
// | ||
// Summary: | ||
// Invoked after security token validation if an authorization code is present in | ||
// the protocol message. | ||
static Func<AuthorizationCodeReceivedContext, Task> onAuthorizationCodeReceived; | ||
// | ||
// Summary: | ||
// Invoked after "authorization code" is redeemed for tokens at the token endpoint. | ||
static Func<TokenResponseReceivedContext, Task> onTokenResponseReceived; | ||
// | ||
// Summary: | ||
// Invoked when an IdToken has been validated and produced an AuthenticationTicket. | ||
static Func<TokenValidatedContext, Task> onTokenValidated; | ||
// | ||
// Summary: | ||
// Invoked when user information is retrieved from the UserInfoEndpoint. | ||
static Func<UserInformationReceivedContext, Task> onUserInformationReceived; | ||
// | ||
// Summary: | ||
// Invoked if exceptions are thrown during request processing. The exceptions will | ||
// be re-thrown after this event unless suppressed. | ||
static Func<AuthenticationFailedContext, Task> onAuthenticationFailed; | ||
// | ||
// Summary: | ||
// Invoked when a request is received on the RemoteSignOutPath. | ||
static Func<RemoteSignOutContext, Task> onRemoteSignOut; | ||
// | ||
// Summary: | ||
// Invoked before redirecting to the identity provider to sign out. | ||
static Func<RedirectContext, Task> onRedirectToIdentityProviderForSignOut; | ||
// | ||
// Summary: | ||
// Invoked before redirecting to the Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectOptions.SignedOutRedirectUri | ||
// at the end of a remote sign-out flow. | ||
static Func<RemoteSignOutContext, Task> onSignedOutCallbackRedirect; | ||
|
||
|
||
/// <summary> | ||
/// Subscribes to all the OpenIdConnect events, to help debugging, while | ||
/// preserving the previous handlers (which are called) | ||
/// </summary> | ||
/// <param name="events">Events to subscribe to</param> | ||
public static void Subscribe(OpenIdConnectEvents events) | ||
{ | ||
onRedirectToIdentityProvider = events.OnRedirectToIdentityProvider; | ||
events.OnRedirectToIdentityProvider = OnRedirectToIdentityProvider; | ||
|
||
onMessageReceived = events.OnMessageReceived; | ||
events.OnMessageReceived = OnMessageReceived; | ||
|
||
onAuthorizationCodeReceived = events.OnAuthorizationCodeReceived; | ||
events.OnAuthorizationCodeReceived = OnAuthorizationCodeReceived; | ||
|
||
onTokenResponseReceived = events.OnTokenResponseReceived; | ||
events.OnTokenResponseReceived = OnTokenResponseReceived; | ||
|
||
onTokenValidated = events.OnTokenValidated; | ||
events.OnTokenValidated = OnTokenValidated; | ||
|
||
onUserInformationReceived = events.OnUserInformationReceived; | ||
events.OnUserInformationReceived = OnUserInformationReceived; | ||
|
||
onAuthenticationFailed = events.OnAuthenticationFailed; | ||
events.OnAuthenticationFailed = OnAuthenticationFailed; | ||
|
||
onRemoteSignOut = events.OnRemoteSignOut; | ||
events.OnRemoteSignOut = OnRemoteSignOut; | ||
|
||
onRedirectToIdentityProviderForSignOut = events.OnRedirectToIdentityProviderForSignOut; | ||
events.OnRedirectToIdentityProviderForSignOut = OnRedirectToIdentityProviderForSignOut; | ||
|
||
onSignedOutCallbackRedirect = events.OnSignedOutCallbackRedirect; | ||
events.OnSignedOutCallbackRedirect = OnSignedOutCallbackRedirect; | ||
} | ||
|
||
static async Task OnRedirectToIdentityProvider(RedirectContext context) | ||
{ | ||
Debug.WriteLine($"1. Begin {nameof(OnRedirectToIdentityProvider)}"); | ||
|
||
await onRedirectToIdentityProvider(context); | ||
|
||
Debug.WriteLine($" Sending OpenIdConnect message:"); | ||
DisplayProtocolMessage(context.ProtocolMessage); | ||
Debug.WriteLine($"1. End - {nameof(OnRedirectToIdentityProvider)}"); | ||
} | ||
|
||
private static void DisplayProtocolMessage(OpenIdConnectMessage message) | ||
{ | ||
foreach (var property in message.GetType().GetProperties()) | ||
{ | ||
object value = property.GetValue(message); | ||
if (value != null) | ||
{ | ||
Debug.WriteLine($" - {property.Name}={value}"); | ||
} | ||
} | ||
} | ||
|
||
static async Task OnMessageReceived(MessageReceivedContext context) | ||
{ | ||
Debug.WriteLine($"2. Begin {nameof(OnMessageReceived)}"); | ||
Debug.WriteLine($" Received from STS the OpenIdConnect message:"); | ||
DisplayProtocolMessage(context.ProtocolMessage); | ||
await onMessageReceived(context); | ||
Debug.WriteLine($"2. End - {nameof(OnMessageReceived)}"); | ||
} | ||
|
||
static async Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedContext context) | ||
{ | ||
Debug.WriteLine($"4. Begin {nameof(OnAuthorizationCodeReceived)}"); | ||
await onAuthorizationCodeReceived(context); | ||
Debug.WriteLine($"4. End - {nameof(OnAuthorizationCodeReceived)}"); | ||
} | ||
|
||
static async Task OnTokenResponseReceived(TokenResponseReceivedContext context) | ||
{ | ||
Debug.WriteLine($"5. Begin {nameof(OnTokenResponseReceived)}"); | ||
await onTokenResponseReceived(context); | ||
Debug.WriteLine($"5. End - {nameof(OnTokenResponseReceived)}"); | ||
|
||
} | ||
static async Task OnTokenValidated(TokenValidatedContext context) | ||
{ | ||
Debug.WriteLine($"3. Begin {nameof(OnTokenValidated)}"); | ||
await onTokenValidated(context); | ||
Debug.WriteLine($"3. End - {nameof(OnTokenValidated)}"); | ||
} | ||
static async Task OnUserInformationReceived(UserInformationReceivedContext context) | ||
{ | ||
Debug.WriteLine($"6. Begin {nameof(OnUserInformationReceived)}"); | ||
await onUserInformationReceived(context); | ||
Debug.WriteLine($"6. End - {nameof(OnUserInformationReceived)}"); | ||
} | ||
|
||
static async Task OnAuthenticationFailed(AuthenticationFailedContext context) | ||
{ | ||
Debug.WriteLine($"99. Begin {nameof(OnAuthenticationFailed)}"); | ||
await onAuthenticationFailed(context); | ||
Debug.WriteLine($"99. End - {nameof(OnAuthenticationFailed)}"); | ||
} | ||
|
||
static async Task OnRedirectToIdentityProviderForSignOut(RedirectContext context) | ||
{ | ||
Debug.WriteLine($"10. Begin {nameof(OnRedirectToIdentityProviderForSignOut)}"); | ||
await onRedirectToIdentityProviderForSignOut(context); | ||
Debug.WriteLine($"10. End - {nameof(OnRedirectToIdentityProviderForSignOut)}"); | ||
} | ||
static async Task OnRemoteSignOut(RemoteSignOutContext context) | ||
{ | ||
Debug.WriteLine($"11. Begin {nameof(OnRemoteSignOut)}"); | ||
await onRemoteSignOut(context); | ||
Debug.WriteLine($"11. End - {nameof(OnRemoteSignOut)}"); | ||
} | ||
static async Task OnSignedOutCallbackRedirect(RemoteSignOutContext context) | ||
{ | ||
Debug.WriteLine($"12. Begin {nameof(OnSignedOutCallbackRedirect)}"); | ||
await onSignedOutCallbackRedirect(context); | ||
Debug.WriteLine($"12. End {nameof(OnSignedOutCallbackRedirect)}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you could tap into the asp.net core logging https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-2.2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, @bgavrilMS this would be better.