Skip to content
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

[BugBash] MSA - Add exception handler for access denied #5485

Merged
merged 5 commits into from
Feb 16, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

using System;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Web.Mvc;
using Microsoft.IdentityModel.Protocols;
using Microsoft.Owin.Security.Notifications;
using Microsoft.Owin.Security.OpenIdConnect;
using NuGetGallery.Configuration;
using Owin;
Expand Down Expand Up @@ -33,6 +35,8 @@ public static class AuthenticationType
public static readonly string V2CommonTenant = "common";
public static readonly string Authority = "https://login.microsoftonline.com/{0}/v2.0";

private static readonly string ACCESS_DENIED = "access_denied";

protected override void AttachToOwinApp(IGalleryConfigurationService config, IAppBuilder app)
{
// Fetch site root from configuration
Expand All @@ -52,13 +56,17 @@ protected override void AttachToOwinApp(IGalleryConfigurationService config, IAp
Scope = OpenIdConnectScopes.OpenIdProfile + " email",
ResponseType = OpenIdConnectResponseTypes.CodeIdToken,
TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters() { ValidateIssuer = false },
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthenticationFailed = AuthenticationFailed
}
};

Config.ApplyToOwinSecurityOptions(options);

app.UseOpenIdConnectAuthentication(options);
}

public override AuthenticatorUI GetUI()
{
return new AuthenticatorUI(
Expand Down Expand Up @@ -136,5 +144,31 @@ public override IdentityInformation GetIdentityInformation(ClaimsIdentity claims

return new IdentityInformation(identifier, nameClaim?.Value, emailClaim.Value, authenticationType, tenantId);
}

// The OpenIdConnect.<AuthenticateCoreAsync> throws OpenIdConnectProtocolException upon denial of access permissions by the user,
// this could result in an internal server error, catch this exception and continue to the controller where appropriate
// error handling is done.
private Task AuthenticationFailed(AuthenticationFailedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> notification)
{
if (notification.Exception.Message == ACCESS_DENIED)
{
// For every 'Challenge' sent to the external providers, we store the 'State'
// with the redirect uri where we intend to return after successful authentication.
// Extract this "RedirectUri" property from this "State" object for redirecting on failed authentication as well.
var authenticationPropertiesEncodedString = notification
.ProtocolMessage
.State
.Split('=');
var authenticationProperties = notification
.Options
.StateDataFormat
.Unprotect(authenticationPropertiesEncodedString[1]);

notification.HandleResponse();
notification.Response.Redirect(authenticationProperties.RedirectUri);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this guaranteed to be set, or do we need to default to redirecting to homepage?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its guaranteed to be set, see

var properties = new AuthenticationProperties() { RedirectUri = RedirectUri };

}

return Task.FromResult(0);
}
}
}