// SPDX-FileCopyrightText: 2026 Progmasoft // SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.1 using System.Collections.Immutable; using OpenIddict.Abstractions; using static OpenIddict.Abstractions.OpenIddictConstants; namespace Progmasoft.OAuth.Pages; /// The applications an account has allowed, and withdrawing an allowance. internal static class AllowedApplicationsEndpoints { /// Maps the pages. /// The application. public static void MapAllowedApplications(this WebApplication app) { app.MapGet("/account/applications", ListAsync); app.MapPost("/account/applications/{authorizationId}/revoke", RevokeAsync); } /// Lists the applications the signed-in account has allowed. private static async Task ListAsync( HttpContext context, IOpenIddictAuthorizationManager authorizations, IOpenIddictApplicationManager applications) { if (await OAuthAccount.CurrentAsync(context) is not { } account) { return OAuthAccount.SignInFirst(context); } List rows = []; await foreach (object authorization in authorizations.FindBySubjectAsync(account.Id.ToString(), context.RequestAborted)) { if (!await authorizations.HasStatusAsync(authorization, Statuses.Valid, context.RequestAborted) || !await authorizations.HasTypeAsync(authorization, AuthorizationTypes.Permanent, context.RequestAborted)) { continue; } string? applicationId = await authorizations.GetApplicationIdAsync(authorization, context.RequestAborted); object? application = applicationId is null ? null : await applications.FindByIdAsync(applicationId, context.RequestAborted); string name = application is null ? "A deleted application" : await applications.GetDisplayNameAsync(application, context.RequestAborted) ?? "An application"; ImmutableArray scopes = await authorizations.GetScopesAsync(authorization, context.RequestAborted); string id = (await authorizations.GetIdAsync(authorization, context.RequestAborted))!; DateTimeOffset? created = await authorizations.GetCreationDateAsync(authorization, context.RequestAborted); string grants = string.Join(", ", scopes.Select(scope => $"{HtmlPage.Encode(scope)}")); rows.Add( $"
  • {HtmlPage.Encode(name)} " + $"since {created:yyyy-MM-dd}
    {grants}" + HtmlPage.Form(context, $"/account/applications/{Uri.EscapeDataString(id)}/revoke", "") + "
  • "); } string body = "

    Applications you have allowed

    " + (rows.Count == 0 ? "

    You have not allowed any application.

    " : $"
      {string.Concat(rows)}
    ") + "

    Withdrawing ends the application's access tokens and refresh tokens at once; " + "it will have to ask you again.

    Back

    "; return HtmlPage.Render(context, "Allowed applications", body); } /// Withdraws an allowance and revokes the tokens issued under it. private static async Task RevokeAsync( HttpContext context, IOpenIddictAuthorizationManager authorizations, IOpenIddictTokenManager tokens, string authorizationId) { if (await OAuthAccount.CurrentAsync(context) is not { } account) { return OAuthAccount.SignInFirst(context); } if (!await OAuthAccount.IsGenuineFormAsync(context)) { return Results.BadRequest(); } object? authorization = await authorizations.FindByIdAsync(authorizationId, context.RequestAborted); // Another account's authorization is treated as absent. if (authorization is not null && await authorizations.GetSubjectAsync(authorization, context.RequestAborted) == account.Id.ToString()) { await authorizations.TryRevokeAsync(authorization, context.RequestAborted); await tokens.RevokeByAuthorizationIdAsync(authorizationId, context.RequestAborted); } return Results.Redirect("/account/applications"); } }