Skip to content

Commit

Permalink
Merge pull request #25 from MahdiyarGHD/develop
Browse files Browse the repository at this point in the history
Deimplement token generation and Identity services
  • Loading branch information
Ali-YousefiTelori authored Nov 22, 2023
2 parents 5ec7f59 + 18a44c6 commit c1e82f8
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 1,215 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.0.0.10</Version>
<Version>0.0.0.13</Version>
<Description>client generated code.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>microservice,auth,authentication,client</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyMicroservices.AuthenticationsMicroservice.Contracts.Requests
{
public class GetUserByUserNameRequestContract
{
public string Username { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

<ItemGroup>
<Folder Include="DatabaseLogics\" />
<Folder Include="Interfaces\" />
<Folder Include="Mappers\" />
</ItemGroup>

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public DatabaseBuilder(IConfiguration configuration)

public void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
//optionsBuilder.UseInMemoryDatabase("AuthDb");
optionsBuilder.UseSqlServer(_configuration.GetConnectionString("local"));
optionsBuilder.UseInMemoryDatabase("AuthDb");
//optionsBuilder.UseSqlServer(_configuration.GetConnectionString("local"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using EasyMicroservices.AuthenticationsMicroservice.Contracts.Responses;
using EasyMicroservices.AuthenticationsMicroservice.Database.Entities;
using EasyMicroservices.AuthenticationsMicroservice.Helpers;
using EasyMicroservices.AuthenticationsMicroservice.Interfaces;
using EasyMicroservices.Cores.AspCoreApi;
using EasyMicroservices.Cores.AspEntityFrameworkCoreApi.Interfaces;
using EasyMicroservices.ServiceContracts;
Expand All @@ -15,104 +14,24 @@ namespace EasyMicroservices.AuthenticationsMicroservice.WebApi.Controllers
public class UsersController : SimpleQueryServiceController<UserEntity, AddUserRequestContract, UserContract, UserContract, long>
{
private readonly IUnitOfWork _unitOfWork;
private readonly IJWTManager _jwtManager;

public UsersController(IUnitOfWork unitOfWork, IJWTManager jwtManager) : base(unitOfWork)
public UsersController(IUnitOfWork unitOfWork) : base(unitOfWork)
{
_unitOfWork = unitOfWork;
_jwtManager = jwtManager;
}

[HttpPost]
public async Task<MessageContract<bool>> VerifyUserName(VerifyEmailAddressContract request)
public async Task<MessageContract<UserContract>> GetUserByUserName(GetUserByUserNameRequestContract request)
{
var logic = _unitOfWork.GetLongContractLogic<UserEntity, AddUserRequestContract, UserContract, UserContract>();
var user = await logic.GetById(new Cores.Contracts.Requests.GetIdRequestContract<long> { Id = request.UserId }).AsCheckedResult();
if (user.IsUsernameVerified)
return true;

await logic.Update(new UserContract
{
CreationDateTime = user.CreationDateTime,
DeletedDateTime = user.DeletedDateTime,
Id = user.Id,
IsDeleted = user.IsDeleted,
IsUsernameVerified = true,
ModificationDateTime = user.ModificationDateTime,
Password = user.Password,
UniqueIdentity = user.UniqueIdentity,
UserName = user.UserName,
}).AsCheckedResult();

return true;

}
[HttpPost]
public async Task<MessageContract<long>> Register(AddUserRequestContract request)
{
return await _jwtManager.Register(request);
}

[HttpPost]
public async Task<MessageContract<long>> Login(UserSummaryContract request)
{
string password = await AuthenticationHelper.HashPassword(request.Password);
request.Password = password;

var response = await _jwtManager.Login(request);

return response;
}

[HttpPost]
public async Task<MessageContract<UserResponseContract>> GenerateToken(UserClaimContract request)
{
string password = await AuthenticationHelper.HashPassword(request.Password);
request.Password = password;

var response = await _jwtManager.GenerateToken(request);

return response;
return await _unitOfWork.GetContractLogic<UserEntity, AddUserRequestContract, UserContract, UserContract, long>().GetBy(x => x.UserName == request.Username);
}

[HttpPost]
public async Task<MessageContract<UserResponseContract>> RegenerateToken(RegenerateTokenContract request)
{
var logic = _unitOfWork.GetLongContractLogic<UserEntity, AddUserRequestContract, UserContract, UserContract>();
var user = await logic.GetById(new Cores.Contracts.Requests.GetIdRequestContract<long>
{
Id = request.UserId
});

if (user)
{

string password = user.Result.Password;

var req = new UserClaimContract
{
Password = password,
UserName = user.Result.UserName,
Claims = request.Claims
};

var response = await _jwtManager.GenerateToken(req);

return new UserResponseContract
{
Token = response.Result.Token
};
}

return user.ToContract<UserResponseContract>();
}

[HttpGet]
[Authorize]
public string Test()
public async Task<MessageContract<UserContract>> VerifyUserIdentity(UserSummaryContract request)
{
return "test";
return await _unitOfWork.GetContractLogic<UserEntity, AddUserRequestContract, UserContract, UserContract, long>().GetBy(x => x.UserName == request.UserName && x.Password == request.Password);
}


}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using EasyMicroservices.AuthenticationsMicroservice.Database.Contexts;
using EasyMicroservices.AuthenticationsMicroservice.Interfaces;
using EasyMicroservices.Cores.AspEntityFrameworkCoreApi;
using EasyMicroservices.Cores.AspEntityFrameworkCoreApi.Interfaces;
using EasyMicroservices.Cores.Interfaces;
Expand Down Expand Up @@ -73,7 +72,7 @@ static WebApplicationBuilder CreateBuilder(string[] args)
});
});

app.Services.AddScoped<IJWTManager, JWTManager>();
//app.Services.AddScoped<IJWTManager, JWTManager>();

app.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
Expand Down

0 comments on commit c1e82f8

Please sign in to comment.