Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/apps/api-tests/OAuthTestClient.cs
diff options
context:
space:
mode:
authorLeitwolf112026-10-23 04:49:52 +0300
committerLeitwolf112026-10-23 04:49:52 +0300
commitcffc8d884ce3620b70c80a9ab98b8f1f03887402 (patch)
tree2f74154b91cd33e170ae9726038880cb25548449 /apps/api-tests/OAuthTestClient.cs
parent46e526f91e805319e0b0b65bae8d09dc4fabfa0c (diff)
downloadprogmaweb-cffc8d884ce3620b70c80a9ab98b8f1f03887402.tar.gz
Add Progmasoft OAuth, an OAuth 2.1 and OpenID Connect provider
Progmasoft accounts can now sign in to any application, and any developer can register one. The provider runs in the account API under the Progmasoft.OAuth namespaces and answers on credentials.progmasoft.com. Protocol: OpenIddict 7 (Apache-2.0) with its Entity Framework stores. Authorization code with PKCE for every client, S256 only; refresh tokens with rotation and no reuse leeway, so a replayed refresh token revokes its chain; client credentials for confidential clients, without account scopes. No implicit or password grant. Codes live 2 minutes, access and identity tokens 15 minutes, refresh tokens 30 days. Signing (RSA) and encryption keys are read from files in production and are required there; other environments make keys per process. Scopes, one grant each, as the consent page lists them: profile (the account name), email (the sign-in address; email_verified is false, since registration does not verify it), viget.packages.read, viget.packages.publish and viget.packages.yank (access tokens for the viget audience), and offline_access. Pages, plain HTML without script, behind a content security policy that allows the one stylesheet by digest: sign-in with the account password (a cookie of its own, no account-site session), consent with the application, its registering account and the return host, the developer portal (register confidential or public clients, redirect address rules from RFC 8252, scopes, client credentials, secret shown once and rotatable, delete; 10 applications per account; other accounts' applications are absent), and the applications an account has allowed, with withdrawal that revokes their tokens. Forms carry antiforgery tokens; return addresses stay on the host. Storage: the oauth_* tables in the account database, created by a numbered schema step like the account tables; the step is the script Entity Framework generates for the model, and a test compares the two. With the memory account store they live in a shared in-memory SQLite database, which runs the bulk revocations the in-memory provider cannot. A host filter keeps OAuth paths on the OAuth host and everything but OAuth and /health off it. AllowedHosts names the new host, and a rate limit policy covers the protocol endpoints and the portal. The Nginx vhost for the host and the key set-up are in ops; docs/OAUTH.md is the developer guide. AccountService gains VerifyPasswordAsync, which LoginAsync now uses, and FindAsync. Verified: dotnet build with warnings as errors; dotnet test, 125 tests, 109 passed and 16 PostgreSQL tests skipped locally (CI runs them, including a new one for the schema and refresh token chain revocation): code flow with PKCE and the claims of each scope, single-use codes, refresh rotation and reuse refusal, consent reuse and withdrawal, deny, public client with a private-use scheme, client credentials, ViGet audience by introspection, refusal without PKCE, of other flows and of unpermitted scopes, unregistered redirect addresses, sign-in redirects and prompt=none, the portal's address rules and ownership, secret rotation, the page security policy, the host filter and the discovery document.
Diffstat (limited to 'apps/api-tests/OAuthTestClient.cs')
-rw-r--r--apps/api-tests/OAuthTestClient.cs169
1 files changed, 169 insertions, 0 deletions
diff --git a/apps/api-tests/OAuthTestClient.cs b/apps/api-tests/OAuthTestClient.cs
new file mode 100644
index 0000000..5453027
--- /dev/null
+++ b/apps/api-tests/OAuthTestClient.cs
@@ -0,0 +1,169 @@
+// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com>
+// SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.1
+
+using System.Net;
+using System.Net.Http.Json;
+using System.Security.Cryptography;
+using System.Text;
+using System.Text.Json;
+using System.Text.RegularExpressions;
+using System.Web;
+
+namespace Progmasoft.Progmaweb.Api.Tests;
+
+/// <summary>An account holder in a browser on the OAuth host, and a developer's client application.</summary>
+/// <remarks>The pages are plain HTML forms; this fills them the way a browser would.</remarks>
+internal sealed partial class OAuthTestClient(AccountApiFactory factory)
+{
+ public const string RedirectUri = "https://client.example/callback";
+
+ public HttpClient Browser { get; } = factory.CreateOAuthClient();
+
+ private static CancellationToken Cancel => TestContext.Current.CancellationToken;
+
+ /// <summary>Creates an account through the account API and signs it in to the OAuth host.</summary>
+ public async Task<string> RegisterAndSignInAsync(string accountName, string email)
+ {
+ using HttpClient accounts = factory.CreateAccountClient(handleCookies: false);
+ HttpResponseMessage registered = await accounts.PostAsJsonAsync("/api/v1/accounts/register",
+ new { accountName, email, password = "correct horse battery staple" }, Cancel);
+ Assert.Equal(HttpStatusCode.Created, registered.StatusCode);
+
+ HttpResponseMessage signedIn = await SubmitAsync("/signin", "/signin", new Dictionary<string, string>
+ {
+ ["email"] = email,
+ ["password"] = "correct horse battery staple",
+ ["returnUrl"] = "/"
+ });
+ Assert.Equal(HttpStatusCode.Redirect, signedIn.StatusCode);
+ return accountName;
+ }
+
+ /// <summary>Registers an application in the developer portal and returns its identifier and secret.</summary>
+ public async Task<(string ClientId, string? Secret)> RegisterApplicationAsync(
+ string scopes = "profile email offline_access viget.packages.read",
+ bool isPublic = false,
+ bool clientCredentials = false,
+ string redirectUris = RedirectUri)
+ {
+ List<KeyValuePair<string, string>> fields =
+ [
+ new("displayName", "Example Client"),
+ new("homepage", "https://client.example"),
+ new("clientType", isPublic ? "public" : "confidential"),
+ new("redirectUris", redirectUris),
+ new("postLogoutRedirectUris", "https://client.example/signed-out")
+ ];
+ fields.AddRange(scopes.Split(' ', StringSplitOptions.RemoveEmptyEntries)
+ .Select(scope => new KeyValuePair<string, string>("scope", scope)));
+ if (clientCredentials)
+ {
+ fields.Add(new("allowClientCredentials", "on"));
+ }
+
+ HttpResponseMessage response = await SubmitAsync("/developers/new", "/developers/new", fields);
+ string html = await response.Content.ReadAsStringAsync(Cancel);
+ Assert.True(response.StatusCode == HttpStatusCode.OK, html);
+ MatchCollection codes = CodePattern().Matches(html);
+ string clientId = codes[0].Groups[1].Value;
+ Assert.StartsWith("pmo_", clientId, StringComparison.Ordinal);
+ return (clientId, isPublic ? null : HttpUtility.HtmlDecode(codes[1].Groups[1].Value));
+ }
+
+ /// <summary>Loads a page with a form, then posts the form with the given fields and its token.</summary>
+ public async Task<HttpResponseMessage> SubmitAsync(
+ string page,
+ string action,
+ IEnumerable<KeyValuePair<string, string>> fields)
+ {
+ string html = await (await Browser.GetAsync(page, Cancel)).Content.ReadAsStringAsync(Cancel);
+ return await Browser.PostAsync(action, new FormUrlEncodedContent(
+ fields.Append(new("form-token", FormToken(html)))), Cancel);
+ }
+
+ /// <summary>The token of the first form of a page.</summary>
+ public static string FormToken(string html) =>
+ HttpUtility.HtmlDecode(TokenPattern().Match(html).Groups[1].Value);
+
+ /// <summary>Every hidden field of the first form of a page, the token included.</summary>
+ public static List<KeyValuePair<string, string>> HiddenFields(string html) => HiddenPattern().Matches(html)
+ .Select(match => new KeyValuePair<string, string>(
+ HttpUtility.HtmlDecode(match.Groups[1].Value), HttpUtility.HtmlDecode(match.Groups[2].Value)))
+ .ToList();
+
+ /// <summary>A PKCE verifier and its S256 challenge.</summary>
+ public static (string Verifier, string Challenge) Pkce()
+ {
+ string verifier = Base64Url(RandomNumberGenerator.GetBytes(32));
+ return (verifier, Base64Url(SHA256.HashData(Encoding.ASCII.GetBytes(verifier))));
+ }
+
+ /// <summary>The query of an authorization request.</summary>
+ public static string AuthorizePath(string clientId, string scope, string? challenge, string redirectUri = RedirectUri,
+ string responseType = "code", string? prompt = null)
+ {
+ Dictionary<string, string?> query = new()
+ {
+ ["response_type"] = responseType,
+ ["client_id"] = clientId,
+ ["redirect_uri"] = redirectUri,
+ ["scope"] = scope,
+ ["state"] = "state-123",
+ ["nonce"] = "nonce-456",
+ ["code_challenge"] = challenge,
+ ["code_challenge_method"] = challenge is null ? null : "S256",
+ ["prompt"] = prompt
+ };
+ return "/connect/authorize?" + string.Join('&', query.Where(pair => pair.Value is not null)
+ .Select(pair => $"{pair.Key}={Uri.EscapeDataString(pair.Value!)}"));
+ }
+
+ /// <summary>Asks for an authorization and allows it on the consent page; returns the redirection.</summary>
+ public async Task<Uri> AuthorizeAsync(string clientId, string scope, string challenge, string decision = "allow")
+ {
+ HttpResponseMessage consent = await Browser.GetAsync(AuthorizePath(clientId, scope, challenge), Cancel);
+ if (consent.StatusCode == HttpStatusCode.Redirect)
+ {
+ return consent.Headers.Location!;
+ }
+
+ string html = await consent.Content.ReadAsStringAsync(Cancel);
+ Assert.True(consent.StatusCode == HttpStatusCode.OK, html);
+ List<KeyValuePair<string, string>> fields = HiddenFields(html);
+ fields.Add(new("decision", decision));
+ HttpResponseMessage decided = await Browser.PostAsync("/connect/authorize", new FormUrlEncodedContent(fields), Cancel);
+ Assert.Equal(HttpStatusCode.Redirect, decided.StatusCode);
+ return decided.Headers.Location!;
+ }
+
+ /// <summary>Posts to the token endpoint as a client would, from outside the browser.</summary>
+ public async Task<(HttpStatusCode Status, JsonElement Body)> TokenAsync(Dictionary<string, string> fields)
+ {
+ using HttpClient client = factory.CreateOAuthClient();
+ HttpResponseMessage response = await client.PostAsync("/connect/token", new FormUrlEncodedContent(fields), Cancel);
+ return (response.StatusCode, await response.Content.ReadFromJsonAsync<JsonElement>(Cancel));
+ }
+
+ /// <summary>The payload of a JSON web token, without checking its signature.</summary>
+ public static JsonElement Payload(string jwt)
+ {
+ string part = jwt.Split('.')[1].Replace('-', '+').Replace('_', '/');
+ part = part.PadRight(part.Length + (4 - part.Length % 4) % 4, '=');
+ return JsonDocument.Parse(Convert.FromBase64String(part)).RootElement.Clone();
+ }
+
+ /// <summary>A query parameter of an address.</summary>
+ public static string? Query(Uri uri, string name) => HttpUtility.ParseQueryString(uri.Query)[name];
+
+ private static string Base64Url(byte[] bytes) =>
+ Convert.ToBase64String(bytes).TrimEnd('=').Replace('+', '-').Replace('/', '_');
+
+ [GeneratedRegex("name=\"form-token\" value=\"([^\"]*)\"")]
+ private static partial Regex TokenPattern();
+
+ [GeneratedRegex("<input type=\"hidden\" name=\"([^\"]*)\" value=\"([^\"]*)\">")]
+ private static partial Regex HiddenPattern();
+
+ [GeneratedRegex("<code>([^<]*)</code>")]
+ private static partial Regex CodePattern();
+}