parent
82e59e0c86
commit
7ec3ee225a
@ -0,0 +1,30 @@
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Cryptography.KeyDerivation;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
namespace API.Auth;
|
||||
|
||||
public static class Authentication
|
||||
{
|
||||
private static readonly TimeSpan TokenLifetime = TimeSpan.FromMinutes(50);
|
||||
|
||||
public static (string, DateTime) GenerateJwt(SymmetricSecurityKey key, IEnumerable<Claim> claims)
|
||||
{
|
||||
var expirationDate = DateTime.UtcNow.Add(TokenLifetime);
|
||||
var tokenDescriptor = new SecurityTokenDescriptor
|
||||
{
|
||||
Subject = new ClaimsIdentity(claims),
|
||||
Expires = expirationDate,
|
||||
SigningCredentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256)
|
||||
};
|
||||
var tokenHandler = new JwtSecurityTokenHandler();
|
||||
|
||||
var token = tokenHandler.CreateToken(tokenDescriptor);
|
||||
|
||||
var jwt = tokenHandler.WriteToken(token);
|
||||
return ("Bearer " + jwt, expirationDate);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
using Model;
|
||||
|
||||
namespace API.DTO;
|
||||
|
||||
public static class ModelToDto
|
||||
{
|
||||
public static TacticDto ToDto(this Tactic t)
|
||||
{
|
||||
return new TacticDto(t.Id, t.Name, t.OwnerId, t.CourtType.ToString().ToUpper(), new DateTimeOffset(t.CreationDate).ToUnixTimeMilliseconds());
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
namespace API.DTO;
|
||||
|
||||
public record TacticDto(int Id, string Name, int OwnerId, string CourtType, long CreationDate);
|
@ -0,0 +1,6 @@
|
||||
namespace DbServices;
|
||||
|
||||
public class Security
|
||||
{
|
||||
|
||||
}
|
Loading…
Reference in new issue