Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion services/SonicRelay.Api/Contracts/LaunchIntentContracts.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace SonicRelay.Api.Contracts;

public sealed record CreateShareLaunchIntentRequest(
string Provider, string GuildId, string ChannelId, string RequestedByUserId, int? TtlSeconds = null);
string Provider, string? GuildId, string ChannelId, string RequestedByUserId, int? TtlSeconds = null);

public sealed record CreateShareLaunchIntentResponse(Guid Id, string LaunchUrl, DateTimeOffset ExpiresAt);
public sealed record ConsumeShareLaunchIntentRequest(string Token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ public static IEndpointRouteBuilder MapDiscordActivityLaunchIntentEndpoints(this
bot.MapPost("/{intentId:guid}/watch", async (Guid intentId, ReadyActivityRequest request,
LaunchIntentService service, IOptions<LaunchIntentOptions> options, CancellationToken ct) =>
{
if (!ValidContext(request.GuildId, request.ChannelId, request.RequestedByUserId)) return Results.BadRequest();
// DMs and Group DMs have no guild; the stored intent's channel still binds the watch link.
if (!ValidContext(request.ChannelId, request.RequestedByUserId)
|| (!string.IsNullOrEmpty(request.GuildId) && !ValidContext(request.GuildId)))
return Results.BadRequest();
try
{
var (token, expiresAt) = await service.CreateWatchCapabilityForReadyIntentAsync(
Expand Down
9 changes: 6 additions & 3 deletions services/SonicRelay.Api/Services/LaunchIntentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ public sealed class LaunchIntentService(
private readonly LaunchIntentOptions _options = options.Value;

public async Task<(ShareLaunchIntent Intent, string LaunchUrl)> CreateShareAsync(
string provider, string guildId, string channelId, string requestedByUserId, int? ttlSeconds,
string provider, string? guildId, string channelId, string requestedByUserId, int? ttlSeconds,
CancellationToken ct)
{
// DMs and Group DMs have no guild; the channel alone scopes the share.
guildId ??= string.Empty;
if (!string.Equals(provider, "discord", StringComparison.OrdinalIgnoreCase)
|| string.IsNullOrWhiteSpace(guildId) || string.IsNullOrWhiteSpace(channelId)
|| (guildId.Length > 0 && string.IsNullOrWhiteSpace(guildId)) || string.IsNullOrWhiteSpace(channelId)
|| string.IsNullOrWhiteSpace(requestedByUserId)
|| guildId.Length > 64 || channelId.Length > 64 || requestedByUserId.Length > 64)
throw new LaunchIntentException("invalid_request");
Expand Down Expand Up @@ -167,8 +169,9 @@ public async Task MarkPublishedAsync(Guid intentId, string? messageId, Cancellat
}

public async Task<(string Token, DateTimeOffset ExpiresAt)> CreateWatchCapabilityForReadyIntentAsync(
Guid intentId, string guildId, string channelId, int? ttlSeconds, CancellationToken ct)
Guid intentId, string? guildId, string channelId, int? ttlSeconds, CancellationToken ct)
{
guildId ??= string.Empty;
var intent = await db.ShareLaunchIntents.AsNoTracking().SingleOrDefaultAsync(x => x.Id == intentId, ct);
if (intent?.Status != ShareLaunchIntentStatuses.Ready || intent.SessionId is not { } sessionId
|| intent.GuildId != guildId || intent.ChannelId != channelId)
Expand Down
61 changes: 61 additions & 0 deletions tests/SonicRelay.Api.IntegrationTests/LaunchIntentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,67 @@ await DeviceIdentityTestHelper.BootstrapAndAuthorizeAsync(
ready.GetProperty("watchLaunchUrl").GetString(), StringComparison.Ordinal);
}

[Theory]
[InlineData("")]
[InlineData(null)]
public async Task Private_channel_share_without_guild_issues_watch_links_only_for_that_channel(string? guildId)
{
using var factory = new SonicRelayApiFactory();
using var bot = factory.CreateClient();
bot.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
"Bearer", "integration-test-discord-service-token");
var createdIntent = await bot.PostAsJsonAsync("/api/launch-intents/share", new
{
provider = "discord", guildId, channelId = "22", requestedByUserId = "33"
});
Assert.Equal(HttpStatusCode.Created, createdIntent.StatusCode);
var launch = await createdIntent.Content.ReadFromJsonAsync<JsonElement>();
var intentId = launch.GetProperty("id").GetGuid();
var token = launch.GetProperty("launchUrl").GetString()![("https://framerelay.hugojava.dev/open/share/".Length)..];

var pending = await bot.GetFromJsonAsync<JsonElement>("/api/launch-intents/pending");
Assert.Equal(string.Empty, pending.EnumerateArray().Single().GetProperty("guildId").GetString());

using var publisher = factory.CreateClient();
await DeviceIdentityTestHelper.BootstrapAndAuthorizeAsync(
publisher, DeviceTypes.WindowsPublisher, DevicePlatforms.Windows);
(await publisher.PostAsJsonAsync("/api/launch-intents/share/consume", new { token })).EnsureSuccessStatusCode();
var sessionResponse = await publisher.PostAsJsonAsync("/api/sessions", new { maxViewers = 3, mode = "screen_share" });
var session = await sessionResponse.Content.ReadFromJsonAsync<JsonElement>();
(await publisher.PostAsJsonAsync($"/api/launch-intents/share/{intentId}/complete",
new { sessionId = session.GetProperty("id").GetGuid() })).EnsureSuccessStatusCode();

var watch = await bot.PostAsJsonAsync($"/api/launch-intents/{intentId}/watch",
new { guildId, channelId = "22", requestedByUserId = "44", ttlSeconds = 120 });
Assert.Equal(HttpStatusCode.OK, watch.StatusCode);
var personal = await watch.Content.ReadFromJsonAsync<JsonElement>();
Assert.StartsWith("https://framerelay.hugojava.dev/open/watch/",
personal.GetProperty("launchUrl").GetString(), StringComparison.Ordinal);

Assert.Equal(HttpStatusCode.NotFound, (await bot.PostAsJsonAsync($"/api/launch-intents/{intentId}/watch",
new { guildId, channelId = "23", requestedByUserId = "44" })).StatusCode);
Assert.Equal(HttpStatusCode.NotFound, (await bot.PostAsJsonAsync($"/api/launch-intents/{intentId}/watch",
new { guildId = "1", channelId = "22", requestedByUserId = "44" })).StatusCode);
Assert.Equal(HttpStatusCode.BadRequest, (await bot.PostAsJsonAsync($"/api/launch-intents/{intentId}/watch",
new { guildId = "", channelId = "", requestedByUserId = "44" })).StatusCode);
}

[Fact]
public async Task Share_intent_without_channel_is_rejected()
{
using var factory = new SonicRelayApiFactory();
using var bot = factory.CreateClient();
bot.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
"Bearer", "integration-test-discord-service-token");

var response = await bot.PostAsJsonAsync("/api/launch-intents/share", new
{
provider = "discord", guildId = "", channelId = "", requestedByUserId = "33"
});

Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
}

[Fact]
public async Task Viewer_resolves_watch_capability_with_join_scope_only()
{
Expand Down
Loading