ApiTokenAttribute.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using Microsoft.AspNetCore.Mvc;
  2. using Microsoft.AspNetCore.Mvc.Filters;
  3. using TEAMModelOS.SDK.Extension;
  4. using System;
  5. using TEAMModelOS.Models;
  6. using Microsoft.Extensions.Options;
  7. using Microsoft.Extensions.DependencyInjection;
  8. using System.IdentityModel.Tokens.Jwt;
  9. using System.Linq;
  10. using TEAMModelOS.SDK.DI;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Reflection;
  14. namespace TEAMModelOS.Filter
  15. {
  16. public class LimitPolicy {
  17. /// <summary>
  18. /// 颁发给谁的主体
  19. /// </summary>
  20. public string id { get; set; }
  21. /// <summary>
  22. /// 颁发主体
  23. /// </summary>
  24. public string school { get; set; }
  25. /// <summary>
  26. /// AIP的唯一ID
  27. /// </summary>
  28. public string jti { get; set; }
  29. /// <summary>
  30. /// minute 分钟,表示按分钟限流,多少分钟内只能访问多少次,
  31. /// hour 小时,表示按小时限流,多少小时内只能访问多少次,
  32. /// day 天数,表示按天数限流,多少天数内只能访问多少次,
  33. /// </summary>
  34. public string policy { get; set; }
  35. /// <summary>
  36. /// policy 策略,分钟,小时,天数对应的时长
  37. /// </summary>
  38. public int duration { get; set; }
  39. /// <summary>
  40. /// policy 策略,分钟,小时,天数对应的时长(duration) 可以访问的次数
  41. /// </summary>
  42. public int times { get; set; }
  43. /// <summary>
  44. /// 是否免费调用
  45. /// </summary>
  46. ///public bool free { get; set; }
  47. /// <summary>
  48. /// 每次调用花费多少钱
  49. /// </summary>
  50. ///public decimal cost { get; set; }
  51. }
  52. public class ApiTokenAttribute : Attribute, IFilterFactory
  53. {
  54. public bool IsReusable => true;
  55. /// <summary>
  56. /// 是否开启限流策略
  57. /// </summary>
  58. public bool Limit { get; set; }
  59. /// <summary>
  60. /// 授权序列
  61. /// </summary>
  62. public string Auth { get; set; }
  63. /// <summary>
  64. /// 接口名称
  65. /// </summary>
  66. public string Name { get; set; }
  67. public IFilterMetadata CreateInstance(IServiceProvider services)
  68. {
  69. var option = services.GetService<IOptions<Option>>();
  70. var azureRedis = services.GetService<AzureRedisFactory>();
  71. return new InternalAuthTokenFilter(option ,azureRedis, Auth, Limit);
  72. }
  73. private class InternalAuthTokenFilter : IResourceFilter
  74. {
  75. private readonly Option _option;
  76. //private readonly string _roles;
  77. private readonly string _auth;
  78. private readonly bool _limit;
  79. private readonly AzureRedisFactory _azureRedis;
  80. public InternalAuthTokenFilter(IOptions<Option> option, AzureRedisFactory azureRedis, string auth, bool limit)
  81. {
  82. _option = option.Value;
  83. _auth = auth;
  84. _limit = limit;
  85. _azureRedis = azureRedis;
  86. }
  87. public void OnResourceExecuting(ResourceExecutingContext context)
  88. {
  89. bool pass = false;
  90. string id = string.Empty, school = string.Empty,jti=string.Empty;
  91. var authtoken = context.HttpContext.GetXAuth("ApiToken");
  92. if (!string.IsNullOrWhiteSpace(authtoken) && JwtAuthExtension.ValidateApiToken(authtoken, _option.JwtSecretKey))
  93. {
  94. var jwt = new JwtSecurityTokenHandler().ReadJwtToken(authtoken);
  95. string iss = jwt.Payload.Iss; //iss 检查jwt是否是测试站,正式站的授权key
  96. if (iss.Equals(_option.HostName))
  97. {
  98. //aud 受众
  99. id = jwt.Payload.Sub;//主题,又是应用APP
  100. school = jwt.Payload.Azp;//学校编码
  101. jti = jwt.Payload.Jti;//jwt唯一标识
  102. var permissions = jwt.Claims.Where(c => c.Type.Equals("auth"));
  103. ///当前请求的api的设置的permission值是否包含在 从jwt的获取["1","2","3","4","5"]值中
  104. if (!string.IsNullOrWhiteSpace(_auth) && permissions.Count() > 0)
  105. {
  106. if (permissions.Select(x => x.Value).Contains(_auth))
  107. {
  108. pass = true;
  109. }
  110. }
  111. if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(school) && !string.IsNullOrEmpty(jti))
  112. {
  113. //AIP 开启限流策略 处理限流问题
  114. if (_limit)
  115. {
  116. }
  117. }
  118. }
  119. }
  120. if (pass)
  121. {
  122. context.HttpContext.Items.Add("ID", id);
  123. context.HttpContext.Items.Add("School", school);
  124. }
  125. else
  126. {
  127. context.Result = new UnauthorizedResult();
  128. }
  129. }
  130. public void OnResourceExecuted(ResourceExecutedContext context)
  131. {
  132. }
  133. }
  134. }
  135. }