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