diff --git a/src/OpenIddict.Server.AspNetCore.AdminUI/Components/Pages/ApplicationEditorPage.razor b/src/OpenIddict.Server.AspNetCore.AdminUI/Components/Pages/ApplicationEditorPage.razor index 35e43155..86a495d7 100644 --- a/src/OpenIddict.Server.AspNetCore.AdminUI/Components/Pages/ApplicationEditorPage.razor +++ b/src/OpenIddict.Server.AspNetCore.AdminUI/Components/Pages/ApplicationEditorPage.razor @@ -26,6 +26,10 @@ Client type (not specified) + @if (Descriptor.ClientType is { Length: > 0 } customClientType && !OpenIddictServerAspNetCoreAdminUIForms.KnownClientTypes.Contains(customClientType)) + { + @customClientType + } @foreach (var value in OpenIddictServerAspNetCoreAdminUIForms.KnownClientTypes) { @value @@ -35,6 +39,10 @@ Application type (not specified) + @if (Descriptor.ApplicationType is { Length: > 0 } customApplicationType && !OpenIddictServerAspNetCoreAdminUIForms.KnownApplicationTypes.Contains(customApplicationType)) + { + @customApplicationType + } @foreach (var value in OpenIddictServerAspNetCoreAdminUIForms.KnownApplicationTypes) { @value @@ -44,6 +52,10 @@ Consent type (not specified) + @if (Descriptor.ConsentType is { Length: > 0 } customConsentType && !OpenIddictServerAspNetCoreAdminUIForms.KnownConsentTypes.Contains(customConsentType)) + { + @customConsentType + } @foreach (var value in OpenIddictServerAspNetCoreAdminUIForms.KnownConsentTypes) { @value @@ -66,10 +78,10 @@ Redirect URIs Redirect URIs (one per line) - + Post-logout redirect URIs (one per line) - + diff --git a/src/OpenIddict.Server.AspNetCore.AdminUI/Components/Pages/AuthorizationDetailsPage.razor b/src/OpenIddict.Server.AspNetCore.AdminUI/Components/Pages/AuthorizationDetailsPage.razor index 033e84c1..fad28b61 100644 --- a/src/OpenIddict.Server.AspNetCore.AdminUI/Components/Pages/AuthorizationDetailsPage.razor +++ b/src/OpenIddict.Server.AspNetCore.AdminUI/Components/Pages/AuthorizationDetailsPage.razor @@ -58,7 +58,7 @@ { More tokens are attached to this authorization: only the first @Tokens.Count tokens are displayed. - Show the tokens of this subject and client + Show all the tokens of this authorization } @@ -91,11 +91,7 @@ [Parameter] public string? Notice { get; set; } private string TokensUrl => Microsoft.AspNetCore.WebUtilities.QueryHelpers.AddQueryString(BasePath + "/" + Paths.Tokens, - new Dictionary - { - [QueryStringParameters.Subject] = Descriptor.Subject, - [QueryStringParameters.Client] = Descriptor.ApplicationId is { Length: > 0 } id && ClientIds.TryGetValue(id, out var client) ? client : Descriptor.ApplicationId - }.Where(static pair => !string.IsNullOrEmpty(pair.Value))); + QueryStringParameters.Authorization, Id); private string ItemUrl => BasePath + "/" + Paths.Authorizations + "/" + Uri.EscapeDataString(Id); } diff --git a/src/OpenIddict.Server.AspNetCore.AdminUI/Components/Pages/TokenListPage.razor b/src/OpenIddict.Server.AspNetCore.AdminUI/Components/Pages/TokenListPage.razor index 31b607d9..7e6e82c9 100644 --- a/src/OpenIddict.Server.AspNetCore.AdminUI/Components/Pages/TokenListPage.razor +++ b/src/OpenIddict.Server.AspNetCore.AdminUI/Components/Pages/TokenListPage.razor @@ -1,4 +1,4 @@ -@* Lists the tokens (payloads are never rendered), optionally filtered by subject, client, status and type. *@ +@* Lists the tokens (payloads are never rendered), optionally filtered by subject, client, status, type and authorization. *@ @using static OpenIddict.Server.AspNetCore.AdminUI.OpenIddictServerAspNetCoreAdminUIFormatting @@ -25,6 +25,13 @@ Type + @if (Authorization is not null) + { + + Authorization + + + } Filter @@ -80,7 +87,8 @@ [QueryStringParameters.Subject] = Subject, [QueryStringParameters.Client] = Client, [QueryStringParameters.Status] = Status, - [QueryStringParameters.Type] = Type + [QueryStringParameters.Type] = Type, + [QueryStringParameters.Authorization] = Authorization })" /> @@ -106,6 +114,9 @@ /// Gets or sets the type filter, if any. [Parameter] public string? Type { get; set; } + /// Gets or sets the authorization filter, if any. + [Parameter] public string? Authorization { get; set; } + /// Gets or sets the current page number. [Parameter] public int PageNumber { get; set; } = 1; diff --git a/src/OpenIddict.Server.AspNetCore.AdminUI/OpenIddictServerAspNetCoreAdminUIConstants.cs b/src/OpenIddict.Server.AspNetCore.AdminUI/OpenIddictServerAspNetCoreAdminUIConstants.cs index 1cf8174d..9136997f 100644 --- a/src/OpenIddict.Server.AspNetCore.AdminUI/OpenIddictServerAspNetCoreAdminUIConstants.cs +++ b/src/OpenIddict.Server.AspNetCore.AdminUI/OpenIddictServerAspNetCoreAdminUIConstants.cs @@ -55,6 +55,7 @@ public static class OpenIddictServerAspNetCoreAdminUIConstants public static class QueryStringParameters { + public const string Authorization = "authorization"; public const string Client = "client"; public const string Notice = "notice"; public const string Page = "page"; diff --git a/src/OpenIddict.Server.AspNetCore.AdminUI/OpenIddictServerAspNetCoreAdminUIEndpoints.cs b/src/OpenIddict.Server.AspNetCore.AdminUI/OpenIddictServerAspNetCoreAdminUIEndpoints.cs index 3449852a..cbe91829 100644 --- a/src/OpenIddict.Server.AspNetCore.AdminUI/OpenIddictServerAspNetCoreAdminUIEndpoints.cs +++ b/src/OpenIddict.Server.AspNetCore.AdminUI/OpenIddictServerAspNetCoreAdminUIEndpoints.cs @@ -612,12 +612,13 @@ internal sealed class OpenIddictServerAspNetCoreAdminUIEndpoints var client = GetQuery(context, QueryStringParameters.Client); var status = GetQuery(context, QueryStringParameters.Status); var type = GetQuery(context, QueryStringParameters.Type); + var authorization = GetQuery(context, QueryStringParameters.Authorization); List<(string Id, OpenIddictTokenDescriptor Descriptor)> items = []; var next = false; var (found, application) = await ResolveApplicationIdAsync(applications, client, context.RequestAborted); - if (found) + if (found && authorization is null) { await foreach (var token in Operations.ListTokensAsync(manager, subject, application, status, type, size + 1, offset, context.RequestAborted)) @@ -632,6 +633,39 @@ internal sealed class OpenIddictServerAspNetCoreAdminUIEndpoints } } + // Note: the token stores can't combine the authorization filter with the other filters: + // when an authorization identifier is specified, the tokens attached to the authorization + // are retrieved and the other filters (and the pagination) are applied in memory. + else if (found) + { + var skipped = 0; + + await foreach (var token in manager.FindByAuthorizationIdAsync(authorization!, context.RequestAborted)) + { + var item = await DescribeTokenAsync(manager, token, context.RequestAborted); + if ((subject is not null && !string.Equals(item.Descriptor.Subject, subject, StringComparison.Ordinal)) || + (application is not null && !string.Equals(item.Descriptor.ApplicationId, application, StringComparison.Ordinal)) || + (status is not null && !string.Equals(item.Descriptor.Status, status, StringComparison.Ordinal)) || + (type is not null && !string.Equals(item.Descriptor.Type, type, StringComparison.Ordinal))) + { + continue; + } + + if (skipped++ < offset) + { + continue; + } + + if (items.Count == size) + { + next = true; + break; + } + + items.Add(item); + } + } + return Page(context, new() { [nameof(TokenListPage.Items)] = items, @@ -641,6 +675,7 @@ internal sealed class OpenIddictServerAspNetCoreAdminUIEndpoints [nameof(TokenListPage.Client)] = client, [nameof(TokenListPage.Status)] = status, [nameof(TokenListPage.Type)] = type, + [nameof(TokenListPage.Authorization)] = authorization, [nameof(TokenListPage.PageNumber)] = page, [nameof(TokenListPage.HasNextPage)] = next, [nameof(TokenListPage.Notice)] = GetQuery(context, QueryStringParameters.Notice) diff --git a/src/OpenIddict.Server.AspNetCore.AdminUI/OpenIddictServerAspNetCoreAdminUIForms.cs b/src/OpenIddict.Server.AspNetCore.AdminUI/OpenIddictServerAspNetCoreAdminUIForms.cs index 5074744d..ef2cc3b4 100644 --- a/src/OpenIddict.Server.AspNetCore.AdminUI/OpenIddictServerAspNetCoreAdminUIForms.cs +++ b/src/OpenIddict.Server.AspNetCore.AdminUI/OpenIddictServerAspNetCoreAdminUIForms.cs @@ -87,9 +87,9 @@ internal static class OpenIddictServerAspNetCoreAdminUIForms descriptor.ClientId = GetString(form, FormFields.ClientId); descriptor.DisplayName = GetString(form, FormFields.DisplayName); - descriptor.ApplicationType = GetChoice(form, FormFields.ApplicationType, KnownApplicationTypes, errors); - descriptor.ClientType = GetChoice(form, FormFields.ClientType, KnownClientTypes, errors); - descriptor.ConsentType = GetChoice(form, FormFields.ConsentType, KnownConsentTypes, errors); + descriptor.ApplicationType = GetChoice(form, FormFields.ApplicationType, KnownApplicationTypes, descriptor.ApplicationType, errors); + descriptor.ClientType = GetChoice(form, FormFields.ClientType, KnownClientTypes, descriptor.ClientType, errors); + descriptor.ConsentType = GetChoice(form, FormFields.ConsentType, KnownConsentTypes, descriptor.ConsentType, errors); ReadValues(form, FormFields.Permissions, FormFields.AdditionalPermissions, descriptor.Permissions); ReadValues(form, FormFields.Requirements, FormFields.AdditionalRequirements, descriptor.Requirements); @@ -154,10 +154,15 @@ internal static class OpenIddictServerAspNetCoreAdminUIForms public static string[] SplitLines(string? value) => value?.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) ?? []; - private static string? GetChoice(IFormCollection form, string name, ImmutableArray choices, List errors) + private static string? GetChoice(IFormCollection form, string name, + ImmutableArray choices, string? current, List errors) { + // Note: custom values (e.g application or consent types not known by the admin UI) can't be + // selected by the user, but are rendered as an additional option when they are already stored + // so that saving the form doesn't silently erase them: such values are always accepted as-is. var value = GetString(form, name); - if (value is not null && !choices.Contains(value, StringComparer.Ordinal)) + if (value is not null && !choices.Contains(value, StringComparer.Ordinal) && + !string.Equals(value, current, StringComparison.Ordinal)) { errors.Add(SR.FormatID2341(name)); return null; diff --git a/test/OpenIddict.Server.AspNetCore.AdminUI.Tests/OpenIddictServerAspNetCoreAdminUITests.cs b/test/OpenIddict.Server.AspNetCore.AdminUI.Tests/OpenIddictServerAspNetCoreAdminUITests.cs index deb42314..2724743e 100644 --- a/test/OpenIddict.Server.AspNetCore.AdminUI.Tests/OpenIddictServerAspNetCoreAdminUITests.cs +++ b/test/OpenIddict.Server.AspNetCore.AdminUI.Tests/OpenIddictServerAspNetCoreAdminUITests.cs @@ -1150,10 +1150,162 @@ public partial class OpenIddictServerAspNetCoreAdminUITests // Assert Assert.Contains("only the first 2 tokens are displayed", html, StringComparison.Ordinal); - Assert.Contains("href=\"/openiddict/admin/tokens?subject=alice\"", html, StringComparison.Ordinal); + Assert.Contains("href=\"/openiddict/admin/tokens?authorization=authz-1\"", html, StringComparison.Ordinal); Assert.Equal(2, Regex.Matches(html, "token", RegexOptions.None, TimeSpan.FromSeconds(1)).Count); } + [Fact] + public async Task UpdateApplication_PreservesUnchangedRedirectUrisAndCustomTypesVerbatim() + { + // Arrange + var application = new object(); + OpenIddictApplicationDescriptor? updated = null; + + var manager = new Mock(); + manager.Setup(mock => mock.FindByIdAsync("1", It.IsAny())).ReturnsAsync(application); + manager.Setup(mock => mock.GetIdAsync(application, It.IsAny())).ReturnsAsync("1"); + manager.Setup(mock => mock.PopulateAsync(It.IsAny(), application, It.IsAny())) + .Callback((OpenIddictApplicationDescriptor descriptor, object _, CancellationToken _) => + { + descriptor.ClientId = "fabrikam"; + descriptor.ApplicationType = "custom_application_type"; + descriptor.ConsentType = "custom_consent_type"; + descriptor.RedirectUris.Add(new Uri("http://localhost:3000")); + descriptor.RedirectUris.Add(new Uri("https://Client.Example/cb?a=%7e")); + descriptor.PostLogoutRedirectUris.Add(new Uri("https://Client.Example")); + }) + .Returns(ValueTask.CompletedTask); + manager.Setup(mock => mock.UpdateAsync(application, It.IsAny(), It.IsAny())) + .Callback((object _, OpenIddictApplicationDescriptor descriptor, CancellationToken _) => updated = descriptor) + .Returns(ValueTask.CompletedTask); + + using var host = await CreateHostAsync(services => services.AddSingleton(manager.Object)); + using var client = CreateClient(host, role: "admin"); + + var html = await client.GetStringAsync("/openiddict/admin/applications/1"); + var antiforgery = await GetAntiforgeryAsync(client, "/openiddict/admin/applications/1"); + + // Act: post the rendered values back unchanged, as a browser would. + var response = await PostAsync(client, antiforgery, "/openiddict/admin/applications/1", + [ + new("client_id", "fabrikam"), + new("display_name", "Fabrikam (renamed)"), + new("application_type", GetSelectedOption(html, "application_type")), + new("client_type", GetSelectedOption(html, "client_type")), + new("consent_type", GetSelectedOption(html, "consent_type")), + new("redirect_uris", GetTextArea(html, "redirect_uris")), + new("post_logout_redirect_uris", GetTextArea(html, "post_logout_redirect_uris")) + ]); + + // Assert + Assert.Equal(HttpStatusCode.Redirect, response.StatusCode); + Assert.NotNull(updated); + Assert.Equal("Fabrikam (renamed)", updated.DisplayName); + Assert.Equal("custom_application_type", updated.ApplicationType); + Assert.Equal("custom_consent_type", updated.ConsentType); + Assert.Null(updated.ClientType); + Assert.Equal(["http://localhost:3000", "https://Client.Example/cb?a=%7e"], + updated.RedirectUris.Select(static uri => uri.OriginalString).Order(StringComparer.Ordinal), + StringComparer.Ordinal); + Assert.Equal("https://Client.Example", Assert.Single(updated.PostLogoutRedirectUris).OriginalString); + + static string GetTextArea(string html, string id) + { + var match = Regex.Match(html, $"]*>(?[^<]*)", + RegexOptions.None, TimeSpan.FromSeconds(1)); + Assert.True(match.Success); + return WebUtility.HtmlDecode(match.Groups["value"].Value); + } + + static string GetSelectedOption(string html, string id) + { + var select = Regex.Match(html, $"]*>(?.*?)", + RegexOptions.Singleline, TimeSpan.FromSeconds(1)); + Assert.True(select.Success); + var option = Regex.Match(select.Groups["options"].Value, "[^\"]*)\")?[^>]*\\bselected", + RegexOptions.None, TimeSpan.FromSeconds(1)); + Assert.True(option.Success, select.Value); + return WebUtility.HtmlDecode(option.Groups["value"].Value); + } + } + + [Fact] + public async Task UpdateApplication_RejectsUnknownTypesThatAreNotStored() + { + // Arrange + var application = new object(); + + var manager = new Mock(); + manager.Setup(mock => mock.FindByIdAsync("1", It.IsAny())).ReturnsAsync(application); + manager.Setup(mock => mock.GetIdAsync(application, It.IsAny())).ReturnsAsync("1"); + manager.Setup(mock => mock.PopulateAsync(It.IsAny(), application, It.IsAny())) + .Callback((OpenIddictApplicationDescriptor descriptor, object _, CancellationToken _) => + { + descriptor.ClientId = "fabrikam"; + descriptor.ApplicationType = "custom_application_type"; + }) + .Returns(ValueTask.CompletedTask); + + using var host = await CreateHostAsync(services => services.AddSingleton(manager.Object)); + using var client = CreateClient(host, role: "admin"); + + var antiforgery = await GetAntiforgeryAsync(client, "/openiddict/admin/applications/1"); + + // Act + var response = await PostAsync(client, antiforgery, "/openiddict/admin/applications/1", + [ + new("client_id", "fabrikam"), + new("application_type", "another_application_type") + ]); + + // Assert + Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); + manager.Verify(mock => mock.UpdateAsync(It.IsAny(), + It.IsAny(), It.IsAny()), Times.Never()); + } + + [Fact] + public async Task ListTokens_FiltersByAuthorization() + { + // Arrange + var (first, second, third) = (new object(), new object(), new object()); + + var applications = new Mock(); + + var manager = new Mock(); + manager.Setup(mock => mock.FindByAuthorizationIdAsync("authz-1", It.IsAny())) + .Returns(() => EnumerateAsync(first, second, third)); + manager.Setup(mock => mock.GetIdAsync(first, It.IsAny())).ReturnsAsync("token-1"); + manager.Setup(mock => mock.GetIdAsync(second, It.IsAny())).ReturnsAsync("token-2"); + manager.Setup(mock => mock.GetIdAsync(third, It.IsAny())).ReturnsAsync("token-3"); + manager.Setup(mock => mock.PopulateAsync(It.IsAny(), It.IsAny(), It.IsAny())) + .Callback((OpenIddictTokenDescriptor descriptor, object token, CancellationToken _) => + { + descriptor.Subject = "alice"; + descriptor.Status = ReferenceEquals(token, second) ? Statuses.Revoked : Statuses.Valid; + }) + .Returns(ValueTask.CompletedTask); + + using var host = await CreateHostAsync(services => services + .AddSingleton(applications.Object) + .AddSingleton(manager.Object), configuration: options => options.PageSize = 1); + using var client = CreateClient(host, role: "admin"); + + // Act + var page1 = await client.GetStringAsync("/openiddict/admin/tokens?authorization=authz-1&status=valid"); + var page2 = await client.GetStringAsync("/openiddict/admin/tokens?authorization=authz-1&status=valid&page=2"); + + // Assert + Assert.Contains("token-1", page1, StringComparison.Ordinal); + Assert.DoesNotContain("token-2", page1, StringComparison.Ordinal); + Assert.Contains("value=\"authz-1\"", page1, StringComparison.Ordinal); + Assert.Contains("authorization=authz-1&page=2", page1, StringComparison.Ordinal); + Assert.Contains("token-3", page2, StringComparison.Ordinal); + Assert.DoesNotContain("token-1", page2, StringComparison.Ordinal); + manager.Verify(mock => mock.FindAsync(It.IsAny<(string?, string?, string?, string?)>(), It.IsAny()), Times.Never()); + manager.Verify(mock => mock.ListAsync(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never()); + } + [Fact] public async Task MapOpenIddictAdminUI_DetectsConflictingEndpoints() {
More tokens are attached to this authorization: only the first @Tokens.Count tokens are displayed. - Show the tokens of this subject and client + Show all the tokens of this authorization
token
token-1
token-2
token-3