mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 11:44:56 +00:00
76094af15b
+ tests + new services + InvitationDto and IInvitationReqest
42 lines
1.3 KiB
C#
42 lines
1.3 KiB
C#
using System.IdentityModel.Tokens.Jwt;
|
||
using System.Security.Claims;
|
||
using System.Text;
|
||
using Govor.API.Services.Authentication.Interfaces;
|
||
using Govor.Core.Models;
|
||
using Govor.Core.Repositories.Admins;
|
||
using Govor.Core.Repositories.Invaites;
|
||
using Microsoft.Extensions.Options;
|
||
using Microsoft.IdentityModel.Tokens;
|
||
|
||
namespace Govor.API.Services.Authentication;
|
||
|
||
public class JwtService : IJwtService
|
||
{
|
||
private JwtOption _jwtOption;
|
||
private IInvitesService _invitesService;
|
||
|
||
public JwtService(IOptions<JwtOption> options, IInvitesService invitesService)
|
||
{
|
||
_jwtOption = options.Value;
|
||
_invitesService = invitesService;
|
||
}
|
||
|
||
public string GenerateJwtToken(User user)
|
||
{
|
||
var claims = new[]
|
||
{
|
||
new Claim("userID", user.Id.ToString()),
|
||
new Claim(ClaimTypes.Role, _invitesService.GetRole(user).Result, ClaimValueTypes.String)
|
||
};
|
||
|
||
var singing = new SigningCredentials(
|
||
new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtOption.SecretKeу)),
|
||
SecurityAlgorithms.HmacSha256Signature);
|
||
|
||
var token = new JwtSecurityToken(signingCredentials: singing,
|
||
expires: DateTime.UtcNow.AddHours(_jwtOption.Hours),
|
||
claims: claims);
|
||
|
||
return new JwtSecurityTokenHandler().WriteToken(token);
|
||
}
|
||
} |