LoginInfoService.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using IdentityModel;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.Extensions.Configuration;
  4. using Microsoft.Extensions.Options;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Security.Claims;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using TEAMModelOS.Model.Common.Dtos;
  11. using TEAMModelOS.Model.Common.Models;
  12. using TEAMModelOS.SDK.Context.Configuration;
  13. using TEAMModelOS.SDK.Context.Constant.Common;
  14. using TEAMModelOS.SDK.Context.Exception;
  15. using TEAMModelOS.SDK.Extension.DataResult.JsonRpcRequest;
  16. using TEAMModelOS.SDK.Extension.DataResult.JsonRpcResponse;
  17. using TEAMModelOS.SDK.Extension.HttpClient.Implements;
  18. using TEAMModelOS.SDK.Extension.JwtAuth.JwtHelper;
  19. using TEAMModelOS.SDK.Extension.JwtAuth.Models;
  20. using TEAMModelOS.SDK.Helper.Common.DateTimeHelper;
  21. using TEAMModelOS.SDK.Helper.Common.JsonHelper;
  22. using TEAMModelOS.SDK.Helper.Network.HttpHelper;
  23. using TEAMModelOS.SDK.Helper.Security.BCryptHelper;
  24. using TEAMModelOS.SDK.Module.AzureTable.Interfaces;
  25. using TEAMModelOS.Service.Common.Interfaces;
  26. namespace TEAMModelOS.Service.Common.Implements
  27. {
  28. public class LoginInfoService : ILoginInfoService
  29. {
  30. private IAzureTableDBRepository _repository;
  31. private IOptions<JwtSetting> _options;
  32. private IHttpContextAccessor _httpContextAccessor;
  33. private HttpClientUserInfo _httpClientService;
  34. public LoginInfoService(IAzureTableDBRepository repository, IOptions<JwtSetting> options, IHttpContextAccessor httpContextAccessor , HttpClientUserInfo httpClientService)
  35. {
  36. _httpContextAccessor = httpContextAccessor;
  37. _options = options;
  38. _repository = repository;
  39. _httpClientService = httpClientService;
  40. }
  41. public async Task<LoginResult> CheckLoginAsync(TicketInfo ticketInfo) {
  42. string jtoken = HttpContextHelper.GetValueInHttp(_httpContextAccessor.HttpContext.Request, Constants.AUTHORIZATION);
  43. if (string.IsNullOrEmpty(ticketInfo.Token))
  44. {
  45. string code = BCryptHelper.Ecrypt(ticketInfo.Ticket + ticketInfo.TeamModelId);
  46. bool f = BCryptHelper.Verify(ticketInfo.Ticket + ticketInfo.TeamModelId, ticketInfo.Sign);
  47. LoginResult result = new LoginResult();
  48. LoginInfo login = _repository.FindOneByKey<LoginInfo>("Ticket", ticketInfo.Ticket).Result;
  49. if (login != null && !string.IsNullOrEmpty(login.Token))
  50. {
  51. result.CheckTicket = true;
  52. JwtResponse token = CreateJwtToken(login);
  53. result.JwtToken = token;
  54. login.Token = token.Access_token;
  55. result.JwtToken.Scope = login.Scope;
  56. await _repository.Update<LoginInfo>(login);
  57. return result;
  58. }
  59. JosnRPCRequest<Dictionary<string, object>> request = new JosnRPCRequest<Dictionary<string, object>>
  60. {
  61. method = "UserInfo"
  62. };
  63. if (string.IsNullOrEmpty(ticketInfo.Ticket)) {
  64. result.CheckTicket = false;
  65. return result;
  66. }
  67. Dictionary<string, object> ticket = new Dictionary<string, object>
  68. {
  69. { "ticket", ticketInfo.Ticket }
  70. };
  71. request.@params = ticket;
  72. string data = MessagePackHelper.ObjectToJson(request);
  73. string jsonStr = _httpClientService.HttpPost(BaseConfigModel.Configuration["HaBookAuth:AccountUrl"], data, Constants.CONTENT_TYPE_JSON, Encoding.UTF8);
  74. if (!string.IsNullOrEmpty(jsonStr))
  75. {
  76. JosnRPCResponse<TeamModelIdInfo> response = MessagePackHelper.JsonToObject<JosnRPCResponse<TeamModelIdInfo>>(jsonStr);
  77. if (response.error == null && response != null)
  78. {
  79. result.CheckTicket = true;
  80. LoginInfo loginInfo = new LoginInfo
  81. {
  82. PartitionKey = response.result.cellphone,
  83. Phone = response.result.cellphone,
  84. RowKey = Guid.NewGuid().ToString(),
  85. TeamModelId = response.result.id,
  86. Name = response.result.name,
  87. Ticket = ticketInfo.Ticket,
  88. CountryCode = response.result.countryCode
  89. };
  90. TeamModelUser user= await _repository.FindOneByKey<TeamModelUser>("TeamModelId", response.result.id);
  91. if (user == null || string.IsNullOrEmpty(user.RowKey))
  92. {
  93. user = new TeamModelUser { RowKey = Guid.NewGuid().ToString(), PartitionKey = loginInfo.CountryCode ,RegisterTime=DateTimeHelper.ConvertToTimeStamp13(DateTime.Now) };
  94. }
  95. user.Cellphone = response.result.cellphone;
  96. user.NickName = response.result.name;
  97. if (string.IsNullOrEmpty(user.FullName)) {
  98. user.FullName = response.result.name;
  99. }
  100. user.TeamModelId = response.result.id;
  101. user.CountryCode = response.result.countryCode;
  102. JwtResponse jwtToken = CreateJwtToken(loginInfo);
  103. loginInfo.Token = jwtToken.Access_token;
  104. loginInfo.Scope = jwtToken.Scope;
  105. result.JwtToken = jwtToken;
  106. await _repository.Save<LoginInfo>(loginInfo);
  107. await _repository.SaveOrUpdate<TeamModelUser>(user);
  108. return result;
  109. }
  110. else
  111. {
  112. result.CheckTicket = false;
  113. return result;
  114. }
  115. }
  116. else
  117. {
  118. result.CheckTicket = false;
  119. return result;
  120. }
  121. }
  122. else
  123. {
  124. ClaimModel claimModel = JwtHelper.SerializeJWT(ticketInfo.Token);
  125. var dateTime = DateTimeHelper.ConvertToTimeStamp10(DateTime.Now);
  126. var expExt=claimModel.Claim.TryGetValue("exp",out var exp);
  127. if (expExt==false || dateTime > long.Parse(exp))
  128. {
  129. throw new BizException(401, "Unauthorized");
  130. }
  131. Dictionary<string, object> msp = new Dictionary<string, object>
  132. {
  133. { "Token", ticketInfo.Token }
  134. };
  135. LoginInfo loginInfo = _repository.FindOneByDict<LoginInfo>(msp).Result;
  136. if (loginInfo != null && !string.IsNullOrEmpty(loginInfo.Token))
  137. {
  138. return new LoginResult { JwtToken = new JwtResponse { Access_token=loginInfo.Token ,Scope=loginInfo.Scope}, CheckTicket = true };
  139. }
  140. else
  141. {
  142. throw new BizException(401, "Unauthorized");
  143. }
  144. }
  145. }
  146. public JwtResponse CreateJwtToken(LoginInfo loginInfo)
  147. {
  148. List<RoleUser> roots = BaseConfigModel.Configuration.GetSection("RoleUser:Root").Get<List<RoleUser>>();
  149. List<RoleUser> admins = BaseConfigModel.Configuration.GetSection("RoleUser:Admin").Get<List<RoleUser>>();
  150. string role = "";
  151. foreach (var roleUser in roots)
  152. {
  153. if (roleUser.Phone.Equals(loginInfo.CountryCode + loginInfo.Phone))
  154. {
  155. role = role + "Root,";
  156. break;
  157. }
  158. }
  159. foreach (var roleUser in admins)
  160. {
  161. if (roleUser.Phone.Equals(loginInfo.CountryCode + loginInfo.Phone))
  162. {
  163. role = role + "Admin,";
  164. break;
  165. }
  166. }
  167. role = role + "User";
  168. ClaimModel model = new ClaimModel
  169. {
  170. Scope = "WebApp"
  171. };
  172. model.Claims.Add(new Claim(JwtClaimTypes.Name, loginInfo.Name));
  173. model.Claims.Add(new Claim(JwtClaimTypes.Id, loginInfo.TeamModelId));
  174. ////保护隐私
  175. //model.Claims.Add(new Claim(JwtClaimTypes.PhoneNumber, loginInfo.Phone));
  176. model.Roles.Add(role);
  177. JwtResponse jwtResponse = JwtHelper.IssueJWT(model, _options.Value);
  178. return jwtResponse;
  179. }
  180. public Task<LoginInfo> SaveLoginInfoAsync(LoginInfo loginInfo)
  181. {
  182. return _repository.Save<LoginInfo>(loginInfo);
  183. }
  184. }
  185. }