-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
3,282 additions
and
5 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
72 changes: 72 additions & 0 deletions
72
src/Services/Masa.Auth.Service.Admin/Domain/Subjects/Aggregates/PasswordType.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,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; | ||
} | ||
} | ||
} |
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
30 changes: 30 additions & 0 deletions
30
src/Services/Masa.Auth.Service.Admin/Infrastructure/ValueConverters/JsonValueConverter.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,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)!; | ||
} | ||
} |
Oops, something went wrong.