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
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,12 @@ public async Task<TTryResult> TryAndCommitAsync<TTryResult>(string tryEndpoint,
{
var basePath = _options.Endpoints.BasePath;

if (Uri.TryCreate(basePath, UriKind.Absolute, out var uri))
return uri.GetLeftPart(UriPartial.Authority);
if (!Uri.TryCreate(basePath, UriKind.Absolute, out var uri))
return null;

// Relative BasePath: JS automatically uses the current origin.
return null;
if (uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps)
return null;

return uri.GetLeftPart(UriPartial.Authority);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ public UAuthRequestClientTests()
.Returns(Task.CompletedTask);
}

private UAuthRequestClient CreateSut(UAuthClientProfile profile = UAuthClientProfile.BlazorWasm)
private UAuthRequestClient CreateSut(UAuthClientProfile profile = UAuthClientProfile.BlazorWasm, string? basePath = null)
{
var options = Options.Create(new UAuthClientOptions
var options = new UAuthClientOptions
{
ClientProfile = profile
});
};

if (basePath is not null)
options.Endpoints.BasePath = basePath;

return new UAuthRequestClient(JSInterop.JSRuntime, _bootstrapper.Object, options);
return new UAuthRequestClient(JSInterop.JSRuntime, _bootstrapper.Object, Options.Create(options));
}

[Fact]
Expand Down Expand Up @@ -178,6 +181,36 @@ public async Task ObservedLogoutReturnsHttpRejectionRatherThanSubmittingAnUnobse
Assert.Equal("logout", GetProperty<string>(invocation.Arguments.Single()!, "mode"));
}

[Theory]
[InlineData("/auth", null)]
[InlineData("auth", null)]
[InlineData("https://example.com/auth", "https://example.com")]
[InlineData("http://localhost:5000/auth", "http://localhost:5000")]
public async Task SendFormAsync_ResolvesTrustedOriginCorrectly(string basePath, string? expectedOrigin)
{
// Arrange
JSInterop
.Setup<UAuthTransportResult>("uauth.post", _ => true)
.SetResult(CreateTransportResult(200));

var sut = CreateSut(basePath: basePath);

// Act
await sut.SendFormAsync("/auth/validate");

// Assert
var invocation = JSInterop.Invocations
.Single(x => x.Identifier == "uauth.post");

var options = invocation.Arguments.Single()!;

GetProperty<string>(options, "url")
.Should().Be("/auth/validate");

GetProperty<string?>(options, "trustedOrigin")
.Should().Be(expectedOrigin);
}

private sealed class TestTryResult
{
}
Expand Down
Loading