Skip to content

Commit

Permalink
feat: Add ThirdPartyUserClaim
Browse files Browse the repository at this point in the history
  • Loading branch information
wzh425 committed Mar 8, 2024
1 parent 54b3b8b commit ea0a7ae
Show file tree
Hide file tree
Showing 12 changed files with 3,282 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -422,12 +422,13 @@ public async Task GetThirdPartyUserAsync(ThirdPartyUserQuery query)
.Include(tpu => tpu.User.Roles)
.FirstOrDefaultAsync(tpu => tpu.ThridPartyIdentity == query.ThridPartyIdentity);
var userModel = tpUser?.User?.Adapt<UserModel>();

if (tpUser != null && tpUser.User != null && userModel != null)
{
var staff = tpUser.User.Staff;
userModel.StaffId = (staff == null || !staff.Enabled) ? Guid.Empty : staff.Id;
userModel.CurrentTeamId = staff?.CurrentTeamId;
userModel.ClaimData = tpUser.ClaimData;
}

query.Result = userModel;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the Apache License. See LICENSE.txt in the project root for license information.

using Microsoft.AspNetCore.Identity;

namespace Masa.Auth.Service.Admin.Domain.Subjects.Aggregates;

public class PasswordType : Enumeration
{
public static PasswordType Default = new PasswordType();

public static PasswordType MD5 = new MD5PasswordType();

public static PasswordType HashPassword = new HashPasswordType();

public PasswordType() : base(0, "") { }

public PasswordType(int id, string name) : base(id, name)
{
}

public virtual string EncryptPassword(User user, string password)
{
return MD5.EncryptPassword(user, password);
}

public virtual bool VerifyPassword(User user, string encryptPassword, string providedPassword)
{
return MD5.VerifyPassword(user, encryptPassword, providedPassword);
}

public static PasswordType StartNew(string type) => type switch
{

nameof(MD5) => new MD5PasswordType(),
nameof(HashPassword) => new HashPasswordType(),
_ => new PasswordType()
};

private class MD5PasswordType : PasswordType
{
public MD5PasswordType() : base(1, nameof(MD5)) { }

public override string EncryptPassword(User user, string password)
{
return MD5Utils.EncryptRepeat(password);
}

public override bool VerifyPassword(User user, string encryptPassword, string providedPassword)
{
return encryptPassword == MD5Utils.EncryptRepeat(providedPassword ?? "");
}
}

private class HashPasswordType : PasswordType
{
public HashPasswordType() : base(2, nameof(HashPassword)) { }

public override string EncryptPassword(User user, string password)
{
var hasher = new PasswordHasher<User>();
return hasher.HashPassword(user, password);
}

public override bool VerifyPassword(User user, string encryptPassword, string providedPassword)
{
var hasher = new PasswordHasher<User>();
var result = hasher.VerifyHashedPassword(user, encryptPassword, providedPassword);
return result != PasswordVerificationResult.Failed;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public string ExtendedData

public IdentityProvider IdentityProvider => _identityProvider;

public Dictionary<string, string> ClaimData { get; private set; } = new();

public ThirdPartyUser(Guid thirdPartyIdpId, string thridPartyIdentity, string extendedData)
{
ThirdPartyIdpId = thirdPartyIdpId;
Expand Down Expand Up @@ -83,6 +85,11 @@ public void Update(string thridPartyIdentity, string extendedData)
ExtendedData = extendedData;
}

public void UpdateClaimData(Dictionary<string, string> claimData)
{
ClaimData = claimData;
}

public static implicit operator ThirdPartyUserDetailDto(ThirdPartyUser tpu)
{
return new ThirdPartyUserDetailDto(tpu.Id, tpu.Enabled, tpu.IdentityProvider.Adapt<IdentityProviderDetailDto>(), tpu.User, tpu.CreationTime, tpu.ModificationTime, tpu.CreateUser?.Name ?? "", tpu.ModifyUser?.Name ?? "");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the Apache License. See LICENSE.txt in the project root for license information.

using Masa.Auth.Service.Admin.Infrastructure.ValueConverters;

namespace Masa.Auth.Service.Admin.Infrastructure.EntityConfigurations.Subjects;

public class ThirdPartyUserEntityTypeConfiguration : IEntityTypeConfiguration<ThirdPartyUser>
Expand All @@ -12,6 +14,7 @@ public void Configure(EntityTypeBuilder<ThirdPartyUser> builder)
builder.HasOne(tpu => tpu.IdentityProvider).WithMany().HasForeignKey(tpu => tpu.ThirdPartyIdpId);
builder.HasIndex(u => new { u.CreationTime, u.ModificationTime });//.IsDescending(); supported 7.0
builder.Navigation(tpu => tpu.IdentityProvider).AutoInclude();
builder.Property(tpu => tpu.ClaimData).HasConversion(new JsonValueConverter<Dictionary<string, string>>());
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the Apache License. See LICENSE.txt in the project root for license information.

using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

namespace Masa.Auth.Service.Admin.Infrastructure.ValueConverters;

public class JsonValueConverter<T> : ValueConverter<T, string> where T : class, new()
{
public JsonValueConverter()
: base(x => SerializeObject(x), x => DeserializeObject(x))
{

}

private static string SerializeObject(T obj)
{
return JsonSerializer.Serialize(obj);
}

private static T DeserializeObject(string json)
{
if (string.IsNullOrEmpty(json))
{
return new T();
}

return JsonSerializer.Deserialize<T>(json)!;
}
}
Loading

0 comments on commit ea0a7ae

Please sign in to comment.