Services, service registration and dependency injection for auth
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
using System.Security.Claims;
|
||||
using AipsCore.Application.Abstract.UserContext;
|
||||
using AipsCore.Domain.Models.User.ValueObjects;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace AipsCore.Infrastructure.Persistence.Authentication;
|
||||
|
||||
public class HttpUserContext : IUserContext
|
||||
{
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
|
||||
public HttpUserContext(IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
}
|
||||
|
||||
public UserId GetCurrentUserId()
|
||||
{
|
||||
var user = _httpContextAccessor.HttpContext?.User;
|
||||
|
||||
if (user is null || !user.Identity!.IsAuthenticated)
|
||||
{
|
||||
throw new UnauthorizedAccessException("User is not authenticated");
|
||||
}
|
||||
|
||||
var userIdClaim = user.FindFirst(ClaimTypes.NameIdentifier);
|
||||
|
||||
if (userIdClaim is null)
|
||||
{
|
||||
throw new UnauthorizedAccessException("User id claim not found");
|
||||
}
|
||||
|
||||
return new UserId(userIdClaim.Value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace AipsCore.Infrastructure.Persistence.Authentication;
|
||||
|
||||
public sealed class JwtSettings
|
||||
{
|
||||
public string Issuer { get; init; } = null!;
|
||||
public string Audience { get; init; } = null!;
|
||||
public string Key { get; init; } = null!;
|
||||
public int ExpirationMinutes { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using AipsCore.Application.Abstract.UserContext;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
namespace AipsCore.Infrastructure.Persistence.Authentication;
|
||||
|
||||
public class JwtTokenProvider : ITokenProvider
|
||||
{
|
||||
private readonly JwtSettings _jwtSettings;
|
||||
|
||||
public JwtTokenProvider(JwtSettings jwtSettings)
|
||||
{
|
||||
_jwtSettings = jwtSettings;
|
||||
}
|
||||
|
||||
public string Generate(Domain.Models.User.User user, IList<string> roles)
|
||||
{
|
||||
var claims = new List<Claim>
|
||||
{
|
||||
new Claim(ClaimTypes.NameIdentifier, user.Id.IdValue),
|
||||
new Claim(ClaimTypes.Email, user.Email.EmailValue)
|
||||
};
|
||||
|
||||
foreach (var role in roles)
|
||||
{
|
||||
claims.Add(new Claim(ClaimTypes.Role, role));
|
||||
}
|
||||
|
||||
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtSettings.Key));
|
||||
|
||||
var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
|
||||
|
||||
var token = new JwtSecurityToken(
|
||||
issuer: _jwtSettings.Issuer,
|
||||
audience: _jwtSettings.Audience,
|
||||
claims: claims,
|
||||
expires: DateTime.UtcNow.AddMinutes(_jwtSettings.ExpirationMinutes),
|
||||
signingCredentials: credentials);
|
||||
|
||||
return new JwtSecurityTokenHandler().WriteToken(token);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace AipsCore.Infrastructure.Persistence.Db;
|
||||
|
||||
public class AipsDbContext : DbContext
|
||||
public class AipsDbContext : IdentityDbContext<User.User, IdentityRole<Guid>, Guid>
|
||||
{
|
||||
public DbSet<User.User> Users { get; set; }
|
||||
public DbSet<Whiteboard.Whiteboard> Whiteboards { get; set; }
|
||||
public DbSet<Shape.Shape> Shapes { get; set; }
|
||||
public DbSet<WhiteboardMembership.WhiteboardMembership> WhiteboardMemberships { get; set; }
|
||||
|
||||
Reference in New Issue
Block a user