52 lines
1.7 KiB
C#
52 lines
1.7 KiB
C#
using System.Security.Cryptography;
|
|
using Microsoft.Extensions.Options;
|
|
using Microsoft.IdentityModel.JsonWebTokens;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Musify.StreamingGateway.Configuration;
|
|
|
|
namespace Musify.StreamingGateway.Authentication;
|
|
|
|
public sealed class TicketValidator : IDisposable
|
|
{
|
|
private readonly StreamTicketValidationOptions _options;
|
|
private readonly RSA _rsa;
|
|
private readonly TokenValidationParameters _validationParameters;
|
|
private readonly JsonWebTokenHandler _handler = new();
|
|
|
|
public TicketValidator(IOptions<StreamTicketValidationOptions> options)
|
|
{
|
|
_options = options.Value;
|
|
|
|
_rsa = RSA.Create();
|
|
_rsa.ImportFromPem(File.ReadAllText(_options.PublicKeyPath));
|
|
|
|
_validationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateIssuer = true,
|
|
ValidIssuer = _options.Issuer,
|
|
ValidateAudience = true,
|
|
ValidAudience = _options.Audience,
|
|
ValidateLifetime = true,
|
|
ValidateIssuerSigningKey = true,
|
|
IssuerSigningKey = new RsaSecurityKey(_rsa),
|
|
ValidAlgorithms = [SecurityAlgorithms.RsaSha256],
|
|
ClockSkew = TimeSpan.FromSeconds(30),
|
|
};
|
|
}
|
|
|
|
public async Task<string> ValidateTicketAsync(string? token)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(token))
|
|
return string.Empty;
|
|
|
|
var result = await _handler.ValidateTokenAsync(token, _validationParameters);
|
|
if (!result.IsValid)
|
|
return string.Empty;
|
|
|
|
var prefix = result.ClaimsIdentity?.FindFirst("prefix")?.Value;
|
|
return string.IsNullOrEmpty(prefix)? string.Empty : prefix;
|
|
}
|
|
|
|
public void Dispose() => _rsa.Dispose();
|
|
}
|