|
@@ -0,0 +1,233 @@
|
|
|
+using IdentityModel;
|
|
|
+using Microsoft.AspNetCore.Authorization;
|
|
|
+using Microsoft.AspNetCore.Mvc;
|
|
|
+using Microsoft.Extensions.Options;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Linq;
|
|
|
+using System.Security.Claims;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using TEAMModelOS.Models;
|
|
|
+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.JwtAuth.JwtHelper;
|
|
|
+using TEAMModelOS.SDK.Extension.JwtAuth.Models;
|
|
|
+using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
|
|
|
+using TEAMModelOS.SDK.Helper.Common.DateTimeHelper;
|
|
|
+using TEAMModelOS.SDK.Helper.Common.ValidateHelper;
|
|
|
+using TEAMModelOS.SDK.Module.AzureCosmosDB.Interfaces;
|
|
|
+using TEAMModelOS.SDK.Module.AzureTable.Interfaces;
|
|
|
+using TEAMModelOS.Service.Models.Core;
|
|
|
+
|
|
|
+namespace TEAMModelOS.Controllers.Core
|
|
|
+{
|
|
|
+ public class LoginController
|
|
|
+ {
|
|
|
+ private readonly IAzureTableDBRepository azureTableDBRepository;
|
|
|
+ private readonly IAzureCosmosDBRepository azureCosmosDBRepository;
|
|
|
+ private readonly IOptions<JwtSetting> options;
|
|
|
+ public LoginController(IAzureTableDBRepository _azureTableDBRepository, IAzureCosmosDBRepository _azureCosmosDBRepository, IOptions<JwtSetting> _options)
|
|
|
+ {
|
|
|
+ azureTableDBRepository = _azureTableDBRepository;
|
|
|
+ azureCosmosDBRepository = _azureCosmosDBRepository;
|
|
|
+ options = _options;
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 登录验证
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="test"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ [HttpPost("CheckLogin")]
|
|
|
+ [AllowAnonymous]
|
|
|
+ public async Task<BaseJosnRPCResponse> CheckLogin(JosnRPCRequest<TicketInfo> request)
|
|
|
+ {
|
|
|
+ JsonRPCResponseBuilder responseBuilder = JsonRPCResponseBuilder.custom();
|
|
|
+
|
|
|
+ if (ValidateHelper.IsValid(request.@params))
|
|
|
+ {
|
|
|
+ LoginResult result = await CheckLoginAsync(request.@params);
|
|
|
+ if (result.CheckTicket)
|
|
|
+ {
|
|
|
+ responseBuilder.Data(result);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ responseBuilder.Error(false, ResponseCode.FAILED, "ticket error!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ responseBuilder.Error(false, ResponseCode.FAILED, "ticket error!");
|
|
|
+ }
|
|
|
+ return responseBuilder.build();
|
|
|
+ }
|
|
|
+
|
|
|
+ public async Task<LoginResult> CheckLoginAsync(TicketInfo ticketInfo)
|
|
|
+ {
|
|
|
+
|
|
|
+ // string jtoken = HttpContextHelper.GetValueInHttp(_httpContextAccessor.HttpContext.Request, Constants.AUTHORIZATION);
|
|
|
+ if (string.IsNullOrEmpty(ticketInfo.Token))
|
|
|
+ {
|
|
|
+ LoginResult result = new LoginResult();
|
|
|
+ if (string.IsNullOrEmpty(ticketInfo.Ticket))
|
|
|
+ {
|
|
|
+ result.CheckTicket = false;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ LoginInfo login = azureTableDBRepository.FindOneByKey<LoginInfo>("Ticket", ticketInfo.Ticket).Result;
|
|
|
+ if (login != null && !string.IsNullOrEmpty(login.Token))
|
|
|
+ {
|
|
|
+ result.CheckTicket = true;
|
|
|
+ JwtResponse token = await CreateJwtToken(login);
|
|
|
+ result.JwtToken = token;
|
|
|
+ login.Token = token.Access_token;
|
|
|
+ result.JwtToken.Scope = login.Scope;
|
|
|
+ await azureTableDBRepository.Update<LoginInfo>(login);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ ///不验证TmdID
|
|
|
+ #region
|
|
|
+ result.CheckTicket = true;
|
|
|
+ LoginInfo loginInfo = new LoginInfo
|
|
|
+ {
|
|
|
+ PartitionKey = "15283771540",
|
|
|
+ Phone = "15283771540",
|
|
|
+ RowKey = Guid.NewGuid().ToString(),
|
|
|
+ TeamModelId = ticketInfo.TEAMModelId,
|
|
|
+ Name = ticketInfo.Name,
|
|
|
+ Ticket = ticketInfo.Ticket,
|
|
|
+ CountryCode = "86"
|
|
|
+ };
|
|
|
+ List<Teacher> users = await azureCosmosDBRepository.FindByDict<Teacher>(new Dictionary<string, object>() { { "TEAMModelId", ticketInfo.TEAMModelId } }, true);
|
|
|
+ Teacher user;
|
|
|
+ if (users.IsEmpty())
|
|
|
+ {
|
|
|
+ user = new Teacher { id = ticketInfo.TEAMModelId.Replace("#", ""), countryCode = loginInfo.CountryCode, createDate = DateTimeOffset.UtcNow.Ticks };
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ user = users[0];
|
|
|
+ }
|
|
|
+ user.cellphone = "15283771540";
|
|
|
+ user.name = ticketInfo.Name;
|
|
|
+ user.TEAMModelId = ticketInfo.TEAMModelId;
|
|
|
+ user.countryCode = "86";
|
|
|
+ JwtResponse jwtToken = await CreateJwtToken(loginInfo);
|
|
|
+ loginInfo.Token = jwtToken.Access_token;
|
|
|
+ loginInfo.Scope = jwtToken.Scope;
|
|
|
+ result.JwtToken = jwtToken;
|
|
|
+ await azureTableDBRepository.Save<LoginInfo>(loginInfo);
|
|
|
+ await azureCosmosDBRepository.SaveAll<Teacher>(new List<Teacher>() { user });
|
|
|
+ // JosnRPCResponse<TeamModelIdInfo> response = MessagePackHelper.JsonToObject<JosnRPCResponse<TeamModelIdInfo>>(jsonStr);
|
|
|
+ return result;
|
|
|
+ #endregion
|
|
|
+ /*
|
|
|
+ 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, Constants.CONTENT_TYPE_JSON, 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 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 = await CreateJwtToken(loginInfo);
|
|
|
+ loginInfo.Token = jwtToken.Access_token;
|
|
|
+ loginInfo.Scope = jwtToken.Scope;
|
|
|
+ result.JwtToken = jwtToken;
|
|
|
+ await Save<LoginInfo>(loginInfo);
|
|
|
+ await SaveOrUpdate<TeamModelUser>(user);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ result.CheckTicket = false;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ result.CheckTicket = false;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ */
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ ClaimModel claimModel = JwtHelper.SerializeJWT(ticketInfo.Token);
|
|
|
+
|
|
|
+ var dateTime = DateTimeHelper.ConvertToTimeStamp10(DateTime.Now);
|
|
|
+ var expExt = claimModel.Claim.TryGetValue("exp", out var exp);
|
|
|
+ if (expExt == false || dateTime > long.Parse(exp.ToString()))
|
|
|
+ {
|
|
|
+ throw new BizException(401, "Unauthorized");
|
|
|
+ }
|
|
|
+
|
|
|
+ Dictionary<string, object> msp = new Dictionary<string, object>
|
|
|
+ {
|
|
|
+ { "Token", ticketInfo.Token }
|
|
|
+ };
|
|
|
+ LoginInfo loginInfo = azureTableDBRepository.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 async Task<JwtResponse> CreateJwtToken(LoginInfo loginInfo)
|
|
|
+ {
|
|
|
+ string role = "Teacher";
|
|
|
+ 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.Claims.AddRange(role.Split(',').Select(s => new Claim(JwtClaimTypes.Role, s)));
|
|
|
+ model.Roles.Add(role);
|
|
|
+ JwtResponse jwtResponse = JwtHelper.IssueJWT(model, options.Value);
|
|
|
+ return jwtResponse;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|