mirror of https://github.com/Squidex/squidex.git
11 changed files with 172 additions and 27 deletions
@ -0,0 +1,36 @@ |
|||
// ==========================================================================
|
|||
// UserClaimsPrincipalFactoryWithEmail.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Linq; |
|||
using System.Security.Claims; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Builder; |
|||
using Microsoft.AspNetCore.Identity; |
|||
using Microsoft.Extensions.Options; |
|||
using Squidex.Infrastructure.Security; |
|||
using Squidex.Shared.Users; |
|||
|
|||
namespace Squidex.Domain.Users |
|||
{ |
|||
public sealed class UserClaimsPrincipalFactoryWithEmail : UserClaimsPrincipalFactory<IUser, IRole> |
|||
{ |
|||
public UserClaimsPrincipalFactoryWithEmail(UserManager<IUser> userManager, RoleManager<IRole> roleManager, IOptions<IdentityOptions> optionsAccessor) |
|||
: base(userManager, roleManager, optionsAccessor) |
|||
{ |
|||
} |
|||
|
|||
public override async Task<ClaimsPrincipal> CreateAsync(IUser user) |
|||
{ |
|||
var principal = await base.CreateAsync(user); |
|||
|
|||
principal.Identities.First().AddClaim(new Claim(OpenIdClaims.Email, await UserManager.GetEmailAsync(user))); |
|||
|
|||
return principal; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,80 @@ |
|||
// ==========================================================================
|
|||
// ScriptUserTests.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Collections.Generic; |
|||
using System.Security.Claims; |
|||
using FluentAssertions; |
|||
using Squidex.Infrastructure.Security; |
|||
using Xunit; |
|||
|
|||
namespace Squidex.Domain.Apps.Core.Scripting |
|||
{ |
|||
public class ScriptUserTests |
|||
{ |
|||
[Fact] |
|||
public void Should_create_script_user_from_user_principal() |
|||
{ |
|||
var identity = new ClaimsIdentity(); |
|||
|
|||
identity.AddClaim(new Claim(OpenIdClaims.Subject, "1")); |
|||
identity.AddClaim(new Claim(OpenIdClaims.Email, "hello@squidex.io")); |
|||
identity.AddClaim(new Claim("claim1", "1a")); |
|||
identity.AddClaim(new Claim("claim1", "1b")); |
|||
identity.AddClaim(new Claim("claim2", "2a")); |
|||
identity.AddClaim(new Claim("claim2", "2b")); |
|||
|
|||
var principal = new ClaimsPrincipal(new[] { identity }); |
|||
|
|||
var scriptUser = ScriptUser.Create(principal); |
|||
|
|||
scriptUser.ShouldBeEquivalentTo( |
|||
new ScriptUser |
|||
{ |
|||
Email = "hello@squidex.io", |
|||
Id = "1", |
|||
IsClient = false, |
|||
Claims = new Dictionary<string, string[]> |
|||
{ |
|||
{ "sub", new [] { "1" } }, |
|||
{ "claim1", new[] { "1a", "1b" } }, |
|||
{ "claim2", new[] { "2a", "2b" } }, |
|||
{ "email", new [] { "hello@squidex.io" } } |
|||
} |
|||
}); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_create_script_user_from_client_principal() |
|||
{ |
|||
var identity = new ClaimsIdentity(); |
|||
|
|||
identity.AddClaim(new Claim(OpenIdClaims.ClientId, "1")); |
|||
identity.AddClaim(new Claim("claim1", "1a")); |
|||
identity.AddClaim(new Claim("claim1", "1b")); |
|||
identity.AddClaim(new Claim("claim2", "2a")); |
|||
identity.AddClaim(new Claim("claim2", "2b")); |
|||
|
|||
var principal = new ClaimsPrincipal(new[] { identity }); |
|||
|
|||
var scriptUser = ScriptUser.Create(principal); |
|||
|
|||
scriptUser.ShouldBeEquivalentTo( |
|||
new ScriptUser |
|||
{ |
|||
Id = "1", |
|||
IsClient = true, |
|||
Claims = new Dictionary<string, string[]> |
|||
{ |
|||
{ "client_id", new [] { "1" } } , |
|||
{ "claim1", new[] { "1a", "1b" } }, |
|||
{ "claim2", new[] { "2a", "2b" } } |
|||
} |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue