FalconSSO/FAuth/Extensions/Decryptor/UserTicketDryptor.cs

53 lines
2.0 KiB
C#
Raw Normal View History

using System;
using Falcon.Extend;
using Microsoft.Extensions.Options;
namespace FAuth.Extensions.Decryptor
{
/// <summary>
/// 用户凭据实现
/// </summary>
public class UserTicketDryptor:IUserTicketDryptor
{
/// <summary>
/// 基础加密解密服务
/// </summary>
public ICryptoService CryptoService { get; set; }
/// <summary>
/// 用户凭据配置
/// </summary>
public UserTicketDecryptorOption Option { get; set; }
/// <summary>
/// 基础Json序列化提供器
/// </summary>
public IJsonProvider JsonProvider { get; set; }
/// <summary>
/// 构造用户凭据实现
/// </summary>
/// <param name="s">基础加密解密提供器</param>
/// <param name="option">凭据选项</param>
/// <param name="json">Json序列化器</param>
public UserTicketDryptor(ICryptoService s,IOptions<UserTicketDecryptorOption> option,IJsonProvider json) {
this.CryptoService = s ?? throw new ArgumentNullException(nameof(s));
this.Option = option?.Value ?? throw new ArgumentNullException(nameof(option));
this.JsonProvider = json ?? throw new ArgumentNullException(nameof(json));
}
/// <summary>
/// 通过用户凭据获得用户信息
/// </summary>
/// <param name="content">用户凭据</param>
/// <returns>用户信息</returns>
public UserTicketModel Decrypt(string content) =>
this.JsonProvider.GetObj<UserTicketModel>(this.CryptoService.Decrypt(content,this.Option.Key));
/// <summary>
/// 通过用户信息生成用户凭据
/// </summary>
/// <param name="user">用户信息</param>
/// <returns>用户凭据</returns>
public string Encrypt(UserTicketModel user) =>
this.CryptoService.Encrypt(this.JsonProvider.GetJson(user),Option.Key);
}
}