From 666593743224e729b5f9b12f90ff00071556f7af Mon Sep 17 00:00:00 2001 From: Ali Yousefi Date: Sat, 25 Nov 2023 21:24:35 +0330 Subject: [PATCH] Support for LoginByPersonalAccessToken --- ...vices.IdentityMicroservice.Database.csproj | 2 - ...ginByPersonalAccessTokenRequestContract.cs | 7 +++ ...ervices.IdentityMicroservice.Domain.csproj | 3 +- ...ervices.IdentityMicroservice.Logics.csproj | 1 + .../Helpers/AppUnitOfWork.cs | 42 +++++++++++++---- .../Helpers/IdentityHelper.cs | 17 ++----- .../Helpers/JWTManager.cs | 15 +++--- .../Interfaces/IAppUnitOfWork.cs | 2 + ...rvices.IdentityMicroservice.StartUp.csproj | 2 +- .../Controllers/IdentityController.cs | 47 ++++++++++++++----- ...ervices.IdentityMicroservice.WebApi.csproj | 3 +- 11 files changed, 90 insertions(+), 51 deletions(-) create mode 100644 src/CSharp/EasyMicroservices.IdentityMicroservice.Domain/Contracts/Requests/LoginByPersonalAccessTokenRequestContract.cs diff --git a/src/CSharp/EasyMicroservices.IdentityMicroservice.Database/EasyMicroservices.IdentityMicroservice.Database.csproj b/src/CSharp/EasyMicroservices.IdentityMicroservice.Database/EasyMicroservices.IdentityMicroservice.Database.csproj index eb46b1e..3511202 100644 --- a/src/CSharp/EasyMicroservices.IdentityMicroservice.Database/EasyMicroservices.IdentityMicroservice.Database.csproj +++ b/src/CSharp/EasyMicroservices.IdentityMicroservice.Database/EasyMicroservices.IdentityMicroservice.Database.csproj @@ -12,8 +12,6 @@ - - diff --git a/src/CSharp/EasyMicroservices.IdentityMicroservice.Domain/Contracts/Requests/LoginByPersonalAccessTokenRequestContract.cs b/src/CSharp/EasyMicroservices.IdentityMicroservice.Domain/Contracts/Requests/LoginByPersonalAccessTokenRequestContract.cs new file mode 100644 index 0000000..2ab049b --- /dev/null +++ b/src/CSharp/EasyMicroservices.IdentityMicroservice.Domain/Contracts/Requests/LoginByPersonalAccessTokenRequestContract.cs @@ -0,0 +1,7 @@ +namespace EasyMicroservices.IdentityMicroservice.Contracts.Requests +{ + public class LoginByPersonalAccessTokenRequestContract + { + public string PersonalAccessToken { get; set; } + } +} diff --git a/src/CSharp/EasyMicroservices.IdentityMicroservice.Domain/EasyMicroservices.IdentityMicroservice.Domain.csproj b/src/CSharp/EasyMicroservices.IdentityMicroservice.Domain/EasyMicroservices.IdentityMicroservice.Domain.csproj index b8f577c..fea29dd 100644 --- a/src/CSharp/EasyMicroservices.IdentityMicroservice.Domain/EasyMicroservices.IdentityMicroservice.Domain.csproj +++ b/src/CSharp/EasyMicroservices.IdentityMicroservice.Domain/EasyMicroservices.IdentityMicroservice.Domain.csproj @@ -11,7 +11,6 @@ - - + diff --git a/src/CSharp/EasyMicroservices.IdentityMicroservice.Logics/EasyMicroservices.IdentityMicroservice.Logics.csproj b/src/CSharp/EasyMicroservices.IdentityMicroservice.Logics/EasyMicroservices.IdentityMicroservice.Logics.csproj index 7c7a8a3..19e602a 100644 --- a/src/CSharp/EasyMicroservices.IdentityMicroservice.Logics/EasyMicroservices.IdentityMicroservice.Logics.csproj +++ b/src/CSharp/EasyMicroservices.IdentityMicroservice.Logics/EasyMicroservices.IdentityMicroservice.Logics.csproj @@ -9,6 +9,7 @@ + diff --git a/src/CSharp/EasyMicroservices.IdentityMicroservice.Logics/Helpers/AppUnitOfWork.cs b/src/CSharp/EasyMicroservices.IdentityMicroservice.Logics/Helpers/AppUnitOfWork.cs index 037494d..7421080 100644 --- a/src/CSharp/EasyMicroservices.IdentityMicroservice.Logics/Helpers/AppUnitOfWork.cs +++ b/src/CSharp/EasyMicroservices.IdentityMicroservice.Logics/Helpers/AppUnitOfWork.cs @@ -1,17 +1,14 @@  -using Microsoft.Extensions.Configuration; -using System; -using EasyMicroservices.IdentityMicroservice; -using System.Threading.Tasks; -using System.Threading; -using Microsoft.AspNetCore.Http; -using System.Security.Cryptography.Xml; -using EasyMicroservices.IdentityMicroservice.Interfaces; +using Authentications.GeneratedServices; using EasyMicroservices.Cores.AspEntityFrameworkCoreApi; -using EasyMicroservices.IdentityMicroservice.Helpers; +using EasyMicroservices.Cores.Clients; +using EasyMicroservices.IdentityMicroservice.Interfaces; using EasyMicroservices.IdentityMicroservice.Services; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Authentications.GeneratedServices; +using System; +using System.Linq; namespace EasyMicroservices.IdentityMicroservice.Helpers { @@ -37,5 +34,30 @@ public IJWTManager GetIJWTManager() { return new JWTManager(GetConfiguration()); } + + T SetToken(HttpContext httpContext, T coreSwaggerClient) + where T : CoreSwaggerClientBase + { + if (httpContext.Request.Headers.Authorization.Count > 0) + { + coreSwaggerClient.SetBearerToken(httpContext.Request.Headers.Authorization.First()); + } + return coreSwaggerClient; + } + + string GetRouteAddress() + { + return GetConfiguration().GetValue("RootAddresses:Authentications"); + } + + public UserClient GetUserClient(HttpContext httpContext) + { + return SetToken(httpContext, new UserClient(GetRouteAddress(), new System.Net.Http.HttpClient())); + } + + public RoleClient GetRoleClient(HttpContext httpContext) + { + return SetToken(httpContext, new RoleClient(GetRouteAddress(), new System.Net.Http.HttpClient())); + } } } diff --git a/src/CSharp/EasyMicroservices.IdentityMicroservice.Logics/Helpers/IdentityHelper.cs b/src/CSharp/EasyMicroservices.IdentityMicroservice.Logics/Helpers/IdentityHelper.cs index 07675f9..500af23 100644 --- a/src/CSharp/EasyMicroservices.IdentityMicroservice.Logics/Helpers/IdentityHelper.cs +++ b/src/CSharp/EasyMicroservices.IdentityMicroservice.Logics/Helpers/IdentityHelper.cs @@ -4,13 +4,6 @@ using EasyMicroservices.IdentityMicroservice.Interfaces; using EasyMicroservices.ServiceContracts; using Microsoft.Extensions.Configuration; -using Microsoft.IdentityModel.Tokens; -using System; -using System.Collections.Generic; -using System.IdentityModel.Tokens.Jwt; -using System.Linq; -using System.Runtime.CompilerServices; -using System.Text; using System.Threading.Tasks; namespace EasyMicroservices.IdentityMicroservice.Helpers @@ -35,7 +28,7 @@ public async Task> Register(Contracts. { request.Password = await SecurityHelper.HashPassword(request.Password); - var usersRecords = await _userClient.GetUserByUserNameAsync(new GetUserByUserNameRequestContract { Username = request.UserName.ToLower()}); + var usersRecords = await _userClient.GetUserByUserNameAsync(new GetUserByUserNameRequestContract { Username = request.UserName.ToLower() }); if (usersRecords.IsSuccess) return (ServiceContracts.FailedReasonType.Duplicate, "User already exists!"); @@ -54,14 +47,15 @@ public async Task> Register(Contracts. public virtual async Task> Login(Contracts.Common.UserSummaryContract cred) { - var user = await _userClient.VerifyUserIdentityAsync(new Authentications.GeneratedServices.UserSummaryContract { UserName = cred.UserName, Password = cred.Password}); + var user = await _userClient.VerifyUserIdentityAsync(new Authentications.GeneratedServices.UserSummaryContract { UserName = cred.UserName, Password = cred.Password }); if (!user.IsSuccess) return (ServiceContracts.FailedReasonType.Incorrect, "Username or password is invalid."); //"Username or password is invalid." - return new LoginResponseContract { + return new LoginResponseContract + { UserId = user.Result.Id - }; + }; } public virtual async Task> GenerateToken(UserClaimContract cred) @@ -79,6 +73,5 @@ public virtual async Task> GenerateToken(U Token = token.Result.Token }; } - } } diff --git a/src/CSharp/EasyMicroservices.IdentityMicroservice.Logics/Helpers/JWTManager.cs b/src/CSharp/EasyMicroservices.IdentityMicroservice.Logics/Helpers/JWTManager.cs index 08ff191..a5ecadd 100644 --- a/src/CSharp/EasyMicroservices.IdentityMicroservice.Logics/Helpers/JWTManager.cs +++ b/src/CSharp/EasyMicroservices.IdentityMicroservice.Logics/Helpers/JWTManager.cs @@ -1,29 +1,26 @@ -using EasyMicroservices.IdentityMicroservice.Contracts.Requests; -using EasyMicroservices.IdentityMicroservice.Helpers; +using Authentications.GeneratedServices; +using EasyMicroservices.IdentityMicroservice.Contracts.Common; +using EasyMicroservices.IdentityMicroservice.Contracts.Requests; +using EasyMicroservices.IdentityMicroservice.Contracts.Responses; using EasyMicroservices.IdentityMicroservice.Interfaces; -using EasyMicroservices.Cores.AspEntityFrameworkCoreApi.Interfaces; -using EasyMicroservices.Cores.Database.Interfaces; using EasyMicroservices.ServiceContracts; using Microsoft.Extensions.Configuration; using Microsoft.IdentityModel.Tokens; using System; +using System.Collections.Generic; using System.IdentityModel.Tokens.Jwt; using System.Linq; using System.Security.Claims; using System.Text; using System.Threading.Tasks; -using Authentications.GeneratedServices; using FailedReasonType = EasyMicroservices.ServiceContracts.FailedReasonType; -using System.Collections.Generic; -using EasyMicroservices.IdentityMicroservice.Contracts.Responses; -using EasyMicroservices.IdentityMicroservice.Contracts.Common; namespace EasyMicroservices.IdentityMicroservice.Services { public class JWTManager : IJWTManager { private readonly IConfiguration _config; - private readonly UsersClient _userClient; + private readonly UserClient _userClient; private readonly string _authRoot; public JWTManager(IConfiguration config) diff --git a/src/CSharp/EasyMicroservices.IdentityMicroservice.Logics/Interfaces/IAppUnitOfWork.cs b/src/CSharp/EasyMicroservices.IdentityMicroservice.Logics/Interfaces/IAppUnitOfWork.cs index 450bcaf..0752441 100644 --- a/src/CSharp/EasyMicroservices.IdentityMicroservice.Logics/Interfaces/IAppUnitOfWork.cs +++ b/src/CSharp/EasyMicroservices.IdentityMicroservice.Logics/Interfaces/IAppUnitOfWork.cs @@ -17,5 +17,7 @@ public interface IAppUnitOfWork : IUnitOfWork public IJWTManager GetIJWTManager(); public IConfiguration GetConfiguration(); public IdentityHelper GetIdentityHelper(); + public UserClient GetUserClient(HttpContext httpContext); + public RoleClient GetRoleClient(HttpContext httpContext); } } diff --git a/src/CSharp/EasyMicroservices.IdentityMicroservice.StartUp/EasyMicroservices.IdentityMicroservice.StartUp.csproj b/src/CSharp/EasyMicroservices.IdentityMicroservice.StartUp/EasyMicroservices.IdentityMicroservice.StartUp.csproj index 5313c30..6695189 100644 --- a/src/CSharp/EasyMicroservices.IdentityMicroservice.StartUp/EasyMicroservices.IdentityMicroservice.StartUp.csproj +++ b/src/CSharp/EasyMicroservices.IdentityMicroservice.StartUp/EasyMicroservices.IdentityMicroservice.StartUp.csproj @@ -7,7 +7,7 @@ - + diff --git a/src/CSharp/EasyMicroservices.IdentityMicroservice.WebApi/Controllers/IdentityController.cs b/src/CSharp/EasyMicroservices.IdentityMicroservice.WebApi/Controllers/IdentityController.cs index 6de703d..228727c 100644 --- a/src/CSharp/EasyMicroservices.IdentityMicroservice.WebApi/Controllers/IdentityController.cs +++ b/src/CSharp/EasyMicroservices.IdentityMicroservice.WebApi/Controllers/IdentityController.cs @@ -1,15 +1,11 @@ -using Authentications.GeneratedServices; -using EasyMicroservices.Cores.AspCoreApi; -using EasyMicroservices.Cores.AspEntityFrameworkCoreApi; -using EasyMicroservices.Cores.AspEntityFrameworkCoreApi.Interfaces; -using EasyMicroservices.Cores.Database.Interfaces; -using EasyMicroservices.IdentityMicroservice.Contracts.Common; +using EasyMicroservices.IdentityMicroservice.Contracts.Common; using EasyMicroservices.IdentityMicroservice.Contracts.Requests; using EasyMicroservices.IdentityMicroservice.Contracts.Responses; using EasyMicroservices.IdentityMicroservice.Helpers; using EasyMicroservices.IdentityMicroservice.Interfaces; using EasyMicroservices.ServiceContracts; using Microsoft.AspNetCore.Mvc; +using System.Security.Claims; namespace EasyMicroservices.IdentityMicroservice.WebApi.Controllers { @@ -19,7 +15,6 @@ namespace EasyMicroservices.IdentityMicroservice.WebApi.Controllers public class IdentityController : ControllerBase { private readonly IConfiguration _config; - private readonly UsersClient _userClient; private readonly IJWTManager _jwtManager; private readonly IdentityHelper _identityHelper; private readonly IAppUnitOfWork _appUnitOfWork; @@ -28,16 +23,14 @@ public class IdentityController : ControllerBase public IdentityController(IAppUnitOfWork appUnitOfWork) { _appUnitOfWork = appUnitOfWork; - _config = _appUnitOfWork.GetConfiguration(); - _authRoot = _config.GetValue("RootAddresses:Authentications"); - _userClient = new(_authRoot, new System.Net.Http.HttpClient()); _identityHelper = _appUnitOfWork.GetIdentityHelper(); } [HttpPost] public async Task VerifyUserName(VerifyUserRequestContract request) { - var user = await _userClient.GetByIdAsync(new Int64GetIdRequestContract { Id = request.UserId }); + var _userClient = _appUnitOfWork.GetUserClient(HttpContext); + var user = await _userClient.GetByIdAsync(new Authentications.GeneratedServices.Int64GetIdRequestContract { Id = request.UserId }); if (!user.IsSuccess) return (ServiceContracts.FailedReasonType.NotFound, "User not found"); @@ -45,7 +38,7 @@ public IdentityController(IAppUnitOfWork appUnitOfWork) if (user.Result.IsUsernameVerified) return true; - await _userClient.UpdateAsync(new UserContract + await _userClient.UpdateAsync(new Authentications.GeneratedServices.UserContract { CreationDateTime = user.Result.CreationDateTime, DeletedDateTime = user.Result.DeletedDateTime, @@ -89,7 +82,8 @@ public async Task> GenerateToken(UserClaim [HttpPost] public async Task> RegenerateToken(RegenerateTokenContract request) { - var user = await _userClient.GetByIdAsync(new Int64GetIdRequestContract + var _userClient = _appUnitOfWork.GetUserClient(HttpContext); + var user = await _userClient.GetByIdAsync(new Authentications.GeneratedServices.Int64GetIdRequestContract { Id = request.UserId }); @@ -116,5 +110,32 @@ public async Task> RegenerateToken(Regener return user.ToContract(); } + + [HttpPost] + public async Task> LoginByPersonalAccessToken(LoginByPersonalAccessTokenRequestContract request) + { + var user = await _appUnitOfWork.GetUserClient(HttpContext).GetUserByPersonalAccessTokenAsync(new Authentications.GeneratedServices.PersonalAccessTokenRequestContract() + { + Value = request.PersonalAccessToken + }).AsCheckedResult(x => x.Result); + + var roles = await _appUnitOfWork.GetRoleClient(HttpContext).GetRolesByUserIdAsync(new Authentications.GeneratedServices.Int64GetIdRequestContract + { + Id = user.Id + }).AsCheckedResult(x => x.Result); + + var response = await _identityHelper.GenerateToken(new UserClaimContract() + { + UserName = user.UserName, + Password = user.Password, + Claims = roles.Select(x => new ClaimContract() + { + Name = ClaimTypes.Role, + Value = x.Name + }).ToList() + }); + + return response; + } } } diff --git a/src/CSharp/EasyMicroservices.IdentityMicroservice.WebApi/EasyMicroservices.IdentityMicroservice.WebApi.csproj b/src/CSharp/EasyMicroservices.IdentityMicroservice.WebApi/EasyMicroservices.IdentityMicroservice.WebApi.csproj index dfd6962..c3bdf0a 100644 --- a/src/CSharp/EasyMicroservices.IdentityMicroservice.WebApi/EasyMicroservices.IdentityMicroservice.WebApi.csproj +++ b/src/CSharp/EasyMicroservices.IdentityMicroservice.WebApi/EasyMicroservices.IdentityMicroservice.WebApi.csproj @@ -6,8 +6,7 @@ - - + all runtime; build; native; contentfiles; analyzers; buildtransitive