security fix

This commit is contained in:
Aigio Liu
2021-03-08 17:10:26 +08:00
parent 77eff6522f
commit ffc0401409
21 changed files with 383 additions and 245 deletions
@@ -1,4 +1,5 @@
using System.Security.Cryptography;
using System.Application.Security;
using System.Security.Cryptography;
using MPIgnore = MessagePack.IgnoreMemberAttribute;
using MPKey = MessagePack.KeyAttribute;
using MPObj = MessagePack.MessagePackObjectAttribute;
@@ -41,7 +42,7 @@ namespace System.Application.Models
{
if (aes == null)
{
if (AesSecret == null) throw new ArgumentNullException(nameof(AesSecret));
if (AesSecret == null) throw new IsNotOfficialChannelPackageException(nameof(Aes), new ArgumentNullException(nameof(AesSecret)));
aes = AESUtils.Create(AesSecret);
}
return aes;
@@ -69,7 +70,7 @@ namespace System.Application.Models
{
if (rsa == null)
{
if (RSASecret == null) throw new ArgumentNullException(nameof(RSASecret));
if (RSASecret == null) throw new IsNotOfficialChannelPackageException(nameof(RSA), new ArgumentNullException(nameof(RSASecret)));
rsa = RSAUtils.CreateFromJsonString(RSASecret);
}
return rsa;
@@ -1,28 +0,0 @@
namespace System.Application.Services
{
/// <summary>
/// 安全服务
/// </summary>
public interface ISecurityService
{
/// <summary>
/// 加密
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
byte[]? E(string? value);
/// <inheritdoc cref="E(string?)"/>
byte[]? EB(byte[]? value);
/// <summary>
/// 解密
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
string? D(byte[]? value);
/// <inheritdoc cref="D(byte[]?)"/>
byte[]? DB(byte[]? value);
}
}
@@ -1,11 +1,12 @@
using Microsoft.Extensions.Options;
using System.Application.Models;
using System.Application.Security;
using System.Security.Cryptography;
namespace System.Application.Services.Implementation
{
/// <inheritdoc cref="ISecurityService"/>
public class SecurityService : ISecurityService
public class SecurityService : SecurityServiceBase
{
protected readonly AppSettings settings;
@@ -14,12 +15,20 @@ namespace System.Application.Services.Implementation
settings = options.Value;
}
public virtual byte[]? E(string? value) => AESUtils.EncryptToByteArray_Nullable(settings.Aes, value);
public virtual byte[]? EB(byte[]? value) => AESUtils.Encrypt_Nullable(settings.Aes, value);
public virtual string? D(byte[]? value) => AESUtils.DecryptToString_Nullable(settings.Aes, value);
public virtual byte[]? DB(byte[]? value) => AESUtils.Decrypt_Nullable(settings.Aes, value);
public override Aes? Aes
{
get
{
try
{
return settings.Aes;
}
catch (IsNotOfficialChannelPackageException e)
{
Log.Error(nameof(SecurityService), e, nameof(ApiResponseCode.IsNotOfficialChannelPackage));
return null;
}
}
}
}
}
@@ -143,19 +143,21 @@ namespace System.Application.Services.Implementation
return userRepository.FindAsync(userId);
}
TUserDTO? GetUserByTable<TUserDTO>(User? user, Func<User, TUserDTO?> binding) where TUserDTO : IUserDTO
async Task<TUserDTO?> GetUserByTable<TUserDTO>(User? user, Func<User, Task<TUserDTO?>> binding) where TUserDTO : IUserDTO
{
if (user == null) return default;
var value = binding.Invoke(user);
var value = await binding(user);
return value;
}
TUserDTO? BindingUser<TUserDTO>(User user) where TUserDTO : IUserDTO, new()
async Task<TUserDTO?> BindingUser<TUserDTO>(User user) where TUserDTO : IUserDTO, new()
{
var nickName = await security.D(user.NickName);
var value = new TUserDTO
{
Id = user.Id,
NickName = security.D(user.NickName),
NickName = nickName,
Avatar = user.Avatar,
};
return value;
@@ -164,7 +166,7 @@ namespace System.Application.Services.Implementation
public async Task<UserDTO?> GetUserByIdAsync(Guid userId)
{
var user = await GetUserTableByIdAsync(userId);
var value = GetUserByTable(user, BindingUser<UserDTO>);
var value = await GetUserByTable(user, BindingUser<UserDTO>);
return value;
}
@@ -224,10 +226,12 @@ namespace System.Application.Services.Implementation
public async Task InsertOrUpdateAsync(IUserDTO user)
{
var nickName_ = await security.E(user.NickName);
var userTable = new User
{
Id = user.Id,
NickName = security.E(user.NickName),
NickName = nickName_,
Avatar = user.Avatar,
};
//if (user is UserInfoDTO userInfo)