Files
Musify/backend/Tests/Musify.StreamingGateway.Tests/Authentication/TicketValidatorTests.cs
T
Admin 356e5c1101 Add anonymous playback with an optional Spotify-style preview cap
New Playback:AllowAnonymousListening / Playback:AnonymousFragmentSeconds
settings control whether a request without a session can stream at all,
and if so, whether it's limited to a fragment instead of the full track:

- GET /tracks/{id}/stream and GET /youtube/tracks/{videoId}/stream no
  longer require authorization outright; the handlers gate on the config
  instead, so both keep working exactly as before when it's off (the
  default).
- The fragment cap is enforced by the Streaming Gateway, not the client:
  the stream ticket now carries an optional maxBytes claim (computed from
  AnonymousFragmentSeconds and the transcoder's known bitrate), and
  TicketValidationMiddleware clamps the Range it forwards to SeaweedFS to
  stay under it — synthesizing one if the request didn't send one, and
  answering 416 outright if the requested range starts past the cap.
- A YouTube track that hasn't been provisioned server-side yet streams
  straight from YouTube's CDN, bypassing the Gateway entirely — there's no
  way to cap that, so an anonymous request for one is refused (409) instead
  of silently handing out an uncapped stream.
- ListeningHistory has a non-nullable UserId, so anonymous plays simply
  don't get recorded rather than trying to force a row through the FK.

Also fixes a real, pre-existing bug this surfaced: the AppSettings.Development.json
of the Api, Worker, and StreamingGateway all pointed StreamTicket/Workers
paths at "../../deploy/..." — one directory short of deploy/'s actual
location at the repo root relative to each project's own folder. Docker
never hit this (it mounts an absolute /keys path), so it had apparently
never been exercised via `dotnet run` before.
2026-09-10 09:56:57 +02:00

166 lines
5.8 KiB
C#

using System.Security.Cryptography;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.JsonWebTokens;
using Microsoft.IdentityModel.Tokens;
using Musify.StreamingGateway.Authentication;
using Musify.StreamingGateway.Configuration;
using Xunit;
namespace Musify.StreamingGateway.Tests.Authentication
{
public sealed class TicketValidatorTests : IDisposable
{
private const string Issuer = "musify-webapi";
private const string Audience = "media-gateway";
private readonly string publicKeyPath = Path.GetTempFileName();
private readonly RSA rsa = RSA.Create(2048);
private readonly JsonWebTokenHandler tokenHandler = new();
public TicketValidatorTests() => File.WriteAllText(publicKeyPath, rsa.ExportSubjectPublicKeyInfoPem());
public void Dispose()
{
rsa.Dispose();
File.Delete(publicKeyPath);
}
private TicketValidator CreateValidator() => new(Options.Create(new StreamTicketValidationOptions
{
PublicKeyPath = publicKeyPath,
Audience = Audience,
Issuer = Issuer
}));
private string IssueToken(string prefix = "Tracks/ProcessedAudios/abc/", string issuer = Issuer, string audience = Audience,
DateTime? expires = null, DateTime? notBefore = null, long? maxBytes = null)
{
var claims = new Dictionary<string, object> { ["prefix"] = prefix };
if (maxBytes is not null)
claims["maxBytes"] = maxBytes.Value.ToString();
var descriptor = new SecurityTokenDescriptor
{
Issuer = issuer,
Audience = audience,
NotBefore = notBefore ?? DateTime.UtcNow.AddMinutes(-1),
Expires = expires ?? DateTime.UtcNow.AddMinutes(5),
Claims = claims,
SigningCredentials = new SigningCredentials(new RsaSecurityKey(rsa), SecurityAlgorithms.RsaSha256),
};
return tokenHandler.CreateToken(descriptor);
}
[Fact]
public async Task ValidateTicketAsync_ValidToken_ReturnsThePrefixClaim()
{
var token = IssueToken(prefix: "Tracks/ProcessedAudios/xyz/");
var result = await CreateValidator().ValidateTicketAsync(token);
Assert.True(result.IsValid);
Assert.Equal("Tracks/ProcessedAudios/xyz/", result.Prefix);
Assert.Null(result.MaxBytes);
}
[Fact]
public async Task ValidateTicketAsync_TokenWithMaxBytesClaim_ReturnsIt()
{
var token = IssueToken(maxBytes: 480_000);
var result = await CreateValidator().ValidateTicketAsync(token);
Assert.True(result.IsValid);
Assert.Equal(480_000, result.MaxBytes);
}
[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
public async Task ValidateTicketAsync_BlankToken_ReturnsInvalid(string? token)
{
var result = await CreateValidator().ValidateTicketAsync(token);
Assert.False(result.IsValid);
Assert.Equal(string.Empty, result.Prefix);
}
[Fact]
public async Task ValidateTicketAsync_MalformedToken_ReturnsInvalid()
{
var result = await CreateValidator().ValidateTicketAsync("not-a-real-jwt");
Assert.False(result.IsValid);
}
[Fact]
public async Task ValidateTicketAsync_ExpiredToken_ReturnsInvalid()
{
var token = IssueToken(expires: DateTime.UtcNow.AddMinutes(-40), notBefore: DateTime.UtcNow.AddMinutes(-50));
var result = await CreateValidator().ValidateTicketAsync(token);
Assert.False(result.IsValid);
}
[Fact]
public async Task ValidateTicketAsync_WrongIssuer_ReturnsInvalid()
{
var token = IssueToken(issuer: "someone-else");
var result = await CreateValidator().ValidateTicketAsync(token);
Assert.False(result.IsValid);
}
[Fact]
public async Task ValidateTicketAsync_WrongAudience_ReturnsInvalid()
{
var token = IssueToken(audience: "someone-else");
var result = await CreateValidator().ValidateTicketAsync(token);
Assert.False(result.IsValid);
}
[Fact]
public async Task ValidateTicketAsync_SignedWithADifferentKey_ReturnsInvalid()
{
using var otherKey = RSA.Create(2048);
var descriptor = new SecurityTokenDescriptor
{
Issuer = Issuer,
Audience = Audience,
NotBefore = DateTime.UtcNow.AddMinutes(-1),
Expires = DateTime.UtcNow.AddMinutes(5),
Claims = new Dictionary<string, object> { ["prefix"] = "Tracks/ProcessedAudios/abc/" },
SigningCredentials = new SigningCredentials(new RsaSecurityKey(otherKey), SecurityAlgorithms.RsaSha256),
};
var token = tokenHandler.CreateToken(descriptor);
var result = await CreateValidator().ValidateTicketAsync(token);
Assert.False(result.IsValid);
}
[Fact]
public async Task ValidateTicketAsync_MissingPrefixClaim_ReturnsInvalid()
{
var descriptor = new SecurityTokenDescriptor
{
Issuer = Issuer,
Audience = Audience,
NotBefore = DateTime.UtcNow.AddMinutes(-1),
Expires = DateTime.UtcNow.AddMinutes(5),
SigningCredentials = new SigningCredentials(new RsaSecurityKey(rsa), SecurityAlgorithms.RsaSha256),
};
var token = tokenHandler.CreateToken(descriptor);
var result = await CreateValidator().ValidateTicketAsync(token);
Assert.False(result.IsValid);
}
}
}