-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8153 from abpframework/maliming/AbpUserClaimsPrin…
…cipalFactory Refactor AbpUserClaimsPrincipalFactory.
- Loading branch information
Showing
10 changed files
with
223 additions
and
32 deletions.
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
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
21 changes: 0 additions & 21 deletions
21
framework/src/Volo.Abp.Security/Volo/Abp/Security/Claims/ClaimsIdentityExtensions.cs
This file was deleted.
Oops, something went wrong.
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
6 changes: 4 additions & 2 deletions
6
framework/test/Volo.Abp.Security.Tests/Volo/Abp/Security/AbpSecurityTestModule.cs
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
104 changes: 104 additions & 0 deletions
104
...k/test/Volo.Abp.Security.Tests/Volo/Abp/Security/Claims/AbpClaimsPrincipalFactory_Test.cs
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,104 @@ | ||
using System.Linq; | ||
using System.Security.Claims; | ||
using System.Security.Principal; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Shouldly; | ||
using Volo.Abp.Testing; | ||
using Xunit; | ||
|
||
namespace Volo.Abp.Security.Claims | ||
{ | ||
public class AbpClaimsPrincipalFactory_Test : AbpIntegratedTest<AbpSecurityTestModule> | ||
{ | ||
private readonly IAbpClaimsPrincipalFactory _abpClaimsPrincipalFactory; | ||
private static string TestAuthenticationType => "Identity.Application"; | ||
|
||
public AbpClaimsPrincipalFactory_Test() | ||
{ | ||
_abpClaimsPrincipalFactory = GetRequiredService<IAbpClaimsPrincipalFactory>(); | ||
|
||
} | ||
|
||
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) | ||
{ | ||
options.UseAutofac(); | ||
} | ||
|
||
protected override void AfterAddApplication(IServiceCollection services) | ||
{ | ||
services.AddTransient<TestAbpClaimsPrincipalContributor>(); | ||
services.AddTransient<Test2AbpClaimsPrincipalContributor>(); | ||
services.AddTransient<Test3AbpClaimsPrincipalContributor>(); | ||
} | ||
|
||
[Fact] | ||
public async Task CreateAsync() | ||
{ | ||
var claimsPrincipal = await _abpClaimsPrincipalFactory.CreateAsync(); | ||
claimsPrincipal.Claims.ShouldContain(x => x.Type == ClaimTypes.Email && x.Value == "[email protected]"); | ||
claimsPrincipal.Claims.ShouldNotContain(x => x.Type == ClaimTypes.Email && x.Value == "[email protected]"); | ||
claimsPrincipal.Claims.ShouldContain(x => x.Type == ClaimTypes.Version && x.Value == "2.0"); | ||
} | ||
|
||
[Fact] | ||
public async Task Create_With_Exists_ClaimsPrincipal() | ||
{ | ||
var claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(TestAuthenticationType, ClaimTypes.Name, ClaimTypes.Role)); | ||
claimsPrincipal.Identities.First().AddClaim(new Claim(ClaimTypes.Name, "123")); | ||
claimsPrincipal.Identities.First().AddClaim(new Claim(ClaimTypes.Role, "admin")); | ||
|
||
await _abpClaimsPrincipalFactory.CreateAsync(claimsPrincipal); | ||
claimsPrincipal.Claims.ShouldContain(x => x.Type == ClaimTypes.Name && x.Value == "123"); | ||
claimsPrincipal.Claims.ShouldContain(x => x.Type == ClaimTypes.Role && x.Value == "admin"); | ||
claimsPrincipal.Claims.ShouldContain(x => x.Type == ClaimTypes.Email && x.Value == "[email protected]"); | ||
claimsPrincipal.Claims.ShouldNotContain(x => x.Type == ClaimTypes.Email && x.Value == "[email protected]"); | ||
claimsPrincipal.Claims.ShouldContain(x => x.Type == ClaimTypes.Version && x.Value == "2.0"); | ||
} | ||
|
||
class TestAbpClaimsPrincipalContributor : IAbpClaimsPrincipalContributor | ||
{ | ||
public Task ContributeAsync(AbpClaimsPrincipalContributorContext context) | ||
{ | ||
var claimsIdentity = context.ClaimsPrincipal.Identities.FirstOrDefault(x => x.AuthenticationType == TestAuthenticationType) | ||
?? new ClaimsIdentity(TestAuthenticationType); | ||
|
||
claimsIdentity.AddOrReplace(new Claim(ClaimTypes.Email, "[email protected]")); | ||
|
||
context.ClaimsPrincipal.AddIdentityIfNotContains(claimsIdentity); | ||
|
||
return Task.CompletedTask; | ||
} | ||
} | ||
|
||
class Test2AbpClaimsPrincipalContributor : IAbpClaimsPrincipalContributor | ||
{ | ||
public Task ContributeAsync(AbpClaimsPrincipalContributorContext context) | ||
{ | ||
var claimsIdentity = context.ClaimsPrincipal.Identities.FirstOrDefault(x => x.AuthenticationType == TestAuthenticationType) | ||
?? new ClaimsIdentity(TestAuthenticationType); | ||
|
||
claimsIdentity.AddOrReplace(new Claim(ClaimTypes.Email, "[email protected]")); | ||
|
||
context.ClaimsPrincipal.AddIdentityIfNotContains(claimsIdentity); | ||
|
||
return Task.CompletedTask; | ||
} | ||
} | ||
|
||
class Test3AbpClaimsPrincipalContributor : IAbpClaimsPrincipalContributor | ||
{ | ||
public Task ContributeAsync(AbpClaimsPrincipalContributorContext context) | ||
{ | ||
var claimsIdentity = context.ClaimsPrincipal.Identities.FirstOrDefault(x => x.AuthenticationType == TestAuthenticationType) | ||
?? new ClaimsIdentity(TestAuthenticationType); | ||
|
||
claimsIdentity.AddOrReplace(new Claim(ClaimTypes.Version, "2.0")); | ||
|
||
context.ClaimsPrincipal.AddIdentityIfNotContains(claimsIdentity); | ||
|
||
return Task.CompletedTask; | ||
} | ||
} | ||
} | ||
} |
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
64 changes: 64 additions & 0 deletions
64
...t/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/AbpUserClaimsPrincipalFactory_Tests.cs
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,64 @@ | ||
using System.Linq; | ||
using System.Security.Claims; | ||
using System.Security.Principal; | ||
using System.Threading.Tasks; | ||
using Shouldly; | ||
using Volo.Abp.DependencyInjection; | ||
using Volo.Abp.Security.Claims; | ||
using Xunit; | ||
|
||
namespace Volo.Abp.Identity | ||
{ | ||
public class AbpUserClaimsPrincipalFactory_Tests : AbpIdentityDomainTestBase | ||
{ | ||
private readonly IdentityUserManager _identityUserManager; | ||
private readonly AbpUserClaimsPrincipalFactory _abpUserClaimsPrincipalFactory; | ||
private readonly IdentityTestData _testData; | ||
|
||
public AbpUserClaimsPrincipalFactory_Tests() | ||
{ | ||
_identityUserManager = GetRequiredService<IdentityUserManager>(); | ||
_abpUserClaimsPrincipalFactory = GetRequiredService<AbpUserClaimsPrincipalFactory>(); | ||
_testData = GetRequiredService<IdentityTestData>(); | ||
} | ||
|
||
[Fact] | ||
public async Task Add_And_Replace_Claims_Test() | ||
{ | ||
await UsingUowAsync(async () => | ||
{ | ||
var user = await _identityUserManager.GetByIdAsync(_testData.UserJohnId); | ||
user.ShouldNotBeNull(); | ||
|
||
var claimsPrincipal = await _abpUserClaimsPrincipalFactory.CreateAsync(user); | ||
|
||
claimsPrincipal.Claims.ShouldContain(x => x.Type == ClaimTypes.NameIdentifier && x.Value == user.Id.ToString()); | ||
claimsPrincipal.Claims.ShouldContain(x => x.Type == ClaimTypes.Name && x.Value == user.UserName); | ||
|
||
claimsPrincipal.Claims.ShouldContain(x => x.Type == ClaimTypes.Uri && x.Value =="www.abp.io"); | ||
|
||
claimsPrincipal.Claims.ShouldNotContain(x => x.Type == ClaimTypes.Email && x.Value == user.Email); | ||
claimsPrincipal.Claims.ShouldContain(x => x.Type == ClaimTypes.Email && x.Value == "[email protected]"); | ||
}); | ||
} | ||
|
||
class TestAbpClaimsPrincipalContributor : IAbpClaimsPrincipalContributor, ITransientDependency | ||
{ | ||
//https://github.com/dotnet/aspnetcore/blob/v5.0.0/src/Identity/Extensions.Core/src/UserClaimsPrincipalFactory.cs#L79 | ||
private static string IdentityAuthenticationType => "Identity.Application"; | ||
|
||
public Task ContributeAsync(AbpClaimsPrincipalContributorContext context) | ||
{ | ||
var claimsIdentity = context.ClaimsPrincipal.Identities.First(x => x.AuthenticationType == IdentityAuthenticationType); | ||
|
||
claimsIdentity.AddOrReplace(new Claim(ClaimTypes.Uri, "www.abp.io")); | ||
claimsIdentity.AddOrReplace(new Claim(ClaimTypes.Email, "[email protected]")); | ||
|
||
context.ClaimsPrincipal.AddIdentityIfNotContains(claimsIdentity); | ||
|
||
return Task.CompletedTask; | ||
} | ||
} | ||
|
||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
....Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AspNetIdentity/AbpUserClaimsFactory.cs
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