Commit 5904697d authored by hasan khaddour's avatar hasan khaddour

Restructure infrastructure layer.

parent 5e798685
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using PSManagement.Application.Common.Services;
using PSManagement.Application.Contracts.Authentication;
using PSManagement.Infrastructure.Authentication;
using PSManagement.Infrastructure.Services;
using PSManagement.Infrastructure.Services.Authentication;
namespace PSManagement.Infrastructure.DI
{
public static class DependencyInjection
{
public static IServiceCollection AddInfrastructure(this IServiceCollection services)
public static IServiceCollection AddInfrastructure(this IServiceCollection services,IConfiguration configuration)
{
services.Configure<JwtSetting>(configuration.GetSection(JwtSetting.Section));
services.AddSingleton<IJwtTokenGenerator,JwtTokenGenerator>();
services.AddSingleton<IDateTimeProvider, DateTimeProvider>();
services.AddScoped<IAuthenticationService,AuthenticationService>();
return services;
}
......
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
......@@ -6,12 +6,19 @@
<ItemGroup>
<Folder Include="Migrations\" />
<Folder Include="EntitiesConfiguration\" />
<Folder Include="Persistence\EntitiesConfiguration\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.2" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="5.0.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.7.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\PSManagement.Application\PSManagement.Application.csproj" />
<ProjectReference Include="..\PSManagement.Contracts\PSManagement.Contracts.csproj" />
</ItemGroup>
</Project>
using PSManagement.Application.Contracts.Authentication;
using System;
using System.Threading.Tasks;
namespace PSManagement.Infrastructure.Services.Authentication
{
public class AuthenticationService :IAuthenticationService
{
private readonly IJwtTokenGenerator _jwtTokenGenerator;
public AuthenticationService(IJwtTokenGenerator jwtTokenGenerator)
{
_jwtTokenGenerator = jwtTokenGenerator;
}
public async Task<AuthenticationResult> Login(String email, String password) {
return new AuthenticationResult {
Id = Guid.NewGuid(),
Email=email,
FirstName="First name ",
LastName ="last Name ",
Token="token"
};
}
public async Task<AuthenticationResult> Register(String email, String firstName, String lastName, String password) {
// check if the user exist
Guid userId = Guid.NewGuid();
// generate token
String token = _jwtTokenGenerator.GenerateToken(userId,firstName,lastName,email);
return new AuthenticationResult
{
Id = Guid.NewGuid(),
Email = email,
FirstName =firstName,
LastName =lastName,
Token=token
};
}
}
}
using PSManagement.Application.Common.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PSManagement.Infrastructure.Services
{
public class DateTimeProvider : IDateTimeProvider
{
public DateTime UtcNow => DateTime.UtcNow;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PSManagement.Infrastructure.Authentication
{
public class JwtSetting
{
public const string Section = "JwtSettings";
public string Audience { get; set; } = null!;
public string Issuer { get; set; } = null!;
public string Secret { get; set; } = null!;
public int ExpireMinutes { get; set; }
}
}
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using PSManagement.Application.Common.Services;
using PSManagement.Application.Contracts.Authentication;
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;
namespace PSManagement.Infrastructure.Authentication
{
public class JwtTokenGenerator : IJwtTokenGenerator
{
private readonly IDateTimeProvider _dateTimeProvider;
private readonly JwtSetting _jwtSetting;
public JwtTokenGenerator(IDateTimeProvider dateTimeProvider, IOptions<JwtSetting> jwtOptions)
{
_dateTimeProvider = dateTimeProvider;
_jwtSetting = jwtOptions.Value;
}
public string GenerateToken(Guid id, string firstName, string lastName, string email)
{
var signingCredentials = new SigningCredentials(
new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtSetting.Secret)),
SecurityAlgorithms.HmacSha256
);
var claims = new[] {
new Claim(JwtRegisteredClaimNames.Sub,id.ToString()),
new Claim(JwtRegisteredClaimNames.Jti,Guid.NewGuid().ToString()),
new Claim(JwtRegisteredClaimNames.Email,email),
new Claim(JwtRegisteredClaimNames.GivenName,firstName),
new Claim(JwtRegisteredClaimNames.FamilyName,lastName)
};
var securityToken = new JwtSecurityToken(
issuer: _jwtSetting.Issuer,
audience:_jwtSetting.Audience,
expires: _dateTimeProvider.UtcNow.AddMinutes(_jwtSetting.ExpireMinutes),
claims : claims,
signingCredentials:signingCredentials);
return new JwtSecurityTokenHandler().WriteToken(securityToken);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment