From 01a4a273d7fcaa1714e249786ee6f88b004de73e Mon Sep 17 00:00:00 2001 From: Nate McMaster Date: Thu, 13 Dec 2018 11:26:34 -0800 Subject: [PATCH] Remove Microsoft.Extensions.SecurityHelper.Sources \n\nCommit migrated from https://github.com/dotnet/extensions/commit/c41abe44afd7890ae81abef88263a9c8a782f68b --- src/Shared/SecurityHelper/SecurityHelper.cs | 40 -------- .../test/Shared.Tests/SecurityHelperTests.cs | 93 ------------------- 2 files changed, 133 deletions(-) delete mode 100644 src/Shared/SecurityHelper/SecurityHelper.cs delete mode 100644 src/Shared/test/Shared.Tests/SecurityHelperTests.cs diff --git a/src/Shared/SecurityHelper/SecurityHelper.cs b/src/Shared/SecurityHelper/SecurityHelper.cs deleted file mode 100644 index 408ef6b224..0000000000 --- a/src/Shared/SecurityHelper/SecurityHelper.cs +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright (c) .NET Foundation. 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.Linq; -using System.Security.Claims; - -namespace Microsoft.Extensions.Internal -{ - /// - /// Helper code used when implementing authentication middleware - /// - internal static class SecurityHelper - { - /// - /// Add all ClaimsIdentities from an additional ClaimPrincipal to the ClaimsPrincipal - /// Merges a new claims principal, placing all new identities first, and eliminating - /// any empty unauthenticated identities from context.User - /// - /// The containing existing . - /// The containing to be added. - public static ClaimsPrincipal MergeUserPrincipal(ClaimsPrincipal existingPrincipal, ClaimsPrincipal additionalPrincipal) - { - var newPrincipal = new ClaimsPrincipal(); - - // New principal identities go first - if (additionalPrincipal != null) - { - newPrincipal.AddIdentities(additionalPrincipal.Identities); - } - - // Then add any existing non empty or authenticated identities - if (existingPrincipal != null) - { - newPrincipal.AddIdentities(existingPrincipal.Identities.Where(i => i.IsAuthenticated || i.Claims.Any())); - } - return newPrincipal; - } - } -} diff --git a/src/Shared/test/Shared.Tests/SecurityHelperTests.cs b/src/Shared/test/Shared.Tests/SecurityHelperTests.cs deleted file mode 100644 index 8e7515ad36..0000000000 --- a/src/Shared/test/Shared.Tests/SecurityHelperTests.cs +++ /dev/null @@ -1,93 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Linq; -using System.Security.Claims; -using System.Security.Principal; -using Xunit; - -namespace Microsoft.Extensions.Internal -{ - public class SecurityHelperTests - { - [Fact] - public void AddingToAnonymousIdentityDoesNotKeepAnonymousIdentity() - { - var user = SecurityHelper.MergeUserPrincipal(new ClaimsPrincipal(), new GenericPrincipal(new GenericIdentity("Test1", "Alpha"), new string[0])); - - Assert.NotNull(user); - Assert.Equal("Alpha", user.Identity.AuthenticationType); - Assert.Equal("Test1", user.Identity.Name); - Assert.IsAssignableFrom(user); - Assert.IsAssignableFrom(user.Identity); - Assert.Single(user.Identities); - } - - [Fact] - public void AddingExistingIdentityChangesDefaultButPreservesPrior() - { - ClaimsPrincipal user = new GenericPrincipal(new GenericIdentity("Test1", "Alpha"), null); - - Assert.Equal("Alpha", user.Identity.AuthenticationType); - Assert.Equal("Test1", user.Identity.Name); - - user = SecurityHelper.MergeUserPrincipal(user, new GenericPrincipal(new GenericIdentity("Test2", "Beta"), new string[0])); - - Assert.Equal("Beta", user.Identity.AuthenticationType); - Assert.Equal("Test2", user.Identity.Name); - - user = SecurityHelper.MergeUserPrincipal(user, new GenericPrincipal(new GenericIdentity("Test3", "Gamma"), new string[0])); - - Assert.Equal("Gamma", user.Identity.AuthenticationType); - Assert.Equal("Test3", user.Identity.Name); - - Assert.Equal(3, user.Identities.Count()); - Assert.Equal("Test3", user.Identities.Skip(0).First().Name); - Assert.Equal("Test2", user.Identities.Skip(1).First().Name); - Assert.Equal("Test1", user.Identities.Skip(2).First().Name); - } - - [Fact] - public void AddingPreservesNewIdentitiesAndDropsEmpty() - { - var existingPrincipal = new ClaimsPrincipal(new ClaimsIdentity()); - var identityNoAuthTypeWithClaim = new ClaimsIdentity(); - identityNoAuthTypeWithClaim.AddClaim(new Claim("identityNoAuthTypeWithClaim", "yes")); - existingPrincipal.AddIdentity(identityNoAuthTypeWithClaim); - var identityEmptyWithAuthType = new ClaimsIdentity("empty"); - existingPrincipal.AddIdentity(identityEmptyWithAuthType); - - Assert.False(existingPrincipal.Identity.IsAuthenticated); - - var newPrincipal = new ClaimsPrincipal(); - var newEmptyIdentity = new ClaimsIdentity(); - var identityTwo = new ClaimsIdentity("yep"); - newPrincipal.AddIdentity(newEmptyIdentity); - newPrincipal.AddIdentity(identityTwo); - - var user = SecurityHelper.MergeUserPrincipal(existingPrincipal, newPrincipal); - - // Preserve newPrincipal order - Assert.False(user.Identity.IsAuthenticated); - Assert.Null(user.Identity.Name); - - Assert.Equal(4, user.Identities.Count()); - Assert.Equal(newEmptyIdentity, user.Identities.Skip(0).First()); - Assert.Equal(identityTwo, user.Identities.Skip(1).First()); - Assert.Equal(identityNoAuthTypeWithClaim, user.Identities.Skip(2).First()); - Assert.Equal(identityEmptyWithAuthType, user.Identities.Skip(3).First()); - - // This merge should drop newEmptyIdentity since its empty - user = SecurityHelper.MergeUserPrincipal(user, new GenericPrincipal(new GenericIdentity("Test3", "Gamma"), new string[0])); - - Assert.Equal("Gamma", user.Identity.AuthenticationType); - Assert.Equal("Test3", user.Identity.Name); - - Assert.Equal(4, user.Identities.Count()); - Assert.Equal("Test3", user.Identities.Skip(0).First().Name); - Assert.Equal(identityTwo, user.Identities.Skip(1).First()); - Assert.Equal(identityNoAuthTypeWithClaim, user.Identities.Skip(2).First()); - Assert.Equal(identityEmptyWithAuthType, user.Identities.Skip(3).First()); - } - } -}