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
|
{
|
/// <summary>
|
///
|
/// </summary>
|
public class EzAuthorizationService : IEzAuthorizationService, IScoped
|
{
|
private IEzCoreNetRedisService _redisCacheSc;
|
private IHttpContextAccessor _Context;
|
public EzAuthorizationService(IEzCoreNetRedisService redisCacheSc, IHttpContextAccessor httpContext)
|
{
|
_redisCacheSc = redisCacheSc;
|
_Context = httpContext;
|
}
|
/// <summary>
|
/// Token
|
/// </summary>
|
/// <returns></returns>
|
public TokenInfo CreateToken<T>(T jwt) where T : EzJwtModel
|
{
|
|
IDictionary<string, object> propertyDictionary = new Dictionary<string, object>();
|
|
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<JWTSettingsOptions>("JWTSettings").ExpiredTime * 60 ?? 3600));
|
var token = JWTEncryption.Encrypt(propertyDictionary, App.GetConfig<JWTSettingsOptions>("JWTSettings").ExpiredTime ?? 3600);
|
|
DateTimeOffset dto = new DateTimeOffset(DateTime.Now);
|
var Expires = dto.ToUnixTimeSeconds();
|
IDictionary<string, object> REfpropertyDictionary = new Dictionary<string, object>();
|
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
|
};
|
|
}
|
/// <summary>
|
/// 刷新TOKEN
|
/// </summary>
|
/// <typeparam name="T"></typeparam>
|
/// <param name="refToken"></param>
|
/// <returns></returns>
|
/// <exception cref="TAxiosException"></exception>
|
public async Task<TokenInfo?> RefreshToken<T>(string refToken) where T : EzJwtModel
|
{
|
// IDictionary<string, object> REfpropertyDictionary = new Dictionary<string, object>();
|
|
// 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<JWTSettingsOptions>("JWTSettings");
|
if (jwtConfig == null)
|
throw new EZCoreException(" 系统错误,请重新登录", System.Net.HttpStatusCode.Unauthorized);
|
var LoinData = _redisCacheSc.Get<T>(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<TokenInfo?>(() =>
|
{
|
|
|
|
IDictionary<string, object> propertyDictionary = new Dictionary<string, object>();
|
|
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<string, object> refreshTokenClaims = new Dictionary<string, object>();
|
|
|
refreshTokenClaims.Add(new(
|
"RefTokenID", key
|
));
|
|
var refreshToken = JWTEncryption.Encrypt(refreshTokenClaims, refreshTokenouttimes);
|
|
return new TokenInfo
|
{
|
accessToken = token,
|
expires = expires,
|
refreshToken = refreshToken
|
};
|
});
|
|
|
|
}
|
}
|
}
|