public void DefaultUniqueClaimTypes_NotPresent_SerializesAllClaimTypes() { var identity = new MockClaimsIdentity(); identity.AddClaim(ClaimTypes.Email, "*****@*****.**"); identity.AddClaim(ClaimTypes.GivenName, "some"); identity.AddClaim(ClaimTypes.Surname, "one"); identity.AddClaim(ClaimTypes.NameIdentifier, String.Empty); // Arrange var claimsIdentity = (ClaimsIdentity)identity; // Act var identiferParameters = DefaultClaimUidExtractor.GetUniqueIdentifierParameters(claimsIdentity) .ToArray(); var claims = claimsIdentity.Claims.ToList(); claims.Sort((a, b) => string.Compare(a.Type, b.Type, StringComparison.Ordinal)); // Assert int index = 0; foreach (var claim in claims) { Assert.True(String.Equals(identiferParameters[index++], claim.Type, StringComparison.Ordinal)); Assert.True(String.Equals(identiferParameters[index++], claim.Value, StringComparison.Ordinal)); } }
public void GetUniqueIdentifierParameters_NoKnownClaimTypesFound_SortsAndReturnsAllClaimsFromAuthenticatedIdentities() { // Arrange var identity1 = new ClaimsIdentity(); // no authentication identity1.AddClaim(new Claim("sub", "subClaimValue")); var identity2 = new ClaimsIdentity("someAuthentication"); identity2.AddClaim(new Claim(ClaimTypes.Email, "*****@*****.**")); var identity3 = new ClaimsIdentity("someAuthentication"); identity3.AddClaim(new Claim(ClaimTypes.Country, "countryValue")); var identity4 = new ClaimsIdentity("someAuthentication"); identity4.AddClaim(new Claim(ClaimTypes.Name, "claimName")); // Act var uniqueIdentifierParameters = DefaultClaimUidExtractor.GetUniqueIdentifierParameters( new ClaimsIdentity[] { identity1, identity2, identity3, identity4 }); // Assert Assert.Equal(new List <string> { ClaimTypes.Country, "countryValue", "LOCAL AUTHORITY", ClaimTypes.Email, "*****@*****.**", "LOCAL AUTHORITY", ClaimTypes.Name, "claimName", "LOCAL AUTHORITY", }, uniqueIdentifierParameters); }
public void ExtractClaimUid_NullIdentity() { // Arrange IClaimUidExtractor extractor = new DefaultClaimUidExtractor(); // Act var claimUid = extractor.ExtractClaimUid(null); // Assert Assert.Null(claimUid); }
public void ExtractClaimUid_Unauthenticated() { // Arrange IClaimUidExtractor extractor = new DefaultClaimUidExtractor(); var mockIdentity = new Mock<ClaimsIdentity>(); mockIdentity.Setup(o => o.IsAuthenticated) .Returns(false); // Act var claimUid = extractor.ExtractClaimUid(mockIdentity.Object); // Assert Assert.Null(claimUid); }
public void ExtractClaimUid_Unauthenticated() { // Arrange IClaimUidExtractor extractor = new DefaultClaimUidExtractor(); var mockIdentity = new Mock <ClaimsIdentity>(); mockIdentity.Setup(o => o.IsAuthenticated) .Returns(false); // Act var claimUid = extractor.ExtractClaimUid(mockIdentity.Object); // Assert Assert.Null(claimUid); }
public void ExtractClaimUid_ClaimsIdentity() { // Arrange var mockIdentity = new Mock<ClaimsIdentity>(); mockIdentity.Setup(o => o.IsAuthenticated) .Returns(true); IClaimUidExtractor extractor = new DefaultClaimUidExtractor(); // Act var claimUid = extractor.ExtractClaimUid(mockIdentity.Object); // Assert Assert.NotNull(claimUid); Assert.Equal("47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=", claimUid); }
public void ExtractClaimUid_ClaimsIdentity() { // Arrange var mockIdentity = new Mock <ClaimsIdentity>(); mockIdentity.Setup(o => o.IsAuthenticated) .Returns(true); IClaimUidExtractor extractor = new DefaultClaimUidExtractor(); // Act var claimUid = extractor.ExtractClaimUid(mockIdentity.Object); // Assert Assert.NotNull(claimUid); Assert.Equal("47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=", claimUid); }
public void DefaultUniqueClaimTypes_Present() { // Arrange var identity = new MockClaimsIdentity(); identity.AddClaim("fooClaim", "fooClaimValue"); identity.AddClaim(ClaimTypes.NameIdentifier, "nameIdentifierValue"); // Act var uniqueIdentifierParameters = DefaultClaimUidExtractor.GetUniqueIdentifierParameters(identity); // Assert Assert.Equal(new string[] { ClaimTypes.NameIdentifier, "nameIdentifierValue", }, uniqueIdentifierParameters); }
public void ExtractClaimUid_ClaimsIdentity() { // Arrange var mockIdentity = new Mock <ClaimsIdentity>(); mockIdentity.Setup(o => o.IsAuthenticated) .Returns(true); mockIdentity.Setup(o => o.Claims).Returns(new Claim[] { new Claim(ClaimTypes.Name, "someName") }); var extractor = new DefaultClaimUidExtractor(_pool); // Act var claimUid = extractor.ExtractClaimUid(new ClaimsPrincipal(mockIdentity.Object)); // Assert Assert.NotNull(claimUid); Assert.Equal("yhXE+2v4zSXHtRHmzm4cmrhZca2J0g7yTUwtUerdeF4=", claimUid); }
public void DefaultUniqueClaimTypes_Present() { // Arrange var identity = new ClaimsIdentity("someAuthentication"); identity.AddClaim(new Claim("fooClaim", "fooClaimValue")); identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, "nameIdentifierValue")); // Act var uniqueIdentifierParameters = DefaultClaimUidExtractor.GetUniqueIdentifierParameters(new ClaimsIdentity[] { identity }); // Assert Assert.Equal(new string[] { ClaimTypes.NameIdentifier, "nameIdentifierValue", "LOCAL AUTHORITY", }, uniqueIdentifierParameters); }
public void GetUniqueIdentifierParameters_PrefersSubClaimOverNameIdentifierAndUpn() { // Arrange var identity = new ClaimsIdentity("someAuthentication"); identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, "nameIdentifierValue")); identity.AddClaim(new Claim("sub", "subClaimValue")); identity.AddClaim(new Claim(ClaimTypes.Upn, "upnClaimValue")); // Act var uniqueIdentifierParameters = DefaultClaimUidExtractor.GetUniqueIdentifierParameters(new ClaimsIdentity[] { identity }); // Assert Assert.Equal(new string[] { "sub", "subClaimValue", "LOCAL AUTHORITY", }, uniqueIdentifierParameters); }
public void GetUniqueIdentifierParameters_MultipleIdentities_UsesOnlyAuthenticatedIdentities() { // Arrange var identity1 = new ClaimsIdentity(); // no authentication identity1.AddClaim(new Claim("sub", "subClaimValue")); var identity2 = new ClaimsIdentity("someAuthentication"); identity2.AddClaim(new Claim(ClaimTypes.NameIdentifier, "nameIdentifierValue")); // Act var uniqueIdentifierParameters = DefaultClaimUidExtractor.GetUniqueIdentifierParameters(new ClaimsIdentity[] { identity1, identity2 }); // Assert Assert.Equal(new string[] { ClaimTypes.NameIdentifier, "nameIdentifierValue", "LOCAL AUTHORITY", }, uniqueIdentifierParameters); }
public void GetUniqueIdentifierParameters_PrefersUpnFromFirstIdentity_OverNameFromSecondIdentity() { // Arrange var identity1 = new ClaimsIdentity("someAuthentication"); identity1.AddClaim(new Claim(ClaimTypes.Upn, "upnValue")); var identity2 = new ClaimsIdentity("someAuthentication"); identity2.AddClaim(new Claim(ClaimTypes.NameIdentifier, "nameIdentifierValue")); // Act var uniqueIdentifierParameters = DefaultClaimUidExtractor.GetUniqueIdentifierParameters( new ClaimsIdentity[] { identity1, identity2 }); // Assert Assert.Equal(new string[] { ClaimTypes.Upn, "upnValue", "LOCAL AUTHORITY", }, uniqueIdentifierParameters); }
public void ExtractClaimUid_ClaimsIdentity() { // Arrange var mockIdentity = new Mock<ClaimsIdentity>(); mockIdentity.Setup(o => o.IsAuthenticated) .Returns(true); mockIdentity.Setup(o => o.Claims).Returns(new Claim[] { new Claim(ClaimTypes.Name, "someName") }); var extractor = new DefaultClaimUidExtractor(_pool); // Act var claimUid = extractor.ExtractClaimUid(new ClaimsPrincipal(mockIdentity.Object )); // Assert Assert.NotNull(claimUid); Assert.Equal("yhXE+2v4zSXHtRHmzm4cmrhZca2J0g7yTUwtUerdeF4=", claimUid); }