diff --git a/packages/Gotrue/Gotrue.Tests/ListCustomProviders/ListCustomProvidersContractTests.cs b/packages/Gotrue/Gotrue.Tests/ListCustomProviders/ListCustomProvidersContractTests.cs new file mode 100644 index 00000000..fc5600fe --- /dev/null +++ b/packages/Gotrue/Gotrue.Tests/ListCustomProviders/ListCustomProvidersContractTests.cs @@ -0,0 +1,106 @@ +#region + +using System; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; +using FluentAssertions; +using Gotrue.Tests.Common; +using Gotrue.Tests.Support; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Supabase.Gotrue; +using Supabase.Gotrue.Common; +using WireMock.RequestBuilders; +using WireMock.ResponseBuilders; + +#endregion + +namespace Gotrue.Tests.ListCustomProviders; + +/// +/// Pins the wire contract of listing custom OAuth/OIDC providers against a stubbed auth server: the route, the +/// service-role credentials, the type filter, the response mapping, and cancellation. +/// +[TestClass] +[TestCategory("Contract")] +public class ListCustomProvidersContractTests +{ + private const string ServiceKey = "service-role-jwt"; + private const string NoProviders = """{"providers":[]}"""; + + private MockGotrueServer server = null!; + + [TestInitialize] + public void TestInitialize() => this.server = new MockGotrueServer(); + + [TestCleanup] + public void TestCleanup() => this.server.Dispose(); + + [TestMethod] + public async Task ListCustomProvidersAsync_ShouldListEveryProviderWithServiceRoleCredentials_GivenNoType() + { + this.AnswerWith(NoProviders); + await this.Admin.ListCustomProvidersAsync(); + this.server.VerifySingleReceivedRequest() + .WithMethod(HttpMethod.Get) + .WithPath("/admin/custom-providers") + .WithHeader("Authorization", $"Bearer {ServiceKey}") + .WithoutQueryParam("type"); + } + + [TestMethod] + [DataRow(CustomProviderType.OAuth2, "oauth2")] + [DataRow(CustomProviderType.Oidc, "oidc")] + public async Task ListCustomProvidersAsync_ShouldFilterByType_GivenType(CustomProviderType type, string wireType) + { + this.AnswerWith(NoProviders); + await this.Admin.ListCustomProvidersAsync(type); + this.server.VerifySingleReceivedRequest().WithQueryParam("type", wireType); + } + + [TestMethod] + public async Task ListCustomProvidersAsync_ShouldReturnEveryListedProvider() + { + this.AnswerWith($$"""{"providers":[{{CustomProviderResponses.OAuth2}},{{CustomProviderResponses.Oidc}}]}"""); + (await this.Admin.ListCustomProvidersAsync()).Select(provider => (provider.Identifier, provider.Type)).Should().Equal( + new[] { ("custom:customauth", CustomProviderType.OAuth2), ("custom:mycompany", CustomProviderType.Oidc) }, + "the auth server answers 200 with every stored provider under providers"); + } + + [TestMethod] + [DataRow("""{"providers":null}""")] + [DataRow("""{}""")] + [DataRow("null")] + public async Task ListCustomProvidersAsync_ShouldReturnEmptyList_GivenNoProvidersInResponse(string body) + { + this.AnswerWith(body); + (await this.Admin.ListCustomProvidersAsync()).Should().BeEmpty("the returned list is never null, whatever the server sends"); + } + + [TestMethod] + public void ListCustomProvidersAsync_ShouldComplete_GivenCallerBlockingOnContextThatNeverRunsContinuations() + { + this.AnswerWith(NoProviders); + BlockingCaller.Completes(() => this.Admin.ListCustomProvidersAsync()).Should().BeTrue( + "a library must not resume on the caller's context: a UI thread blocking on the task would deadlock"); + } + + [TestMethod] + public async Task ListCustomProvidersAsync_ShouldNotSendRequest_GivenCancelledToken() + { + using var cancelled = new CancellationTokenSource(); + cancelled.Cancel(); + var list = () => this.Admin.ListCustomProvidersAsync(cancellationToken: cancelled.Token); + await list.Should().ThrowAsync(); + this.server.CountReceivedRequests().Should().Be(0, "a cancelled token must stop the request before it reaches the wire"); + } + + private AdminClient Admin => new(ServiceKey, new ClientOptions { Url = this.server.Url }); + + private void AnswerWith(string body) => + this.server.Given(Request.Create().WithPath("/admin/custom-providers").UsingGet()) + .RespondWith(Response.Create().WithStatusCode(HttpStatusCode.OK) + .WithHeader("Content-Type", "application/json").WithBody(body)); +} diff --git a/packages/Gotrue/Gotrue.Tests/ListCustomProviders/ListCustomProvidersTests.cs b/packages/Gotrue/Gotrue.Tests/ListCustomProviders/ListCustomProvidersTests.cs new file mode 100644 index 00000000..1249b76d --- /dev/null +++ b/packages/Gotrue/Gotrue.Tests/ListCustomProviders/ListCustomProvidersTests.cs @@ -0,0 +1,42 @@ +#region + +using System.Linq; +using System.Threading.Tasks; +using FluentAssertions; +using Gotrue.Tests.Common; +using Gotrue.Tests.Support; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Supabase.Gotrue.Common; +using Supabase.Gotrue.Exceptions; + +#endregion + +namespace Gotrue.Tests.ListCustomProviders; + +/// +/// End-to-end listing of custom OAuth/OIDC providers against the live stack, with and without a type filter. +/// +[TestClass] +[TestCategory("E2E")] +public class ListCustomProvidersTests : CustomProviderAdminFixture +{ + [TestMethod] + [DataRow(null, true)] + [DataRow(CustomProviderType.OAuth2, true)] + [DataRow(CustomProviderType.Oidc, false)] + public async Task ListCustomProvidersAsync_ShouldListSeededOAuth2ProviderOnlyWhenTypeMatches(CustomProviderType? type, bool listed) + { + await this.SeedProvider(); + (await this.Admin.ListCustomProvidersAsync(type)).Any(provider => provider.Identifier == this.Identifier).Should().Be(listed, + "the auth server filters the list by provider_type when type is given and returns every provider otherwise"); + } + + [TestMethod] + public async Task ListCustomProvidersAsync_ShouldThrowAdminTokenRequired_GivenNonAdminKey() + { + var list = () => TestClients.AdminAgainstCliStack(TestClients.CliAnonKey).ListCustomProvidersAsync(); + await list.Should().ThrowAsync() + .Where(failure => failure.Reason == FailureHint.Reason.AdminTokenRequired, + "the auth server answers 403 not_admin to a key without the service_role claim"); + } +} diff --git a/packages/Gotrue/Gotrue/AdminClient.cs b/packages/Gotrue/Gotrue/AdminClient.cs index 0ebefa64..b1001034 100644 --- a/packages/Gotrue/Gotrue/AdminClient.cs +++ b/packages/Gotrue/Gotrue/AdminClient.cs @@ -170,6 +170,11 @@ public Task UpdateCustomProviderAsync(string identifier, CustomP public Task GetCustomProviderAsync(string identifier, CancellationToken cancellationToken = default) => this.api.GetCustomProviderAsync(this.serviceKey, identifier, cancellationToken); + /// + [ImplementedCapability("auth.admin.list_providers")] + public Task> ListCustomProvidersAsync(CustomProviderType? type = null, CancellationToken cancellationToken = default) => + this.api.ListCustomProvidersAsync(this.serviceKey, type, cancellationToken); + /// public async Task Update(UserAttributes attributes) { diff --git a/packages/Gotrue/Gotrue/Api.cs b/packages/Gotrue/Gotrue/Api.cs index 251dca96..2c83effc 100644 --- a/packages/Gotrue/Gotrue/Api.cs +++ b/packages/Gotrue/Gotrue/Api.cs @@ -14,6 +14,7 @@ using Supabase.Gotrue.CustomProviders; using Supabase.Gotrue.Exceptions; using Supabase.Gotrue.Interfaces; +using Supabase.Gotrue.ListCustomProviders; using Supabase.Gotrue.Mfa; using Supabase.Gotrue.Resend; using Supabase.Gotrue.Responses; @@ -668,6 +669,13 @@ public async Task GetCustomProviderAsync(string jwt, string iden return CustomProviderResponse.Read(provider); } + /// + public async Task> ListCustomProvidersAsync(string jwt, CustomProviderType? type = null, CancellationToken cancellationToken = default) + { + var listed = await this.MakeRequestAsync(HttpMethod.Get, $"{this.Url}/admin/custom-providers", ListCustomProvidersQuery.For(type), this.CreateAuthedRequestHeaders(jwt), cancellationToken).ConfigureAwait(false); + return CustomProviderListResponse.Read(listed); + } + private string CustomProviderUrl(string identifier) => $"{this.Url}/admin/custom-providers/{CustomProviderIdentifier.EnsureRoutable(identifier)}"; diff --git a/packages/Gotrue/Gotrue/Interfaces/IGotrueAdminClient.cs b/packages/Gotrue/Gotrue/Interfaces/IGotrueAdminClient.cs index b17d9d6d..97894232 100644 --- a/packages/Gotrue/Gotrue/Interfaces/IGotrueAdminClient.cs +++ b/packages/Gotrue/Gotrue/Interfaces/IGotrueAdminClient.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Supabase.Core.Interfaces; @@ -257,4 +258,26 @@ public interface IGotrueAdminClient : IGettableHeaders /// /// Task GetCustomProviderAsync(string identifier, CancellationToken cancellationToken = default); + + /// + /// Lists the custom OAuth/OIDC providers registered on the project, optionally only those of one type. + /// Requires a full-access API key, such as the service_role key: call it from a server, never from a client app. + /// + /// Only providers of this type; every provider when null. + /// Cancels the request. + /// The providers, empty when none is registered. Their client secrets are never returned. + /// + /// The server rejected the request. is + /// when the key is not a full-access key; + /// is feature_disabled when custom providers are disabled on the project. + /// + /// + /// + /// using Supabase.Gotrue.Common; + /// + /// var admin = new AdminClient(serviceRoleKey, new ClientOptions { Url = authUrl }); + /// var oidcProviders = await admin.ListCustomProvidersAsync(CustomProviderType.Oidc); + /// + /// + Task> ListCustomProvidersAsync(CustomProviderType? type = null, CancellationToken cancellationToken = default); } diff --git a/packages/Gotrue/Gotrue/Interfaces/IGotrueApi.cs b/packages/Gotrue/Gotrue/Interfaces/IGotrueApi.cs index a463991a..36872cd1 100644 --- a/packages/Gotrue/Gotrue/Interfaces/IGotrueApi.cs +++ b/packages/Gotrue/Gotrue/Interfaces/IGotrueApi.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Supabase.Core.Interfaces; @@ -117,6 +118,16 @@ public interface IGotrueApi : IGettableHeaders /// The server rejected the request. Task GetCustomProviderAsync(string jwt, string identifier, CancellationToken cancellationToken = default); + /// + /// Lists the custom OAuth/OIDC providers, optionally only those of one type. + /// + /// A full-access API key, such as the service_role key. + /// Only providers of this type; every provider when null. + /// Cancels the request. + /// The providers. + /// The server rejected the request. + Task> ListCustomProvidersAsync(string jwt, CustomProviderType? type = null, CancellationToken cancellationToken = default); + /// /// Links an oauth identity to an existing user. /// diff --git a/packages/Gotrue/Gotrue/ListCustomProviders/CustomProviderListResponse.cs b/packages/Gotrue/Gotrue/ListCustomProviders/CustomProviderListResponse.cs new file mode 100644 index 00000000..c50563f8 --- /dev/null +++ b/packages/Gotrue/Gotrue/ListCustomProviders/CustomProviderListResponse.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text.Json.Serialization; +using Supabase.Gotrue.Common; + +namespace Supabase.Gotrue.ListCustomProviders; + +internal sealed record CustomProviderListResponse +{ + [JsonPropertyName("providers")] + public IReadOnlyList? Providers { get; init; } + + internal static IReadOnlyList Read(CustomProviderListResponse? response) => + (response?.Providers ?? Array.Empty()).Select(provider => provider.ToCustomProvider()).ToList(); +} diff --git a/packages/Gotrue/Gotrue/ListCustomProviders/ListCustomProvidersQuery.cs b/packages/Gotrue/Gotrue/ListCustomProviders/ListCustomProvidersQuery.cs new file mode 100644 index 00000000..f1d04357 --- /dev/null +++ b/packages/Gotrue/Gotrue/ListCustomProviders/ListCustomProvidersQuery.cs @@ -0,0 +1,10 @@ +using System.Collections.Generic; +using Supabase.Gotrue.Common; + +namespace Supabase.Gotrue.ListCustomProviders; + +internal static class ListCustomProvidersQuery +{ + internal static Dictionary? For(CustomProviderType? type) => + type is { } filter ? new Dictionary { ["type"] = Core.Helpers.GetMappedToAttr(filter).Mapping } : null; +} diff --git a/packages/Gotrue/Gotrue/PublicAPI.Unshipped.txt b/packages/Gotrue/Gotrue/PublicAPI.Unshipped.txt index 63c9a8a1..6b8b7a19 100644 --- a/packages/Gotrue/Gotrue/PublicAPI.Unshipped.txt +++ b/packages/Gotrue/Gotrue/PublicAPI.Unshipped.txt @@ -28,9 +28,11 @@ static Supabase.Gotrue.UpdateCustomProvider.CustomProviderChanges.operator !=(Su static Supabase.Gotrue.UpdateCustomProvider.CustomProviderChanges.operator ==(Supabase.Gotrue.UpdateCustomProvider.CustomProviderChanges? left, Supabase.Gotrue.UpdateCustomProvider.CustomProviderChanges? right) -> bool Supabase.Gotrue.AdminClient.CreateCustomProviderAsync(Supabase.Gotrue.CreateCustomProvider.NewCustomProvider! provider, Supabase.Gotrue.Common.CustomProviderOptions? options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! Supabase.Gotrue.AdminClient.GetCustomProviderAsync(string! identifier, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! +Supabase.Gotrue.AdminClient.ListCustomProvidersAsync(Supabase.Gotrue.Common.CustomProviderType? type = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task!>! Supabase.Gotrue.AdminClient.UpdateCustomProviderAsync(string! identifier, Supabase.Gotrue.UpdateCustomProvider.CustomProviderChanges! changes, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! Supabase.Gotrue.Api.CreateCustomProviderAsync(string! jwt, Supabase.Gotrue.CreateCustomProvider.NewCustomProvider! provider, Supabase.Gotrue.Common.CustomProviderOptions? options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! Supabase.Gotrue.Api.GetCustomProviderAsync(string! jwt, string! identifier, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! +Supabase.Gotrue.Api.ListCustomProvidersAsync(string! jwt, Supabase.Gotrue.Common.CustomProviderType? type = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task!>! Supabase.Gotrue.Api.UpdateCustomProviderAsync(string! jwt, string! identifier, Supabase.Gotrue.UpdateCustomProvider.CustomProviderChanges! changes, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! Supabase.Gotrue.Common.CustomProvider Supabase.Gotrue.Common.CustomProvider.$() -> Supabase.Gotrue.Common.CustomProvider! @@ -147,12 +149,14 @@ Supabase.Gotrue.CreateCustomProvider.NewCustomProvider.Type.get -> Supabase.Gotr Supabase.Gotrue.CreateCustomProvider.NewCustomProvider.UserInfoUrl.get -> System.Uri? Supabase.Gotrue.Interfaces.IGotrueAdminClient.CreateCustomProviderAsync(Supabase.Gotrue.CreateCustomProvider.NewCustomProvider! provider, Supabase.Gotrue.Common.CustomProviderOptions? options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! Supabase.Gotrue.Interfaces.IGotrueAdminClient.GetCustomProviderAsync(string! identifier, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! +Supabase.Gotrue.Interfaces.IGotrueAdminClient.ListCustomProvidersAsync(Supabase.Gotrue.Common.CustomProviderType? type = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task!>! Supabase.Gotrue.Interfaces.IGotrueAdminClient.UpdateCustomProviderAsync(string! identifier, Supabase.Gotrue.UpdateCustomProvider.CustomProviderChanges! changes, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! Supabase.Gotrue.Interfaces.IGotrueApi.CreateCustomProviderAsync(string! jwt, Supabase.Gotrue.CreateCustomProvider.NewCustomProvider! provider, Supabase.Gotrue.Common.CustomProviderOptions? options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! Supabase.Gotrue.AdminClient.SignOutAsync(string! accessToken, Supabase.Gotrue.Constants.SignOutScope scope = Supabase.Gotrue.Constants.SignOutScope.Global, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! Supabase.Gotrue.Api.SignOutAsync(string! jwt, Supabase.Gotrue.Constants.SignOutScope scope = Supabase.Gotrue.Constants.SignOutScope.Global, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! Supabase.Gotrue.Interfaces.IGotrueAdminClient.SignOutAsync(string! accessToken, Supabase.Gotrue.Constants.SignOutScope scope = Supabase.Gotrue.Constants.SignOutScope.Global, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! Supabase.Gotrue.Interfaces.IGotrueApi.GetCustomProviderAsync(string! jwt, string! identifier, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! +Supabase.Gotrue.Interfaces.IGotrueApi.ListCustomProvidersAsync(string! jwt, Supabase.Gotrue.Common.CustomProviderType? type = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task!>! Supabase.Gotrue.Interfaces.IGotrueApi.SignOutAsync(string! jwt, Supabase.Gotrue.Constants.SignOutScope scope = Supabase.Gotrue.Constants.SignOutScope.Global, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! Supabase.Gotrue.Interfaces.IGotrueApi.UpdateCustomProviderAsync(string! jwt, string! identifier, Supabase.Gotrue.UpdateCustomProvider.CustomProviderChanges! changes, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! Supabase.Gotrue.UpdateCustomProvider.CustomProviderChanges