Files
Musify/Musify.Infrastructure/Services/StreamTicketService.cs
T
Admin 3be31a6378 refactor: standardize project names to Musify.* and flatten layout
- Flatten nested per-repo folders into flat Musify.* projects at root
- Rename assemblies/csproj: Domain->Musify.Domain, Application->Musify.Application,
  Infrastructure->Musify.Infrastructure, WebApi->Musify.Api, Worker->Musify.Worker,
  StreamingGateway->Musify.StreamingGateway
- Rename WebApi.* namespaces to Musify.Api.*
- Update all ProjectReference paths and the solution file
- Consolidate stray per-repo dotfiles; add root .gitattributes
- Move flutter client out to _archive
2026-07-05 03:32:48 +02:00

53 lines
1.8 KiB
C#

using System.Security.Cryptography;
using Microsoft.IdentityModel.JsonWebTokens;
using Microsoft.IdentityModel.Tokens;
using Musify.Application.Contracts;
using Musify.Infrastructure.Configuration;
namespace Musify.Infrastructure.Services
{
public sealed class StreamTicketService : IStreamTicketService, IDisposable
{
private readonly StreamTicketConfiguration configuration;
private readonly RSA rsa;
private readonly SigningCredentials signingCredentials;
private readonly JsonWebTokenHandler tokenHandler = new();
public StreamTicketService(StreamTicketConfiguration configuration)
{
this.configuration = configuration;
rsa = RSA.Create();
rsa.ImportFromPem(File.ReadAllText(configuration.PrivateKeyPath));
var key = new RsaSecurityKey(rsa);
signingCredentials = new SigningCredentials(key, SecurityAlgorithms.RsaSha256);
}
public StreamTicket IssueTicket(long userId, string keyPrefix)
{
var now = DateTimeOffset.UtcNow;
var descriptor = new SecurityTokenDescriptor
{
Issuer = configuration.Issuer,
Audience = configuration.Audience,
IssuedAt = now.UtcDateTime,
NotBefore = now.UtcDateTime,
Expires = now.AddSeconds(configuration.TicketTtlSeconds).UtcDateTime,
Claims = new Dictionary<string, object>
{
["sub"] = userId.ToString(),
["prefix"] = keyPrefix,
},
SigningCredentials = signingCredentials,
};
var token = tokenHandler.CreateToken(descriptor);
return new StreamTicket(token, configuration.TicketTtlSeconds);
}
public void Dispose() => rsa.Dispose();
}
}