LoginInfoService.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 HttpClientService _httpClientService;
  34. public LoginInfoService(IAzureTableDBRepository repository, IOptions<JwtSetting> options, IHttpContextAccessor httpContextAccessor , HttpClientService 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. await _repository.Update<LoginInfo>(login);
  56. return result;
  57. }
  58. Dictionary<string, string> dict = new Dictionary<string, string>
  59. {
  60. { Constants.AUTHORIZATION, BaseConfigModel.Configuration["HaBookAuth:UserInfoKey"] }
  61. };
  62. JosnRPCRequest<Dictionary<string, object>> request = new JosnRPCRequest<Dictionary<string, object>>
  63. {
  64. method = "UserInfo"
  65. };
  66. Dictionary<string, object> ticket = new Dictionary<string, object>
  67. {
  68. { "ticket", ticketInfo.Ticket }
  69. };
  70. request.@params = ticket;
  71. string data = MessagePackHelper.ObjectToJson(request);
  72. string jsonStr = _httpClientService.HttpPost(BaseConfigModel.Configuration["HaBookAuth:AccountUrl"], data, dict, Constants.CONTENT_TYPE_JSON, 1000, Encoding.UTF8);
  73. if (!string.IsNullOrEmpty(jsonStr))
  74. {
  75. JosnRPCResponse<TeamModelIdInfo> response = MessagePackHelper.JsonToObject<JosnRPCResponse<TeamModelIdInfo>>(jsonStr);
  76. if (response.error == null && response != null)
  77. {
  78. result.CheckTicket = true;
  79. LoginInfo loginInfo = new LoginInfo
  80. {
  81. PartitionKey = response.result.cellphone,
  82. Phone = response.result.cellphone,
  83. RowKey = Guid.NewGuid().ToString(),
  84. TeamModelId = response.result.id,
  85. Name = response.result.name,
  86. Ticket = ticketInfo.Ticket,
  87. CountryCode = response.result.countryCode
  88. };
  89. TeamModelUser user= await _repository.FindOneByKey<TeamModelUser>("TeamModelId", response.result.id);
  90. if (user == null || string.IsNullOrEmpty(user.RowKey))
  91. {
  92. user = new TeamModelUser { RowKey = Guid.NewGuid().ToString(), PartitionKey = loginInfo.CountryCode ,RegisterTime=DateTimeHelper.ConvertToTimeStamp13(DateTime.Now) };
  93. }
  94. user.Cellphone = response.result.cellphone;
  95. user.NickName = response.result.name;
  96. if (string.IsNullOrEmpty(user.FullName)) {
  97. user.FullName = response.result.name;
  98. }
  99. user.TeamModelId = response.result.id;
  100. user.CountryCode = response.result.countryCode;
  101. JwtResponse jwtToken = CreateJwtToken(loginInfo);
  102. loginInfo.Token = jwtToken.Access_token;
  103. result.JwtToken = jwtToken;
  104. await _repository.Save<LoginInfo>(loginInfo);
  105. await _repository.SaveOrUpdate<TeamModelUser>(user);
  106. return result;
  107. }
  108. else
  109. {
  110. result.CheckTicket = false;
  111. return result;
  112. }
  113. }
  114. else
  115. {
  116. result.CheckTicket = false;
  117. return result;
  118. }
  119. }
  120. else
  121. {
  122. ClaimModel claimModel = JwtHelper.SerializeJWT(ticketInfo.Token);
  123. foreach (Claim claim in claimModel.Claims)
  124. {
  125. if ("exp".Equals(claim.Type))
  126. {
  127. var dateTime = DateTimeHelper.ConvertToTimeStamp10(DateTime.Now);
  128. var exp = claim.Value;
  129. if (dateTime > long.Parse(exp))
  130. {
  131. throw new BizException(401, "Unauthorized");
  132. }
  133. }
  134. }
  135. Dictionary<string, object> msp = new Dictionary<string, object>
  136. {
  137. { "Token", ticketInfo.Token }
  138. };
  139. LoginInfo loginInfo = _repository.FindOneByDict<LoginInfo>(msp).Result;
  140. if (loginInfo != null && !string.IsNullOrEmpty(loginInfo.Token))
  141. {
  142. return new LoginResult { JwtToken = new JwtResponse { Access_token=loginInfo.Token ,Scope=loginInfo.Scope}, CheckTicket = true };
  143. }
  144. else
  145. {
  146. throw new BizException(401, "Unauthorized");
  147. }
  148. }
  149. }
  150. public JwtResponse CreateJwtToken(LoginInfo loginInfo)
  151. {
  152. List<RoleUser> roots = BaseConfigModel.Configuration.GetSection("RoleUser:Root").Get<List<RoleUser>>();
  153. List<RoleUser> admins = BaseConfigModel.Configuration.GetSection("RoleUser:Admin").Get<List<RoleUser>>();
  154. string role = "";
  155. foreach (var roleUser in roots)
  156. {
  157. if (roleUser.Phone.Equals(loginInfo.CountryCode + loginInfo.Phone))
  158. {
  159. role = role + "Root,";
  160. break;
  161. }
  162. }
  163. foreach (var roleUser in admins)
  164. {
  165. if (roleUser.Phone.Equals(loginInfo.CountryCode + loginInfo.Phone))
  166. {
  167. role = role + "Admin,";
  168. break;
  169. }
  170. }
  171. role = role + "User";
  172. ClaimModel model = new ClaimModel
  173. {
  174. Scope = "WebApp"
  175. };
  176. model.Claims.Add(new Claim(JwtClaimTypes.Name, loginInfo.Name));
  177. model.Claims.Add(new Claim(JwtClaimTypes.Id, loginInfo.TeamModelId));
  178. model.Claims.Add(new Claim(JwtClaimTypes.PhoneNumber, loginInfo.Phone));
  179. model.Roles.Add(role);
  180. JwtResponse jwtResponse = JwtHelper.IssueJWT(model, _options.Value);
  181. return jwtResponse;
  182. }
  183. public Task<LoginInfo> SaveLoginInfoAsync(LoginInfo loginInfo)
  184. {
  185. return _repository.Save<LoginInfo>(loginInfo);
  186. }
  187. }
  188. }