|
@@ -13,6 +13,9 @@ using System.IO;
|
|
|
using System.Reflection;
|
|
|
using Microsoft.Extensions.Primitives;
|
|
|
using TEAMModelOS.SDK.Models.Table;
|
|
|
+using TEAMModelOS.SDK.Models.Cosmos.BI.BINormal;
|
|
|
+using System.Text.Json;
|
|
|
+using HTEXLib.COMM.Helpers;
|
|
|
|
|
|
namespace TEAMModelOS.Filter
|
|
|
{
|
|
@@ -78,7 +81,8 @@ namespace TEAMModelOS.Filter
|
|
|
var option = services.GetService<IOptions<Option>>();
|
|
|
var azureRedis = services.GetService<AzureRedisFactory>();
|
|
|
var azureStorage = services.GetService<AzureStorageFactory>();
|
|
|
- return new InternalAuthTokenFilter(option ,azureRedis, azureStorage, Auth, Limit);
|
|
|
+ var azureCosmos = services.GetService<AzureCosmosFactory>();
|
|
|
+ return new InternalAuthTokenFilter(azureCosmos , option ,azureRedis, Auth, Limit);
|
|
|
}
|
|
|
|
|
|
private class InternalAuthTokenFilter : IResourceFilter
|
|
@@ -88,15 +92,14 @@ namespace TEAMModelOS.Filter
|
|
|
private readonly string _auth;
|
|
|
private readonly bool _limit;
|
|
|
private readonly AzureRedisFactory _azureRedis;
|
|
|
- private readonly AzureStorageFactory _azureStorage ;
|
|
|
-
|
|
|
- public InternalAuthTokenFilter(IOptions<Option> option, AzureRedisFactory azureRedis, AzureStorageFactory azureStorage, string auth, bool limit)
|
|
|
+ private readonly AzureCosmosFactory _azureCosmos;
|
|
|
+ public InternalAuthTokenFilter(AzureCosmosFactory azureCosmos,IOptions<Option> option, AzureRedisFactory azureRedis, string auth, bool limit)
|
|
|
{
|
|
|
_option = option.Value;
|
|
|
_auth = auth;
|
|
|
_limit = limit;
|
|
|
_azureRedis = azureRedis;
|
|
|
- _azureStorage = azureStorage;
|
|
|
+ _azureCosmos=azureCosmos;
|
|
|
}
|
|
|
public void OnResourceExecuting(ResourceExecutingContext context)
|
|
|
{
|
|
@@ -113,6 +116,8 @@ namespace TEAMModelOS.Filter
|
|
|
scope = paths[0];
|
|
|
}
|
|
|
}
|
|
|
+ string msg = "";
|
|
|
+ int code = 0;
|
|
|
if (scope.Equals("school") ||scope.Equals("business"))
|
|
|
{
|
|
|
string id = string.Empty, school = string.Empty, jti = string.Empty;
|
|
@@ -123,8 +128,10 @@ namespace TEAMModelOS.Filter
|
|
|
{
|
|
|
var jwt = new JwtSecurityTokenHandler().ReadJwtToken(authtoken);
|
|
|
string iss = jwt.Payload.Iss; //iss 检查jwt是否是测试站,正式站的授权key
|
|
|
- string tokenScope= jwt.Claims.FirstOrDefault(claim => claim.Type.Equals("scope"))?.Value;
|
|
|
- //if (tokenScope.Equals(scope)) {
|
|
|
+ string tokenScope = jwt.Claims.FirstOrDefault(claim => claim.Type.Equals("scope"))?.Value;
|
|
|
+ //访问的jwt的tokenScope 是否与访问的接口一致。
|
|
|
+ if (tokenScope.Equals(scope))
|
|
|
+ {
|
|
|
|
|
|
var keys = OpenApiJtwIssuer.OpenApiJtw签发者.GetDescriptionText().Split(',');
|
|
|
string issuer = "";
|
|
@@ -162,7 +169,7 @@ namespace TEAMModelOS.Filter
|
|
|
//school = jwt.Payload.Azp;//学校编码
|
|
|
//如果是商务合作模式 则需要手动获取学校编码
|
|
|
school = context.HttpContext.GetXAuth("School");
|
|
|
-#if !DEBUG
|
|
|
+#if DEBUG
|
|
|
var permissions = jwt.Claims.Where(c => c.Type.Equals("auth"));
|
|
|
///当前请求的api的设置的permission值是否包含在 从jwt的获取["1","2","3","4","5"]值中
|
|
|
if (!string.IsNullOrWhiteSpace(_auth) && permissions.Count() > 0)
|
|
@@ -171,9 +178,16 @@ namespace TEAMModelOS.Filter
|
|
|
{
|
|
|
pass = true;
|
|
|
}
|
|
|
+ else {
|
|
|
+ msg = "当前访问的接口不在已授权的接口范围内!";
|
|
|
+ code = 401001;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ msg = "token未设置可访问的接口!"; code = 401002;
|
|
|
}
|
|
|
#else
|
|
|
- pass = true;
|
|
|
+ pass = true;
|
|
|
#endif
|
|
|
}
|
|
|
else if (scope.Equals("business"))
|
|
@@ -186,31 +200,47 @@ namespace TEAMModelOS.Filter
|
|
|
{
|
|
|
pass = true;
|
|
|
}
|
|
|
- else {
|
|
|
+ else
|
|
|
+ {
|
|
|
//如果访问的接口是 business/get-schools
|
|
|
if (!string.IsNullOrWhiteSpace(XAuthSchool))
|
|
|
{
|
|
|
- var table = _azureStorage.GetCloudTableClient().GetTableReference("IESOpenApi");
|
|
|
- List<BusinessSchool> schools = table.FindListByDictSync<BusinessSchool>(new Dictionary<string, object> { { "PartitionKey", $"BusinessSchool" } ,{ "school", XAuthSchool },{ "bizid" ,id} });
|
|
|
- List<BusinessConfig> configs = table.FindListByDictSync<BusinessConfig>(new Dictionary<string, object> { { "PartitionKey", $"BusinessConfig" }, { "RowKey", id } });
|
|
|
- //同时授权学校存在,且jti一致 ,验证jti原因在于 token轮换或刷新后,旧的token不能再使用。
|
|
|
- if (schools.Any() && configs.Any())
|
|
|
+ var response = _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync(id ,new Azure.Cosmos.PartitionKey("BizConfig")).Result ;
|
|
|
+ if (response.Status == 200)
|
|
|
{
|
|
|
- if (configs[0].jti.Equals(jti)) {
|
|
|
- pass = true;
|
|
|
- school = XAuthSchool;
|
|
|
+ BizConfig bizConfig = JsonDocument.Parse(response.Content).Deserialize<BizConfig>() ;
|
|
|
+ if (bizConfig.schools.IsNotEmpty() && bizConfig.schools.Select(z=>z.id).Contains(XAuthSchool))
|
|
|
+ {
|
|
|
+ if (bizConfig.jti.Equals(jti))
|
|
|
+ {
|
|
|
+ school = XAuthSchool;
|
|
|
+ pass = true;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ msg = "当前Token已经失效!"; code = 401003;
|
|
|
+ pass = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ msg = "未开通学校数据访问授权!"; code = 401004;
|
|
|
+ pass = false;
|
|
|
}
|
|
|
}
|
|
|
else {
|
|
|
+ msg = "企业信息不存在!"; code = 401005;
|
|
|
pass = false;
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
+ msg = "学校编码为空!"; code = 401006;
|
|
|
pass = false;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ else {
|
|
|
+ msg = "访问的接口不在学校或第三方业务范围内!"; code = 401007;
|
|
|
+ }
|
|
|
if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(school) && !string.IsNullOrEmpty(jti))
|
|
|
{
|
|
|
//AIP 开启限流策略 处理限流问题
|
|
@@ -219,7 +249,13 @@ namespace TEAMModelOS.Filter
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- //}
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ msg = "token的scope与接口的业务类型不匹配!"; code = 401008;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ msg = "token error!"; code = 401009;
|
|
|
}
|
|
|
if (pass)
|
|
|
{
|
|
@@ -228,16 +264,15 @@ namespace TEAMModelOS.Filter
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- JsonResult jsonResult = new JsonResult(new { });
|
|
|
+ JsonResult jsonResult = new JsonResult(new { msg,code});
|
|
|
jsonResult.StatusCode = 401;
|
|
|
context.Result = jsonResult;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
}
|
|
|
else {
|
|
|
- JsonResult jsonResult = new JsonResult(new { });
|
|
|
+ msg = "该接口暂未授权访问!";
|
|
|
+ code = 401010;
|
|
|
+ JsonResult jsonResult = new JsonResult(new { msg,code});
|
|
|
jsonResult.StatusCode = 401;
|
|
|
context.Result = jsonResult;
|
|
|
}
|