Proxy
This commit is contained in:
@@ -41,5 +41,17 @@ namespace Musify.Application.Configuration
|
||||
|
||||
return serviceDescriptors;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddStreamGatewayConfiguration(this IServiceCollection serviceDescriptors, IConfiguration configuration)
|
||||
{
|
||||
serviceDescriptors
|
||||
.AddOptionsWithValidateOnStart<StreamGatewayConfiguration>()
|
||||
.Bind(configuration.GetRequiredSection(StreamGatewayConfiguration.SectionName))
|
||||
.ValidateDataAnnotations();
|
||||
serviceDescriptors.AddSingleton(serviceProvider =>
|
||||
serviceProvider.GetRequiredService<IOptions<StreamGatewayConfiguration>>().Value);
|
||||
|
||||
return serviceDescriptors;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Musify.Application.Configuration
|
||||
{
|
||||
public class StreamGatewayConfiguration
|
||||
{
|
||||
public const string SectionName = "StreamGateway";
|
||||
|
||||
[Url]
|
||||
[Required]
|
||||
public required string PublicBaseUrl { get; set; }
|
||||
|
||||
[Required]
|
||||
public string ManifestFileName { get; set; } = "manifest.mpd";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace Musify.Application.Contracts
|
||||
{
|
||||
public interface IStreamTicketService
|
||||
{
|
||||
StreamTicket IssueTicket(Guid userId, string keyPrefix);
|
||||
}
|
||||
|
||||
public record StreamTicket(string Token, int ExpiresInSeconds);
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using Ardalis.Result;
|
||||
using Mediator;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Musify.Application.Configuration;
|
||||
using Musify.Application.Contracts;
|
||||
using Musify.Application.Tracks.Queries;
|
||||
using Musify.Application.Tracks.Responses;
|
||||
using Musify.Domain.ValueObjects;
|
||||
|
||||
namespace Musify.Application.Tracks.Handlers
|
||||
{
|
||||
public class GetTrackStreamQueryHandler(
|
||||
IDatabase database,
|
||||
IStreamTicketService ticketService,
|
||||
TrackConfiguration trackConfiguration,
|
||||
StreamGatewayConfiguration streamGatewayConfiguration,
|
||||
ILogger<GetTrackStreamQueryHandler> logger)
|
||||
: IQueryHandler<GetTrackStreamQuery, Result<TrackStreamResponse>>
|
||||
{
|
||||
public async ValueTask<Result<TrackStreamResponse>> Handle(GetTrackStreamQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var track = await database.Tracks.AsNoTracking()
|
||||
.Where(t => t.Id == request.TrackId)
|
||||
.Select(t => new { t.Id, t.AudioTranscodeProcessingStatus, t.AudioFolderName })
|
||||
.SingleOrDefaultAsync(cancellationToken);
|
||||
|
||||
if (track is null)
|
||||
return Result<TrackStreamResponse>.NotFound();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(track.AudioFolderName)
|
||||
|| track.AudioTranscodeProcessingStatus != ProcessingStatus.Completed)
|
||||
{
|
||||
logger.LogInformation("Stream requested for track {TrackId} but audio is not ready", request.TrackId);
|
||||
return Result<TrackStreamResponse>.Conflict("Track audio is not available for streaming yet.");
|
||||
}
|
||||
|
||||
var folderPath = trackConfiguration.Routes.BuildProcessedAudioPath(track.AudioFolderName);
|
||||
var keyPrefix = $"{folderPath}/";
|
||||
|
||||
var ticket = ticketService.IssueTicket(request.UserId, keyPrefix);
|
||||
|
||||
var manifestUrl = string.Join('/',
|
||||
streamGatewayConfiguration.PublicBaseUrl.TrimEnd('/'),
|
||||
"media",
|
||||
folderPath,
|
||||
streamGatewayConfiguration.ManifestFileName);
|
||||
|
||||
logger.LogInformation("Issued stream ticket for track {TrackId} to user {UserId}", request.TrackId, request.UserId);
|
||||
|
||||
return Result.Success(new TrackStreamResponse(manifestUrl, ticket.Token, ticket.ExpiresInSeconds));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
using Ardalis.Result;
|
||||
using Mediator;
|
||||
using Musify.Application.Tracks.Responses;
|
||||
|
||||
namespace Musify.Application.Tracks.Queries
|
||||
{
|
||||
public record GetTrackStreamQuery(Guid TrackId, Guid UserId)
|
||||
: IQuery<Result<TrackStreamResponse>>;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Musify.Application.Tracks.Responses
|
||||
{
|
||||
public record TrackStreamResponse(
|
||||
string ManifestUrl,
|
||||
string Ticket,
|
||||
int ExpiresInSeconds);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Musify.Infrastructure.Configuration
|
||||
{
|
||||
public class StreamTicketConfiguration
|
||||
{
|
||||
public const string SectionName = "StreamTicket";
|
||||
|
||||
[Required]
|
||||
public required string PrivateKeyPath { get; set; }
|
||||
|
||||
[Required]
|
||||
public string Audience { get; set; } = "media-gateway";
|
||||
|
||||
[Required]
|
||||
public string Issuer { get; set; } = "musify-webapi";
|
||||
|
||||
[Range(60, 86400)]
|
||||
public int TicketTtlSeconds { get; set; } = 3600;
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,7 @@
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="10.0.8" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="10.0.8" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="10.0.8" />
|
||||
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.14.0" />
|
||||
<PackageReference Include="MimeMapping" Version="4.0.0" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.2" />
|
||||
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.15.3" />
|
||||
|
||||
@@ -71,6 +71,20 @@ namespace Musify.Infrastructure.Services
|
||||
return serviceDescriptors;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddStreamTicketService(this IServiceCollection serviceDescriptors, IConfiguration configuration)
|
||||
{
|
||||
serviceDescriptors
|
||||
.AddOptionsWithValidateOnStart<StreamTicketConfiguration>()
|
||||
.Bind(configuration.GetRequiredSection(StreamTicketConfiguration.SectionName))
|
||||
.ValidateDataAnnotations();
|
||||
|
||||
serviceDescriptors.AddSingleton(serviceProvider =>
|
||||
serviceProvider.GetRequiredService<IOptions<StreamTicketConfiguration>>().Value);
|
||||
|
||||
serviceDescriptors.AddSingleton<IStreamTicketService, StreamTicketService>();
|
||||
return serviceDescriptors;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddAudioTranscoder(this IServiceCollection serviceDescriptors, IConfiguration configuration)
|
||||
{
|
||||
serviceDescriptors
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
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(Guid 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user