From 38fb911afc8f44fdbebb63efa36e8ffdc7e0a213 Mon Sep 17 00:00:00 2001 From: Chris Ross Date: Wed, 14 Jan 2015 15:13:24 -0800 Subject: [PATCH] Clean up auth types, copywrite headers, file names, exceptions. --- samples/OpenIdConnectSample/Startup.cs | 10 ++++---- .../OAuthBearerAuthenticationHandler.cs | 23 ++++++++----------- .../{NonceCache.cs => INonceCache.cs} | 3 ++- .../AuthorizationCodeReceivedNotification.cs | 3 ++- .../OpenIdConnectAuthenticationDefaults.cs | 4 ++-- .../OpenIdConnectAuthenticationExtensions.cs | 3 ++- .../OpenIdConnectAuthenticationMiddleware.cs | 3 ++- ...penIdConnectAuthenticationNotifications.cs | 3 ++- .../OpenIdConnectAuthenticationOptions.cs | 3 ++- .../OpenidConnectAuthenticationHandler.cs | 23 ++++++------------- .../OAuthBearer/OAuthBearerMiddlewareTests.cs | 3 ++- .../OpenIdConnectMiddlewareTests.cs | 3 ++- 12 files changed, 38 insertions(+), 46 deletions(-) rename src/Microsoft.AspNet.Security.OpenIdConnect/{NonceCache.cs => INonceCache.cs} (69%) rename test/Microsoft.AspNet.Security.Test/{OpenIdConnectMiddlewareTests => OpenIdConnect}/OpenIdConnectMiddlewareTests.cs (99%) diff --git a/samples/OpenIdConnectSample/Startup.cs b/samples/OpenIdConnectSample/Startup.cs index 17850d03b2..bd443424ef 100644 --- a/samples/OpenIdConnectSample/Startup.cs +++ b/samples/OpenIdConnectSample/Startup.cs @@ -1,9 +1,10 @@ using Microsoft.AspNet.Builder; using Microsoft.AspNet.Http; -using Microsoft.Framework.DependencyInjection; -using Microsoft.AspNet.Security.OpenIdConnect; using Microsoft.AspNet.Http.Security; using Microsoft.AspNet.Security; +using Microsoft.AspNet.Security.Cookies; +using Microsoft.AspNet.Security.OpenIdConnect; +using Microsoft.Framework.DependencyInjection; namespace OpenIdConnectSample { @@ -16,14 +17,13 @@ namespace OpenIdConnectSample services.AddDataProtection(); services.Configure(options => { - options.SignInAsAuthenticationType = OpenIdConnectAuthenticationDefaults.AuthenticationType; + options.SignInAsAuthenticationType = CookieAuthenticationDefaults.AuthenticationType; }); }); app.UseCookieAuthentication(options => { - options.AuthenticationType = OpenIdConnectAuthenticationDefaults.AuthenticationType; }); app.UseOpenIdConnectAuthentication(options => @@ -31,8 +31,6 @@ namespace OpenIdConnectSample options.ClientId = "fe78e0b4-6fe7-47e6-812c-fb75cee266a4"; options.Authority = "https://login.windows.net/cyrano.onmicrosoft.com"; options.RedirectUri = "http://localhost:42023"; - options.SignInAsAuthenticationType = OpenIdConnectAuthenticationDefaults.AuthenticationType; - options.AuthenticationType = OpenIdConnectAuthenticationDefaults.AuthenticationType; }); app.Run(async context => diff --git a/src/Microsoft.AspNet.Security.OAuthBearer/OAuthBearerAuthenticationHandler.cs b/src/Microsoft.AspNet.Security.OAuthBearer/OAuthBearerAuthenticationHandler.cs index 1c2eb287ae..b81af0c735 100644 --- a/src/Microsoft.AspNet.Security.OAuthBearer/OAuthBearerAuthenticationHandler.cs +++ b/src/Microsoft.AspNet.Security.OAuthBearer/OAuthBearerAuthenticationHandler.cs @@ -5,7 +5,6 @@ using System; using System.Collections.Generic; using System.IdentityModel.Tokens; using System.Linq; -using System.Runtime.ExceptionServices; using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNet.Http; @@ -38,7 +37,6 @@ namespace Microsoft.AspNet.Security.OAuthBearer /// protected override async Task AuthenticateCoreAsync() { - ExceptionDispatchInfo authFailedEx = null; string token = null; try { @@ -144,16 +142,10 @@ namespace Microsoft.AspNet.Security.OAuthBearer } catch (Exception ex) { - // We can't await inside a catch block, capture and handle outside. - authFailedEx = ExceptionDispatchInfo.Capture(ex); - } - - if (authFailedEx != null) - { - _logger.WriteError("Exception occurred while processing message", authFailedEx.SourceException); + _logger.WriteError("Exception occurred while processing message", ex); // Refresh the configuration for exceptions that may be caused by key rollovers. The user can also request a refresh in the notification. - if (Options.RefreshOnIssuerKeyNotFound && authFailedEx.SourceException.GetType().Equals(typeof(SecurityTokenSignatureKeyNotFoundException))) + if (Options.RefreshOnIssuerKeyNotFound && ex.GetType().Equals(typeof(SecurityTokenSignatureKeyNotFoundException))) { Options.ConfigurationManager.RequestRefresh(); } @@ -162,7 +154,7 @@ namespace Microsoft.AspNet.Security.OAuthBearer new AuthenticationFailedNotification(Context, Options) { ProtocolMessage = Context, - Exception = authFailedEx.SourceException + Exception = ex }; await Options.Notifications.AuthenticationFailed(authenticationFailedNotification); @@ -176,10 +168,8 @@ namespace Microsoft.AspNet.Security.OAuthBearer return null; } - authFailedEx.Throw(); + throw; } - - return null; } protected override void ApplyResponseChallenge() @@ -189,6 +179,11 @@ namespace Microsoft.AspNet.Security.OAuthBearer protected override async Task ApplyResponseChallengeAsync() { + if ((Response.StatusCode != 401) || (ChallengeContext == null)) + { + return; + } + await Options.Notifications.ApplyChallenge(new AuthenticationChallengeNotification(Context, Options)); } diff --git a/src/Microsoft.AspNet.Security.OpenIdConnect/NonceCache.cs b/src/Microsoft.AspNet.Security.OpenIdConnect/INonceCache.cs similarity index 69% rename from src/Microsoft.AspNet.Security.OpenIdConnect/NonceCache.cs rename to src/Microsoft.AspNet.Security.OpenIdConnect/INonceCache.cs index 11c09b3457..3f5255f56d 100644 --- a/src/Microsoft.AspNet.Security.OpenIdConnect/NonceCache.cs +++ b/src/Microsoft.AspNet.Security.OpenIdConnect/INonceCache.cs @@ -1,4 +1,5 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. namespace Microsoft.AspNet.Security.OpenIdConnect { diff --git a/src/Microsoft.AspNet.Security.OpenIdConnect/Notifications/AuthorizationCodeReceivedNotification.cs b/src/Microsoft.AspNet.Security.OpenIdConnect/Notifications/AuthorizationCodeReceivedNotification.cs index cbcdc06183..9c7694e560 100644 --- a/src/Microsoft.AspNet.Security.OpenIdConnect/Notifications/AuthorizationCodeReceivedNotification.cs +++ b/src/Microsoft.AspNet.Security.OpenIdConnect/Notifications/AuthorizationCodeReceivedNotification.cs @@ -1,4 +1,5 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using Microsoft.AspNet.Http; using Microsoft.AspNet.Security.OpenIdConnect; diff --git a/src/Microsoft.AspNet.Security.OpenIdConnect/OpenIdConnectAuthenticationDefaults.cs b/src/Microsoft.AspNet.Security.OpenIdConnect/OpenIdConnectAuthenticationDefaults.cs index d6271488a9..5942efb4dc 100644 --- a/src/Microsoft.AspNet.Security.OpenIdConnect/OpenIdConnectAuthenticationDefaults.cs +++ b/src/Microsoft.AspNet.Security.OpenIdConnect/OpenIdConnectAuthenticationDefaults.cs @@ -1,5 +1,5 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. - +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. namespace Microsoft.AspNet.Security.OpenIdConnect { diff --git a/src/Microsoft.AspNet.Security.OpenIdConnect/OpenIdConnectAuthenticationExtensions.cs b/src/Microsoft.AspNet.Security.OpenIdConnect/OpenIdConnectAuthenticationExtensions.cs index 81a74b65ca..5d4a72b217 100644 --- a/src/Microsoft.AspNet.Security.OpenIdConnect/OpenIdConnectAuthenticationExtensions.cs +++ b/src/Microsoft.AspNet.Security.OpenIdConnect/OpenIdConnectAuthenticationExtensions.cs @@ -1,4 +1,5 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using Microsoft.AspNet.Security.OpenIdConnect; diff --git a/src/Microsoft.AspNet.Security.OpenIdConnect/OpenIdConnectAuthenticationMiddleware.cs b/src/Microsoft.AspNet.Security.OpenIdConnect/OpenIdConnectAuthenticationMiddleware.cs index bfe82b51f0..bbe59f50d2 100644 --- a/src/Microsoft.AspNet.Security.OpenIdConnect/OpenIdConnectAuthenticationMiddleware.cs +++ b/src/Microsoft.AspNet.Security.OpenIdConnect/OpenIdConnectAuthenticationMiddleware.cs @@ -1,4 +1,5 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.ObjectModel; diff --git a/src/Microsoft.AspNet.Security.OpenIdConnect/OpenIdConnectAuthenticationNotifications.cs b/src/Microsoft.AspNet.Security.OpenIdConnect/OpenIdConnectAuthenticationNotifications.cs index 36ee2fd25a..327bbfa599 100644 --- a/src/Microsoft.AspNet.Security.OpenIdConnect/OpenIdConnectAuthenticationNotifications.cs +++ b/src/Microsoft.AspNet.Security.OpenIdConnect/OpenIdConnectAuthenticationNotifications.cs @@ -1,4 +1,5 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Threading.Tasks; diff --git a/src/Microsoft.AspNet.Security.OpenIdConnect/OpenIdConnectAuthenticationOptions.cs b/src/Microsoft.AspNet.Security.OpenIdConnect/OpenIdConnectAuthenticationOptions.cs index 5022cbf3c4..5f1b5077ec 100644 --- a/src/Microsoft.AspNet.Security.OpenIdConnect/OpenIdConnectAuthenticationOptions.cs +++ b/src/Microsoft.AspNet.Security.OpenIdConnect/OpenIdConnectAuthenticationOptions.cs @@ -1,4 +1,5 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; diff --git a/src/Microsoft.AspNet.Security.OpenIdConnect/OpenidConnectAuthenticationHandler.cs b/src/Microsoft.AspNet.Security.OpenIdConnect/OpenidConnectAuthenticationHandler.cs index 5305039106..317cd42e8b 100644 --- a/src/Microsoft.AspNet.Security.OpenIdConnect/OpenidConnectAuthenticationHandler.cs +++ b/src/Microsoft.AspNet.Security.OpenIdConnect/OpenidConnectAuthenticationHandler.cs @@ -1,11 +1,11 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Globalization; using System.IdentityModel.Tokens; using System.IO; using System.Linq; -using System.Runtime.ExceptionServices; using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNet.Http; @@ -122,7 +122,7 @@ namespace Microsoft.AspNet.Security.OpenIdConnect } // order for redirect_uri - // 1. challenge.Properties.RedirectUri + // 1. challenge.Properties.RedirectUri // 2. CurrentUri AuthenticationProperties properties = new AuthenticationProperties(ChallengeContext.Properties); if (string.IsNullOrEmpty(properties.RedirectUri)) @@ -224,7 +224,6 @@ namespace Microsoft.AspNet.Security.OpenIdConnect return null; } - ExceptionDispatchInfo authFailedEx = null; try { var messageReceivedNotification = new MessageReceivedNotification(Context, Options) @@ -410,16 +409,10 @@ namespace Microsoft.AspNet.Security.OpenIdConnect } catch (Exception exception) { - // We can't await inside a catch block, capture and handle outside. - authFailedEx = ExceptionDispatchInfo.Capture(exception); - } - - if (authFailedEx != null) - { - _logger.WriteError("Exception occurred while processing message", authFailedEx.SourceException); + _logger.WriteError("Exception occurred while processing message", exception); // Refresh the configuration for exceptions that may be caused by key rollovers. The user can also request a refresh in the notification. - if (Options.RefreshOnIssuerKeyNotFound && authFailedEx.SourceException.GetType().Equals(typeof(SecurityTokenSignatureKeyNotFoundException))) + if (Options.RefreshOnIssuerKeyNotFound && exception.GetType().Equals(typeof(SecurityTokenSignatureKeyNotFoundException))) { Options.ConfigurationManager.RequestRefresh(); } @@ -427,7 +420,7 @@ namespace Microsoft.AspNet.Security.OpenIdConnect var authenticationFailedNotification = new AuthenticationFailedNotification(Context, Options) { ProtocolMessage = openIdConnectMessage, - Exception = authFailedEx.SourceException + Exception = exception }; await Options.Notifications.AuthenticationFailed(authenticationFailedNotification); @@ -441,10 +434,8 @@ namespace Microsoft.AspNet.Security.OpenIdConnect return null; } - authFailedEx.Throw(); + throw; } - - return null; } /// diff --git a/test/Microsoft.AspNet.Security.Test/OAuthBearer/OAuthBearerMiddlewareTests.cs b/test/Microsoft.AspNet.Security.Test/OAuthBearer/OAuthBearerMiddlewareTests.cs index 6fa8b7afd4..eff8e28841 100644 --- a/test/Microsoft.AspNet.Security.Test/OAuthBearer/OAuthBearerMiddlewareTests.cs +++ b/test/Microsoft.AspNet.Security.Test/OAuthBearer/OAuthBearerMiddlewareTests.cs @@ -1,4 +1,5 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; diff --git a/test/Microsoft.AspNet.Security.Test/OpenIdConnectMiddlewareTests/OpenIdConnectMiddlewareTests.cs b/test/Microsoft.AspNet.Security.Test/OpenIdConnect/OpenIdConnectMiddlewareTests.cs similarity index 99% rename from test/Microsoft.AspNet.Security.Test/OpenIdConnectMiddlewareTests/OpenIdConnectMiddlewareTests.cs rename to test/Microsoft.AspNet.Security.Test/OpenIdConnect/OpenIdConnectMiddlewareTests.cs index 652c295ec1..e3a5df4718 100644 --- a/test/Microsoft.AspNet.Security.Test/OpenIdConnectMiddlewareTests/OpenIdConnectMiddlewareTests.cs +++ b/test/Microsoft.AspNet.Security.Test/OpenIdConnect/OpenIdConnectMiddlewareTests.cs @@ -1,4 +1,5 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic;