LoginInfoService.cs 8.7 KB

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