TokenVerifyPluginService.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Collections.Generic;
  3. using JsonRpcLite.Network;
  4. using JsonRpcLite.Services;
  5. using JsonRpcLite.Utilities;
  6. using Newtonsoft.Json;
  7. using WingInterfaceLibrary.Enum;
  8. using WingInterfaceLibrary.Interface;
  9. using WingInterfaceLibrary.Request.Authentication;
  10. using WingServerCommon.Log;
  11. using WingServerCommon.Service;
  12. namespace WingCloudServer.Plugin
  13. {
  14. public interface ITokenVerifyPlugin : IJsonRpcHttpServerEnginePlugin
  15. {
  16. }
  17. public class TokenVerifyPlugin : JsonRpcService, ITokenVerifyPlugin
  18. {
  19. protected IAuthenticationService _authenticationService;
  20. public override void Load(JsonRpcClientPool jsonRpcClientPool)
  21. {
  22. base.Load(jsonRpcClientPool);
  23. _authenticationService = GetProxy<IAuthenticationService>();
  24. }
  25. /// <summary>
  26. /// 不需要token验证的接口列表
  27. /// </summary>
  28. /// <value></value>
  29. public static List<string> NotValidationRequiredList = new List<string>
  30. {
  31. "IConnectService/ConnectAsync",
  32. "IEmailService/SendEmailAsync",
  33. "ILoginService/CommonLoginAsync",
  34. "ILoginService/CheckLoginTypeAsync",
  35. "ILoginService/CommonSignUpAsync",
  36. "ILoginService/CheckSMSVerificationCodeAsync",
  37. "ILoginService/SendSMSVerificationCodeAsync",
  38. "ILoginService/SendEmailVerificationCodeAsync",
  39. "ILoginService/CheckEmailVerificationCodeAsync",
  40. "ILoginService/RetrievePasswordByPhoneAsync",
  41. "ILoginService/RetrievePasswordByEmailAsync",
  42. "ILoginService/VerifyAccountAsync",
  43. "IManagementService/AdminLogin",
  44. "IManagementService/FindReportPreviewUrlAsync",
  45. "IManagementService/FindReportShareConentAsync",
  46. "IManagementService/FindRelatedDeviceCodesAsync",
  47. "IManagementService/AddReportTemplateAsync",
  48. "IManagementService/UpdateReportTemplateAsync",
  49. "IManagementService/RemoveReportTemplateAsync",
  50. "IManagementService/GetReportTemplateAsync",
  51. "IManagementService/GetShareExamUrlAsync",
  52. "IRemedicalService/GetReportElementByLanguageAsync",
  53. "IRemedicalService/PushFinshExamNotificationToClientAsync",
  54. "INotificationService/SendMessageAsync",
  55. "INotificationService/PostMessageAsync",
  56. "IRegionService/GetRegionsAsync",
  57. "IReportService/FindShareContentAsync",
  58. "ISMSService/SendMessageAsync",
  59. "ISMSService/CheckVerificationCodeAsync",
  60. "ISMSService/GeneralMessageAsync",
  61. "IStorageService/UploadFileAsync",
  62. "IVinnoServerService/UpdateServerInfoAsync",
  63. "IVinnoServerService/GetServerInfoListAsync",
  64. "IVinnoServerService/EchoAsync",
  65. "IVinnoServerService/UpdateServerIPListAsync",
  66. "IAIDiagnosisService/DiagnosisImageAsync",
  67. "IReportService/CreateReportTemplatePreviewAsync",
  68. "IWingRtcService/GetRoomIdAsync",
  69. "IWingRtcService/GenerateRoomUrlAsync",
  70. "IWingRtcService/GetRtcSettingAsync",
  71. "IWingRtcService/GetUserSignAsync",
  72. "ILiveConsultationService/SyncServerMessageAsync",
  73. };
  74. public PluginProcessResult PreProcess(IJsonRpcHttpContext context, byte[] requestData)
  75. {
  76. var dataLength = (int)context.GetRequestContentLength();
  77. var requests = JsonRpcCodec.DecodeRequestsAsync(requestData, new System.Threading.CancellationToken(), dataLength).Result;
  78. var apiName = $"{context.GetRequestPath().Trim('/')}/{requests[0].Method.Trim('/')}";
  79. if (context.GetRequestPath() != "/IAuthenticationService" && context.GetRequestPath() != "/IMasterInteractionCenterService" && !NotValidationRequiredList.Contains(apiName)&&!context.GetRequestPath().Contains("IDynamicSlaveService") )
  80. {
  81. var tokenRequest = new ValidateTokenRequest();
  82. try
  83. {
  84. tokenRequest = JsonConvert.DeserializeObject<List<ValidateTokenRequest>>(requests[0].Params.Value.ToString().Replace("\r", "").Replace("\n", "").Replace("\t", ""))[0];
  85. }
  86. catch (Exception)
  87. {
  88. try
  89. {
  90. tokenRequest = JsonConvert.DeserializeObject<ValidateTokenRequest>(requests[0].Params.Value.ToString().Replace("\r", "").Replace("\n", "").Replace("\t", ""));
  91. }
  92. catch (Exception ex)
  93. {
  94. Logger.WriteLineError($"TokenVerifyPlugin err{ex.ToString()},{JsonConvert.SerializeObject(requests)}");
  95. }
  96. }
  97. var result = _authenticationService.ValidateTokenAsync(tokenRequest).Result;
  98. if (result.Code != CustomerRpcCode.Ok)
  99. {
  100. ThrowRpcException((int)result.Code, "Permission validation error");
  101. }
  102. }
  103. return new PluginProcessResult(requestData, false);
  104. }
  105. public PluginProcessResult PostProcess(IJsonRpcHttpContext context, byte[] responseData)
  106. {
  107. return new PluginProcessResult(responseData, false);
  108. }
  109. }
  110. }