using Furion.Authorization;
using Furion.DataEncryption;
using Furion;
using Furion.DependencyInjection;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using EzCoreNet.Redis;
using SqlSugar.Extensions;
using Furion.FriendlyException;
namespace cylsg.Authorization
{
///
///
///
public class EzAuthorizationService : IEzAuthorizationService, IScoped
{
private IEzCoreNetRedisService _redisCacheSc;
private IHttpContextAccessor _Context;
public EzAuthorizationService(IEzCoreNetRedisService redisCacheSc, IHttpContextAccessor httpContext)
{
_redisCacheSc = redisCacheSc;
_Context = httpContext;
}
///
/// Token
///
///
public TokenInfo CreateToken(T jwt) where T : EzJwtModel
{
IDictionary propertyDictionary = new Dictionary();
PropertyInfo[] properties = jwt.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo property in properties)
{
string propertyName = property.Name;
object propertyValue = property.GetValue(jwt);
propertyDictionary.Add(propertyName.ToLower(), propertyValue);
}
var expires = DateTime.Now.AddSeconds(Convert.ToInt32(App.GetConfig("JWTSettings").ExpiredTime * 60 ?? 3600));
var token = JWTEncryption.Encrypt(propertyDictionary, App.GetConfig("JWTSettings").ExpiredTime ?? 3600);
DateTimeOffset dto = new DateTimeOffset(DateTime.Now);
var Expires = dto.ToUnixTimeSeconds();
IDictionary REfpropertyDictionary = new Dictionary();
REfpropertyDictionary.Add(new(
"RefTokenID", $"{jwt.ITCode}:{Expires}"
));
var RefExpires = App.Configuration["JWTSettings:RefreshTokenExpires"].ObjToInt();
var refreshToken = JWTEncryption.Encrypt(REfpropertyDictionary, RefExpires);
//写入刷新可Token时间
_redisCacheSc.Add($"{jwt.ITCode}:{Expires}", jwt, RefExpires * 60);
return new TokenInfo
{
accessToken = token,
expires = expires,
refreshToken = refreshToken
};
}
///
/// 刷新TOKEN
///
///
///
///
///
public async Task RefreshToken(string refToken) where T : EzJwtModel
{
// IDictionary REfpropertyDictionary = new Dictionary();
// var tokenInfo = JWTEncryption.ReadJwtToken("token");
var (isValid, tokenData, validationResult) = JWTEncryption.Validate(refToken);
if (!isValid)
new EZCoreException(" 系统错误,请重新登录", System.Net.HttpStatusCode.Unauthorized);
var user = tokenData.Claims;
if (user == null)
throw new EZCoreException("参数丢失,请重新登录", System.Net.HttpStatusCode.Unauthorized);
var key = user.Where(x => x.Type == "RefTokenID").Select(x => x.Value).FirstOrDefault();
//if (key == null)
// throw Oops.Oh("token已过期,请重新登录", System.Net.HttpStatusCode.Unauthorized);
var jwtConfig = App.GetConfig("JWTSettings");
if (jwtConfig == null)
throw new EZCoreException(" 系统错误,请重新登录", System.Net.HttpStatusCode.Unauthorized);
var LoinData = _redisCacheSc.Get(key);
if (LoinData == null)
throw new EZCoreException("token已过期,请重新登录", System.Net.HttpStatusCode.Unauthorized); //Oops.Oh("token已过期,请重新登录", System.Net.HttpStatusCode.Unauthorized);
var refreshTokenouttimes = _redisCacheSc.GetTtl(key);
if (refreshTokenouttimes <= 0)
{
throw new EZCoreException("token已过期,请重新登录", System.Net.HttpStatusCode.Unauthorized);
}
return await Task.Run(() =>
{
IDictionary propertyDictionary = new Dictionary();
PropertyInfo[] properties = LoinData.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo property in properties)
{
string propertyName = property.Name;
object propertyValue = property.GetValue(LoinData);
propertyDictionary.Add(propertyName.ToLower(), propertyValue);
}
var expires = DateTime.Now.AddSeconds(Convert.ToInt32(jwtConfig.ExpiredTime * 60));
var token = JWTEncryption.Encrypt(propertyDictionary, jwtConfig.ExpiredTime);
IDictionary refreshTokenClaims = new Dictionary();
refreshTokenClaims.Add(new(
"RefTokenID", key
));
var refreshToken = JWTEncryption.Encrypt(refreshTokenClaims, refreshTokenouttimes);
return new TokenInfo
{
accessToken = token,
expires = expires,
refreshToken = refreshToken
};
});
}
}
}