Browse Source

Handle multiple roles.

pull/217/head
Halil İbrahim Kalkan 9 years ago
parent
commit
9bf60d41e0
  1. 7
      Volo.Abp.sln
  2. 12
      src/Volo.Abp.AspNetCore.Authentication.OAuth/Microsoft/AspNetCore/Authentication/OAuth/Claims/AbpClaimActionCollectionExtensions.cs
  3. 40
      src/Volo.Abp.AspNetCore.Authentication.OAuth/Volo/Abp/AspNetCore/Authentication/OAuth/Claims/MultipleClaimAction.cs
  4. 24
      test/Volo.Abp.AspNetCore.Authentication.OAuth.Tests/Volo.Abp.AspNetCore.Authentication.OAuth.Tests.csproj
  5. 46
      test/Volo.Abp.AspNetCore.Authentication.OAuth.Tests/Volo/Abp/AspNetCore/Authentication/OAuth/Claims/Class1.cs

7
Volo.Abp.sln

@ -304,6 +304,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MicroserviceDemo.ConsoleCli
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.AspNetCore.Authentication.OAuth", "src\Volo.Abp.AspNetCore.Authentication.OAuth\Volo.Abp.AspNetCore.Authentication.OAuth.csproj", "{A1C792B7-0DBF-460D-9158-A1A68A2D9C1A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.AspNetCore.Authentication.OAuth.Tests", "test\Volo.Abp.AspNetCore.Authentication.OAuth.Tests\Volo.Abp.AspNetCore.Authentication.OAuth.Tests.csproj", "{627B88DB-BDCF-4D92-8454-EFE95F4AFB7A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -802,6 +804,10 @@ Global
{A1C792B7-0DBF-460D-9158-A1A68A2D9C1A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A1C792B7-0DBF-460D-9158-A1A68A2D9C1A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A1C792B7-0DBF-460D-9158-A1A68A2D9C1A}.Release|Any CPU.Build.0 = Release|Any CPU
{627B88DB-BDCF-4D92-8454-EFE95F4AFB7A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{627B88DB-BDCF-4D92-8454-EFE95F4AFB7A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{627B88DB-BDCF-4D92-8454-EFE95F4AFB7A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{627B88DB-BDCF-4D92-8454-EFE95F4AFB7A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -950,6 +956,7 @@ Global
{E4AB8A4F-BB59-4BDB-B915-877CE97D8113} = {DB012309-74FD-4D5A-B843-DD77BF053BF4}
{CD4E755D-D47C-45B1-AFB3-3444FF2E2E39} = {3510E248-DC9F-4A07-8134-02E7F5CC5783}
{A1C792B7-0DBF-460D-9158-A1A68A2D9C1A} = {C4C6961D-01CC-49A5-8B96-2A36E71CF01F}
{627B88DB-BDCF-4D92-8454-EFE95F4AFB7A} = {37087D1B-3693-4E96-983D-A69F210BDE53}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BB97ECF4-9A84-433F-A80B-2A3285BDD1D5}

12
src/Volo.Abp.AspNetCore.Authentication.OAuth/Microsoft/AspNetCore/Authentication/OAuth/Claims/AbpClaimActionCollectionExtensions.cs

@ -1,4 +1,5 @@
using Volo.Abp.Security.Claims;
using Volo.Abp.AspNetCore.Authentication.OAuth.Claims;
using Volo.Abp.Security.Claims;
namespace Microsoft.AspNetCore.Authentication.OAuth.Claims
{
@ -6,14 +7,21 @@ namespace Microsoft.AspNetCore.Authentication.OAuth.Claims
{
public static void MapAbpClaimTypes(this ClaimActionCollection claimActions)
{
claimActions.MapJsonKey(AbpClaimTypes.Role, "role");
claimActions.MapJsonKey(AbpClaimTypes.Email, "email");
claimActions.MapJsonKey(AbpClaimTypes.UserName, "name");
claimActions.MapJsonKey(AbpClaimTypes.EmailVerified, "email_verified");
claimActions.MapJsonKey(AbpClaimTypes.PhoneNumber, "phone_number");
claimActions.MapJsonKey(AbpClaimTypes.PhoneNumberVerified, "phone_number_verified");
claimActions.MapJsonKeyMultiple(AbpClaimTypes.Role, "role");
claimActions.DeleteClaim("name");
claimActions.DeleteClaim("email");
}
public static void MapJsonKeyMultiple(this ClaimActionCollection claimActions, string claimType, string jsonKey)
{
claimActions.Add(new MultipleClaimAction(claimType, jsonKey));
}
}
}

40
src/Volo.Abp.AspNetCore.Authentication.OAuth/Volo/Abp/AspNetCore/Authentication/OAuth/Claims/MultipleClaimAction.cs

@ -0,0 +1,40 @@
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication.OAuth.Claims;
using Newtonsoft.Json.Linq;
namespace Volo.Abp.AspNetCore.Authentication.OAuth.Claims
{
public class MultipleClaimAction : ClaimAction
{
public MultipleClaimAction(string claimType, string jsonKey)
: base(claimType, jsonKey)
{
}
public override void Run(JObject userData, ClaimsIdentity identity, string issuer)
{
var prop = userData?.Property(ValueType);
if (prop == null)
{
return;
}
var propValue = prop.Value;
switch (propValue.Type)
{
case JTokenType.String:
identity.AddClaim(new Claim(ClaimType, propValue.Value<string>(), ValueType, issuer));
break;
case JTokenType.Array:
foreach (var innterValue in propValue.Values<string>())
{
identity.AddClaim(new Claim(ClaimType, innterValue, ValueType, issuer));
}
break;
default:
throw new AbpException("Unhandled JTokenType: " + propValue.Type);
}
}
}
}

24
test/Volo.Abp.AspNetCore.Authentication.OAuth.Tests/Volo.Abp.AspNetCore.Authentication.OAuth.Tests.csproj

@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<AssemblyName>Volo.Abp.AspNetCore.Authentication.OAuth.Tests</AssemblyName>
<PackageId>Volo.Abp.AspNetCore.Authentication.OAuth.Tests</PackageId>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<RootNamespace />
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\AbpTestBase\AbpTestBase.csproj" />
<ProjectReference Include="..\..\src\Volo.Abp.AspNetCore.Authentication.OAuth\Volo.Abp.AspNetCore.Authentication.OAuth.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
</ItemGroup>
</Project>

46
test/Volo.Abp.AspNetCore.Authentication.OAuth.Tests/Volo/Abp/AspNetCore/Authentication/OAuth/Claims/Class1.cs

@ -0,0 +1,46 @@
using System.Linq;
using System.Security.Claims;
using Newtonsoft.Json.Linq;
using Shouldly;
using Volo.Abp.Security.Claims;
using Xunit;
namespace Volo.Abp.AspNetCore.Authentication.OAuth.Claims
{
public class MultipleClaimAction_Tests
{
[Fact]
public void Should_Set_Single_Value()
{
var jObject = JObject.Parse(@"{
""sub"": ""71054539-0e48-af28-5e7a-39e4e42d8ea5"",
""role"": ""admin""
}");
var claimsIdentity = new ClaimsIdentity();
new MultipleClaimAction(AbpClaimTypes.Role, "role").Run(jObject, claimsIdentity, null);
var claims = claimsIdentity.FindAll(AbpClaimTypes.Role).ToList();
claims.Count.ShouldBe(1);
claims[0].Value.ShouldBe("admin");
}
[Fact]
public void Should_Set_Multiple_Values()
{
var jObject = JObject.Parse(@"{
""sub"": ""71054539-0e48-af28-5e7a-39e4e42d8ea5"",
""role"": [
""admin"",
""moderator""
]
}");
var claimsIdentity = new ClaimsIdentity();
new MultipleClaimAction(AbpClaimTypes.Role, "role").Run(jObject, claimsIdentity, null);
var claims = claimsIdentity.FindAll(AbpClaimTypes.Role).ToList();
claims.Count.ShouldBe(2);
claims[0].Value.ShouldBe("admin");
claims[1].Value.ShouldBe("moderator");
}
}
}
Loading…
Cancel
Save