using IdentityModel; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Options; using System; using System.Collections.Generic; using System.Security.Claims; using System.Text; using System.Threading.Tasks; using TEAMModelOS.Model.Common.Dtos; using TEAMModelOS.Model.Common.Models; using TEAMModelOS.SDK.Context.Configuration; using TEAMModelOS.SDK.Context.Constant.Common; using TEAMModelOS.SDK.Context.Exception; using TEAMModelOS.SDK.Extension.DataResult.JsonRpcRequest; using TEAMModelOS.SDK.Extension.DataResult.JsonRpcResponse; using TEAMModelOS.SDK.Extension.HttpClient.Implements; using TEAMModelOS.SDK.Extension.JwtAuth.JwtHelper; using TEAMModelOS.SDK.Extension.JwtAuth.Models; using TEAMModelOS.SDK.Helper.Common.DateTimeHelper; using TEAMModelOS.SDK.Helper.Common.JsonHelper; using TEAMModelOS.SDK.Helper.Network.HttpHelper; using TEAMModelOS.SDK.Helper.Security.BCryptHelper; using TEAMModelOS.SDK.Module.AzureTable.Interfaces; using TEAMModelOS.Service.Common.Interfaces; namespace TEAMModelOS.Service.Common.Implements { public class LoginInfoService : ILoginInfoService { private IAzureTableDBRepository _repository; private IOptions _options; private IHttpContextAccessor _httpContextAccessor; private HttpClientService _httpClientService; public LoginInfoService(IAzureTableDBRepository repository, IOptions options, IHttpContextAccessor httpContextAccessor , HttpClientService httpClientService) { _httpContextAccessor = httpContextAccessor; _options = options; _repository = repository; _httpClientService = httpClientService; } public async Task CheckLoginAsync(TicketInfo ticketInfo) { string jtoken = HttpContextHelper.GetValueInHttp(_httpContextAccessor.HttpContext.Request, Constants.AUTHORIZATION); if (string.IsNullOrEmpty(ticketInfo.Token)) { string code = BCryptHelper.Ecrypt(ticketInfo.Ticket + ticketInfo.TeamModelId); bool f = BCryptHelper.Verify(ticketInfo.Ticket + ticketInfo.TeamModelId, ticketInfo.Sign); LoginResult result = new LoginResult(); LoginInfo login = _repository.FindOneByKey("Ticket", ticketInfo.Ticket).Result; if (login != null && !string.IsNullOrEmpty(login.Token)) { result.CheckTicket = true; JwtResponse token = CreateJwtToken(login); result.JwtToken = token; login.Token = token.Access_token; await _repository.Update(login); return result; } Dictionary dict = new Dictionary { { Constants.AUTHORIZATION, BaseConfigModel.Configuration["HaBookAuth:UserInfoKey"] } }; JosnRPCRequest> request = new JosnRPCRequest> { method = "UserInfo" }; Dictionary ticket = new Dictionary { { "ticket", ticketInfo.Ticket } }; request.@params = ticket; string data = MessagePackHelper.ObjectToJson(request); string jsonStr = _httpClientService.HttpPost(BaseConfigModel.Configuration["HaBookAuth:AccountUrl"], data, dict, Constants.CONTENT_TYPE_JSON, 1000, Encoding.UTF8); if (!string.IsNullOrEmpty(jsonStr)) { JosnRPCResponse response = MessagePackHelper.JsonToObject>(jsonStr); if (response.error == null && response != null) { result.CheckTicket = true; LoginInfo loginInfo = new LoginInfo { PartitionKey = response.result.cellphone, Phone = response.result.cellphone, RowKey = Guid.NewGuid().ToString(), TeamModelId = response.result.id, Name = response.result.name, Ticket = ticketInfo.Ticket, CountryCode = response.result.countryCode }; TeamModelUser user= await _repository.FindOneByKey("TeamModelId", response.result.id); if (user == null || string.IsNullOrEmpty(user.RowKey)) { user = new TeamModelUser { RowKey = Guid.NewGuid().ToString(), PartitionKey = loginInfo.CountryCode ,RegisterTime=DateTimeHelper.ConvertToTimeStamp13(DateTime.Now) }; } user.Cellphone = response.result.cellphone; user.NickName = response.result.name; if (string.IsNullOrEmpty(user.FullName)) { user.FullName = response.result.name; } user.TeamModelId = response.result.id; user.CountryCode = response.result.countryCode; JwtResponse jwtToken = CreateJwtToken(loginInfo); loginInfo.Token = jwtToken.Access_token; result.JwtToken = jwtToken; await _repository.Save(loginInfo); await _repository.SaveOrUpdate(user); return result; } else { result.CheckTicket = false; return result; } } else { result.CheckTicket = false; return result; } } else { ClaimModel claimModel = JwtHelper.SerializeJWT(ticketInfo.Token); foreach (Claim claim in claimModel.Claims) { if ("exp".Equals(claim.Type)) { var dateTime = DateTimeHelper.ConvertToTimeStamp10(DateTime.Now); var exp = claim.Value; if (dateTime > long.Parse(exp)) { throw new BizException(401, "Unauthorized"); } } } Dictionary msp = new Dictionary { { "Token", ticketInfo.Token } }; LoginInfo loginInfo = _repository.FindOneByDict(msp).Result; if (loginInfo != null && !string.IsNullOrEmpty(loginInfo.Token)) { return new LoginResult { JwtToken = new JwtResponse { Access_token=loginInfo.Token ,Scope=loginInfo.Scope}, CheckTicket = true }; } else { throw new BizException(401, "Unauthorized"); } } } public JwtResponse CreateJwtToken(LoginInfo loginInfo) { List roots = BaseConfigModel.Configuration.GetSection("RoleUser:Root").Get>(); List admins = BaseConfigModel.Configuration.GetSection("RoleUser:Admin").Get>(); string role = ""; foreach (var roleUser in roots) { if (roleUser.Phone.Equals(loginInfo.CountryCode + loginInfo.Phone)) { role = role + "Root,"; break; } } foreach (var roleUser in admins) { if (roleUser.Phone.Equals(loginInfo.CountryCode + loginInfo.Phone)) { role = role + "Admin,"; break; } } role = role + "User"; ClaimModel model = new ClaimModel { Scope = "WebApp" }; model.Claims.Add(new Claim(JwtClaimTypes.Name, loginInfo.Name)); model.Claims.Add(new Claim(JwtClaimTypes.Id, loginInfo.TeamModelId)); model.Claims.Add(new Claim(JwtClaimTypes.PhoneNumber, loginInfo.Phone)); model.Roles.Add(role); JwtResponse jwtResponse = JwtHelper.IssueJWT(model, _options.Value); return jwtResponse; } public Task SaveLoginInfoAsync(LoginInfo loginInfo) { return _repository.Save(loginInfo); } } }