Files
EchoHub/src/EchoHub.Server/Auth/JwtTokenService.cs
T
HueByte b4c3ebd254 feat: enhance profile editing with avatar support and UI adjustments
- Added AvatarPath to ProfileEditResult for user profile updates.
- Updated ProfileEditDialog to include avatar selection with a browse button.
- Increased dialog height to accommodate new avatar input fields.
- Implemented avatar file path handling in the profile edit dialog.

feat: introduce moderation features and user roles

- Added ServerRole enum to define user roles (Member, Mod, Admin, Owner).
- Extended User model to include role, mute, and ban status.
- Created ModerationController for user role assignment, kicking, banning, and muting.
- Implemented methods in IChatBroadcaster and SignalRBroadcaster for user moderation actions.
- Updated database schema with new columns for user roles and moderation states.

fix: ensure muted users cannot send messages

- Added mute status checks in ChatService to prevent message sending for muted users.
- Updated user presence and status handling to reflect role changes and moderation actions.

chore: update constants for ASCII art rendering

- Introduced AsciiArtHeightHalfBlock constant for improved ASCII art rendering.
2026-02-19 17:47:10 +01:00

68 lines
2.3 KiB
C#

using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Text;
using EchoHub.Core.Models;
using Microsoft.IdentityModel.Tokens;
namespace EchoHub.Server.Auth;
public class JwtTokenService
{
private readonly string _secret;
private readonly string _issuer;
private readonly string _audience;
private static readonly TimeSpan AccessTokenLifetime = TimeSpan.FromMinutes(15);
public static readonly TimeSpan RefreshTokenLifetime = TimeSpan.FromDays(30);
public JwtTokenService(IConfiguration configuration)
{
_secret = configuration["Jwt:Secret"]
?? throw new InvalidOperationException("Jwt:Secret is not configured.");
_issuer = configuration["Jwt:Issuer"]
?? throw new InvalidOperationException("Jwt:Issuer is not configured.");
_audience = configuration["Jwt:Audience"]
?? throw new InvalidOperationException("Jwt:Audience is not configured.");
}
public (string Token, DateTimeOffset ExpiresAt) GenerateAccessToken(User user)
{
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_secret));
var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
var expiresAt = DateTimeOffset.UtcNow.Add(AccessTokenLifetime);
Claim[] claims =
[
new(JwtRegisteredClaimNames.Sub, user.Id.ToString()),
new("username", user.Username),
new("display_name", user.DisplayName ?? user.Username),
new("role", user.Role.ToString()),
new(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
];
var token = new JwtSecurityToken(
issuer: _issuer,
audience: _audience,
claims: claims,
expires: expiresAt.UtcDateTime,
signingCredentials: credentials);
return (new JwtSecurityTokenHandler().WriteToken(token), expiresAt);
}
public static string GenerateRefreshToken()
{
var randomBytes = new byte[64];
using var rng = RandomNumberGenerator.Create();
rng.GetBytes(randomBytes);
return Convert.ToBase64String(randomBytes);
}
public static string HashToken(string token)
{
var bytes = SHA256.HashData(Encoding.UTF8.GetBytes(token));
return Convert.ToBase64String(bytes);
}
}