123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- 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<JwtSetting> _options;
- private IHttpContextAccessor _httpContextAccessor;
- private HttpClientService _httpClientService;
- public LoginInfoService(IAzureTableDBRepository repository, IOptions<JwtSetting> options, IHttpContextAccessor httpContextAccessor , HttpClientService httpClientService)
- {
- _httpContextAccessor = httpContextAccessor;
- _options = options;
- _repository = repository;
- _httpClientService = httpClientService;
- }
- public async Task<LoginResult> 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<LoginInfo>("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<LoginInfo>(login);
- return result;
- }
- Dictionary<string, string> dict = new Dictionary<string, string>
- {
- { Constants.AUTHORIZATION, BaseConfigModel.Configuration["HaBookAuth:UserInfoKey"] }
- };
- JosnRPCRequest<Dictionary<string, object>> request = new JosnRPCRequest<Dictionary<string, object>>
- {
- method = "UserInfo"
- };
- Dictionary<string, object> ticket = new Dictionary<string, object>
- {
- { "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<TeamModelIdInfo> response = MessagePackHelper.JsonToObject<JosnRPCResponse<TeamModelIdInfo>>(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<TeamModelUser>("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>(loginInfo);
- await _repository.SaveOrUpdate<TeamModelUser>(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<string, object> msp = new Dictionary<string, object>
- {
- { "Token", ticketInfo.Token }
- };
- LoginInfo loginInfo = _repository.FindOneByDict<LoginInfo>(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<RoleUser> roots = BaseConfigModel.Configuration.GetSection("RoleUser:Root").Get<List<RoleUser>>();
- List<RoleUser> admins = BaseConfigModel.Configuration.GetSection("RoleUser:Admin").Get<List<RoleUser>>();
- 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<LoginInfo> SaveLoginInfoAsync(LoginInfo loginInfo)
- {
- return _repository.Save<LoginInfo>(loginInfo);
- }
- }
- }
|