Skip to content

Commit

Permalink
Merge pull request #4 from Ali-YousefiTelori/develop
Browse files Browse the repository at this point in the history
Support for LoginByPersonalAccessToken
  • Loading branch information
Ali-YousefiTelori authored Nov 26, 2023
2 parents 83fb39f + 6665937 commit 39ef9b8
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

<ItemGroup>
<PackageReference Include="EasyMicroservices.Configuration" Version="0.0.0.2" />
<PackageReference Include="EasyMicroservices.Cores.Relational.EntityFrameworkCore" Version="0.0.0.49" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.13" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace EasyMicroservices.IdentityMicroservice.Contracts.Requests
{
public class LoginByPersonalAccessTokenRequestContract
{
public string PersonalAccessToken { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="EasyMicroservices.Cores.AspEntityFrameworkCoreApi" Version="0.0.0.49" />
<PackageReference Include="EasyMicroservices.Cores.Database" Version="0.0.0.49" />
<PackageReference Include="EasyMicroservices.Cores.AspEntityFrameworkCoreApi" Version="0.0.0.53" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<PackageReference Include="EasyMicroservices.Mapper.CompileTimeMapper" Version="0.0.0.6" />
<PackageReference Include="EasyMicroservices.Mapper.SerializerMapper" Version="0.0.0.3" />
<PackageReference Include="EasyMicroservices.Serialization.Newtonsoft.Json" Version="0.0.0.6" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.0.3" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -37,5 +34,30 @@ public IJWTManager GetIJWTManager()
{
return new JWTManager(GetConfiguration());
}

T SetToken<T>(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<string>("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()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,7 +28,7 @@ public async Task<MessageContract<RegisterResponseContract>> 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!");
Expand All @@ -54,14 +47,15 @@ public async Task<MessageContract<RegisterResponseContract>> Register(Contracts.

public virtual async Task<MessageContract<LoginResponseContract>> 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<MessageContract<UserResponseContract>> GenerateToken(UserClaimContract cred)
Expand All @@ -79,6 +73,5 @@ public virtual async Task<MessageContract<UserResponseContract>> GenerateToken(U
Token = token.Result.Token
};
}

}
}
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<ItemGroup>
<PackageReference Include="EasyMicroservices.WhiteLabelsMicroservice.Clients" Version="0.0.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.14" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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;
Expand All @@ -28,24 +23,22 @@ public class IdentityController : ControllerBase
public IdentityController(IAppUnitOfWork appUnitOfWork)
{
_appUnitOfWork = appUnitOfWork;
_config = _appUnitOfWork.GetConfiguration();
_authRoot = _config.GetValue<string>("RootAddresses:Authentications");
_userClient = new(_authRoot, new System.Net.Http.HttpClient());
_identityHelper = _appUnitOfWork.GetIdentityHelper();
}

[HttpPost]
public async Task<ServiceContracts.MessageContract> 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");

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,
Expand Down Expand Up @@ -89,7 +82,8 @@ public async Task<MessageContract<UserResponseContract>> GenerateToken(UserClaim
[HttpPost]
public async Task<MessageContract<UserResponseContract>> 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
});
Expand All @@ -116,5 +110,32 @@ public async Task<MessageContract<UserResponseContract>> RegenerateToken(Regener

return user.ToContract<UserResponseContract>();
}

[HttpPost]
public async Task<MessageContract<UserResponseContract>> 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="EasyMicroservices.Cores.AspCoreApi" Version="0.0.0.49" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.13">
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.14">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down

0 comments on commit 39ef9b8

Please sign in to comment.