Add the OIDC client: PKCE loopback sign-in and the key binding flow

Authorization Code with PKCE on a loopback redirect, per RFC 6749, RFC 7636
and RFC 8252. Zero package references: the flow is fully specified, and the
one thing a library would own for us -- nonce generation and validation -- is
exactly what the key binding needs to control. Duende's OidcClient generates
and validates its own nonce as an internal detail, and the binding requires
the nonce be a specific value: the hash of the key statement being enrolled.
Fighting that is worse than owning the flow.

AuthorizeKeyBindingAsync is the client half of the primary trust anchor. It
runs a second authorization with nonce set to the statement hash and
prompt=login, so the ID token that returns is the provider's signature over
exactly those public keys, attesting to a user present now rather than to a
session opened at some unknown earlier time. It requests only openid -- a
second refresh token would be one more long-lived credential for no benefit
-- and rejects a token whose nonce is not the one it asked for, because
enrolling that would store evidence verifying against keys we are not
publishing.

The nonce is read without validating the ID token's signature. Sanctioned by
OIDC Core 3.1.3.7: for a token received by direct communication with the
token endpoint, TLS server authentication may stand in for signature
checking. That reasoning does not extend to another user's binding, which
arrives via the DodoSSH server and must be verified against JWKS fetched
directly -- the directory work in M3.

Raw TcpListener rather than HttpListener for the redirect: an ephemeral port
can be bound and read atomically instead of picking one and hoping it is
still free, there is no HTTP.SYS URL-ACL question on Windows, and the whole
surface is one request line. It answers 404 on other paths and keeps
waiting, because a browser asks for /favicon.ico first and treating that as
the callback would abort every sign-in. 127.0.0.1 rather than localhost: RFC
8252 permits either, but the name resolves through the hosts file.

20 tests, driving the real listener over TCP with a fake browser that
actually fetches the redirect -- injecting a fabricated callback would skip
the parsing, path filtering and response writing that can break. Mostly
negative, because the loopback port is reachable by every local process: a
response with the wrong state is rejected *and* never reaches the token
endpoint, metadata declaring an issuer other than its own authority is
rejected (RFC 8414 3.3, without which a mix-up attack works), a provider
offering only 'plain' is fatal rather than a silent downgrade, and the
verifier sent is checked against the challenge advertised so PKCE is not
theatre that only fails in production.

Two bugs caught by writing the tests: the authorize URL builder dropped
client_id entirely after a refactor, and CancellationTokenSource.CancelAfter
has no TimeProvider overload -- so the browser timeout is now constructed
with the clock and a test can advance it instead of waiting five minutes.
This commit is contained in:
2026-07-28 21:13:35 +02:00
parent e65d738912
commit 94f66be5e8
16 changed files with 3122 additions and 0 deletions
+38
View File
@@ -0,0 +1,38 @@
namespace DodoSSH.Client.Auth;
/// <summary>What a token exchange returned.</summary>
/// <remarks>
/// The refresh token is the one long-lived credential the client holds, and the only part of this
/// that belongs in the OS keystore. The access token is short-lived and re-obtainable, and the ID
/// token is an assertion rather than a credential. None of them can open the vault: that needs the
/// passphrase, which is never stored anywhere.
/// </remarks>
/// <param name="AccessToken">Bearer token for the DodoSSH API.</param>
/// <param name="RefreshToken">Refresh token, when <c>offline_access</c> was granted.</param>
/// <param name="IdToken">Identity assertion, when <c>openid</c> was requested.</param>
/// <param name="ExpiresAtUtc">When the access token stops being accepted.</param>
/// <param name="Scope">Scopes actually granted, which may be narrower than those requested.</param>
public sealed record TokenSet(
string AccessToken,
string? RefreshToken,
string? IdToken,
DateTimeOffset ExpiresAtUtc,
string? Scope)
{
/// <summary>
/// Whether the access token should be refreshed before use.
/// </summary>
/// <remarks>
/// The margin exists because expiry is checked here and enforced by the server after a network
/// round trip. Without it a token that is valid at the moment of the check is rejected by the
/// time it arrives, which surfaces as a random 401 mid-sync.
/// </remarks>
/// <param name="clock">Time source.</param>
/// <param name="margin">How far ahead to consider the token already expired.</param>
public bool NeedsRefresh(TimeProvider clock, TimeSpan? margin = null)
{
ArgumentNullException.ThrowIfNull(clock);
return clock.GetUtcNow() + (margin ?? TimeSpan.FromSeconds(60)) >= ExpiresAtUtc;
}
}