From a151cb76e360dbc38a9dc1bc4e77ed22a5b7ca9a Mon Sep 17 00:00:00 2001 From: tr00d Date: Thu, 29 Oct 2026 15:23:27 +0100 Subject: [PATCH] feat(auth): list custom OAuth/OIDC providers as an admin --- .../ListCustomProvidersContractTests.cs | 106 ++++++++++++++++++ .../ListCustomProvidersTests.cs | 42 +++++++ packages/Gotrue/Gotrue/AdminClient.cs | 5 + packages/Gotrue/Gotrue/Api.cs | 8 ++ .../Gotrue/Interfaces/IGotrueAdminClient.cs | 23 ++++ .../Gotrue/Gotrue/Interfaces/IGotrueApi.cs | 11 ++ .../CustomProviderListResponse.cs | 16 +++ .../ListCustomProvidersQuery.cs | 10 ++ .../Gotrue/Gotrue/PublicAPI.Unshipped.txt | 4 + 9 files changed, 225 insertions(+) create mode 100644 packages/Gotrue/Gotrue.Tests/ListCustomProviders/ListCustomProvidersContractTests.cs create mode 100644 packages/Gotrue/Gotrue.Tests/ListCustomProviders/ListCustomProvidersTests.cs create mode 100644 packages/Gotrue/Gotrue/ListCustomProviders/CustomProviderListResponse.cs create mode 100644 packages/Gotrue/Gotrue/ListCustomProviders/ListCustomProvidersQuery.cs 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 1cc9b2ea..1e4e0c5e 100644 --- a/packages/Gotrue/Gotrue/AdminClient.cs +++ b/packages/Gotrue/Gotrue/AdminClient.cs @@ -159,6 +159,11 @@ private static string RequireAccessToken(string accessToken) => public Task CreateCustomProviderAsync(NewCustomProvider provider, CustomProviderOptions? options = null, CancellationToken cancellationToken = default) => this.api.CreateCustomProviderAsync(this.serviceKey, provider, options, 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 d4875a70..90ba3f31 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; @@ -652,6 +653,13 @@ public async Task CreateCustomProviderAsync(string jwt, NewCusto return CustomProviderResponse.Read(created); } + /// + 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); + } + /// public async Task LinkIdentity(string token, Provider provider, SignInOptions options) { diff --git a/packages/Gotrue/Gotrue/Interfaces/IGotrueAdminClient.cs b/packages/Gotrue/Gotrue/Interfaces/IGotrueAdminClient.cs index fbf0c7e2..68d54278 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; @@ -194,4 +195,26 @@ public interface IGotrueAdminClient : IGettableHeaders /// /// Task CreateCustomProviderAsync(NewCustomProvider provider, CustomProviderOptions? options = null, 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 d3fa36d5..828cf076 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; @@ -92,6 +93,16 @@ public interface IGotrueApi : IGettableHeaders /// The server rejected the request. Task CreateCustomProviderAsync(string jwt, NewCustomProvider provider, CustomProviderOptions? options = null, 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 2c0e4de1..dedde77d 100644 --- a/packages/Gotrue/Gotrue/PublicAPI.Unshipped.txt +++ b/packages/Gotrue/Gotrue/PublicAPI.Unshipped.txt @@ -22,7 +22,9 @@ static Supabase.Gotrue.CreateCustomProvider.NewCustomProvider.Oidc(string! ident static Supabase.Gotrue.CreateCustomProvider.NewCustomProvider.operator !=(Supabase.Gotrue.CreateCustomProvider.NewCustomProvider? left, Supabase.Gotrue.CreateCustomProvider.NewCustomProvider? right) -> bool static Supabase.Gotrue.CreateCustomProvider.NewCustomProvider.operator ==(Supabase.Gotrue.CreateCustomProvider.NewCustomProvider? left, Supabase.Gotrue.CreateCustomProvider.NewCustomProvider? 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.ListCustomProvidersAsync(Supabase.Gotrue.Common.CustomProviderType? type = null, 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.ListCustomProvidersAsync(string! jwt, Supabase.Gotrue.Common.CustomProviderType? type = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task!>! Supabase.Gotrue.Common.CustomProvider Supabase.Gotrue.Common.CustomProvider.$() -> Supabase.Gotrue.Common.CustomProvider! Supabase.Gotrue.Common.CustomProvider.AcceptableClientIds.get -> System.Collections.Generic.IReadOnlyList! @@ -137,8 +139,10 @@ Supabase.Gotrue.CreateCustomProvider.NewCustomProvider.TokenUrl.get -> System.Ur Supabase.Gotrue.CreateCustomProvider.NewCustomProvider.Type.get -> Supabase.Gotrue.Common.CustomProviderType 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.ListCustomProvidersAsync(Supabase.Gotrue.Common.CustomProviderType? type = null, 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.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.ListCustomProvidersAsync(string! jwt, Supabase.Gotrue.Common.CustomProviderType? type = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task!>!