Adds messages api

This commit is contained in:
Jonathan Bourdon
2024-06-27 12:37:59 -04:00
parent 97a2c296f1
commit 623972bc36
21 changed files with 485 additions and 61 deletions

View File

@@ -1,3 +1,4 @@
using System.Diagnostics;
using Google.Apis.Oauth2.v2.Data;
using System.Security.Claims;
using Hutopy.Application.Common.Interfaces;
@@ -220,14 +221,20 @@ public class IdentityService(
}
var user = await GetUserByUserNameAsync(userName);
if (user is null) throw new InvalidOperationException();
var jwtSection = configuration.GetRequiredSection("Authentication:Jwt");
var token = JwtTokenHelper.GenerateJwtToken(
issuer: jwtSection["Issuer"] ?? "",
audience: jwtSection["Audience"] ?? "",
key: jwtSection["Key"] ?? "",
userId: user?.Id ?? "");
userId: user.Id,
email: user.Email,
firstname: user.FirstName,
lastname: user.LastName,
portraitUrl: user.PortraitUrl);
return token;
}

View File

@@ -7,20 +7,31 @@ namespace Hutopy.Infrastructure.Utils;
public static class JwtTokenHelper
{
public static string GenerateJwtToken(string issuer, string audience, string key, string userId)
public static string GenerateJwtToken(string issuer, string audience, string key, string? userId, string? email,
string? firstname, string? lastname, string? portraitUrl)
{
var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key));
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
var claims = new List<Claim>(new[]
{
new Claim(JwtRegisteredClaimNames.Sub, userId),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
new Claim(ClaimTypes.NameIdentifier, userId),
new Claim(ClaimTypes.Email, email),
new Claim(ClaimTypes.GivenName, firstname),
new Claim(ClaimTypes.Surname, lastname),
});
if (portraitUrl is not null)
{
claims.Add(new Claim("portrait-url", portraitUrl));
}
var token = new JwtSecurityToken(
issuer: issuer,
audience: audience,
claims: new[]
{
new Claim(JwtRegisteredClaimNames.Sub, userId),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
new Claim(ClaimTypes.NameIdentifier, userId)
},
claims: claims,
expires: DateTime.Now.AddMinutes(30),
signingCredentials: credentials);