Skip to content

OAuth 2.0 token acquisition - #4

Merged
ShikeChen-MS merged 2 commits into
mainfrom
oauth2-tokens
Nov 18, 2026
Merged

ShikeChen-MS merged 2 commits into
mainfrom
oauth2-tokens

Conversation

@ShikeChen-MS

@ShikeChen-MS ShikeChen-MS commented Nov 18, 2026 •

Copy link
Copy Markdown
Owner

Implements the roadmap item OAuth 2.0 token acquisition in a generic way. It works with any authorization server that follows the standard (RFC 6749), such as Keycloak, Okta, Auth0, Entra ID or your own, with no provider-specific presets. Saving responses as Postman examples, the rest of that roadmap item, will come in its own PR.

Core (TermRest.Core.Auth, no new dependencies)

  • Grants: client credentials, password, and authorization code with or without PKCE (S256 or plain). The implicit grant is kept but not fetched, because RFC 9700 retires it.
  • Postman's settings: grant_type, accessTokenUrl, authUrl, redirect_uri, clientId, clientSecret, scope, state, code_verifier, challengeAlgorithm, client_authentication, refreshTokenUrl, headerPrefix, addTokenTo and accessToken.
    • The advanced tokenRequestParams, authRequestParams and refreshRequestParams are sent too, in the body, a header or the URL. They replace a built-in parameter of the same name.
    • Until the token URL is set, the stored accessToken is sent as before. Collections exported from Postman with a pasted token behave as they did.
  • Client authentication: the client ID and secret go in a Basic header (the default) or in the body, as in Postman.
    • Signed JWT (client_authentication: private_key_jwt, RFC 7523) is new. The JWT is signed with a certificate from the client-certificate registry.
    • Algorithms: RS, PS and ES with 256, 384 or 512. The default is RS256; for EC keys it is the ES algorithm that matches the curve.
    • Header: x5t#S256 and x5t, with an optional kid and x5c chain.
    • Claims: aud (the token URL unless set), iss/sub, jti, and iat/nbf/exp (valid for 5 minutes).
    • TermRest's own settings start with x-: x-clientAssertionCertificate, x-clientAssertionAlgorithm, x-clientAssertionKeyId, x-clientAssertionChain and x-clientAssertionAudience.
  • Signing in: the sign-in page opens in the system browser.
    • With a loopback callback (localhost, 127.0.0.1 or [::1]), TermRest receives the code itself through an in-process listener (RFC 8252). An empty callback uses a free port on 127.0.0.1.
    • With any other callback, the user pastes the address the browser ended on.
    • state is checked, server errors such as access_denied are reported, and signing in times out after 5 minutes.
  • Cache: tokens are kept in memory only, one per set of settings, with one fetch at a time.
    • A token is used until shortly before it expires, then refreshed with its refresh token. If the refresh is refused, a new token is fetched and a warning says so.
    • Tokens are never written to collections, the history or recordings.
  • Token requests: a form POST with Accept: application/json.
    • They use the workspace's SSL verification (--insecure), proxy, CA bundle and matching client certificate.
    • They follow no redirects (a 3xx is an error, because the request carries credentials), send no cookies and are not recorded.
    • The timeout is 60 s unless one is set, and responses are capped at 1 MiB.
    • A failure stops the request with the server's OAuth error, for example OAuth 2.0: the token request to https://…/token failed: 401 Unauthorized — invalid_client: ….
  • A request's Timeline (and send --verbose) says where its token came from and when it expires.

UI

  • Auth tab → OAuth 2.0: a scrolling form, laid out like Postman's. The mouse wheel works over its fields too.
    • At the top: the current token and its expiry, with Get new token and Forget token. The configuration follows.
    • The fields shown depend on the chosen grant and client authentication, and the signing certificate is picked from the registered ones.
  • Sign-in dialog: waits for the browser's callback. It has Open browser, Copy address, and a field for pasting the address the browser ended on. Cancel ends the request with "signing in was cancelled".
  • While a token is being fetched, the response pane shows progress, and Esc cancels.
  • Copy as cURL adds a token fetched earlier, or warns that there is none.
  • Certificates dialog: an entry may now have no URL patterns. Such an entry is sent to no server, but it can still sign client assertions.

CLI

  • termrest send fetches tokens as needed. To sign in, it opens the browser, prints the address on stderr, and accepts the loopback callback or a pasted address. Without a terminal, it fails at once if nobody could complete the sign-in.
  • send --curl adds a token fetched earlier, or warns that there is none.
  • certs list describes entries without URL patterns.

Verification

  • Windows: Core 809 passed (6 OS-specific skips), CLI 38 passed (2 skips), TUI 291 passed.
    • New tests: 20 Core tests against an in-process fake OAuth server, 1 CLI test and 5 TUI tests.
    • The Core tests cover each grant, PKCE, loopback and pasted sign-in, a forged state, refresh, RS256/PS256/ES256 assertions verified by the server, and errors.
  • CI: on each of Windows, Ubuntu 24.04 and macOS 15, 1146 tests ran: 1135 passed, 11 skipped, none failed. Native AOT builds pass for all six runtimes, and the smoke tests pass on the five that CI can run.
  • Native AOT win-x64: no warnings; 20.73 MiB (+0.26 MiB).
    • Smoke-tested send with a PS256 client assertion against a local token server, and snapshot.

ShikeChen-MS and others added 2 commits November 17, 2026 18:44
An OAuth 2.0 auth helper fetches its access token itself, from any authorization server that follows the standard (RFC 6749), when a request that uses it is sent: client credentials, password, and authorization code with or without PKCE (SHA-256 or plain). Signing in opens the sign-in page in the browser; a loopback callback URL (localhost, 127.0.0.1 or [::1], or none for a free port) is received by TermRest itself (RFC 8252), and otherwise the address the browser ended on is pasted, its state checked. The client authenticates with HTTP Basic or in the form, as in Postman, or with a JWT client assertion (RFC 7523, private_key_jwt: TermRest's x-clientAssertion settings) signed with a registered client certificate's key, in RS, PS or ES 256/384/512, naming the certificate by its thumbprints (x5t#S256, x5t), and optionally a key ID and the chain (x5c). Tokens are kept in memory for the session, one per set of settings, and refreshed with the refresh token when they expire (a new one is fetched if the refresh is refused). Token requests go with the workspace's SSL verification, proxy, CA bundle and client certificate, follow no redirects, send no cookies and are not recorded; one that fails stops the request with the server's error. Until the token URL is set, the access token stored in the collection is sent, as before. Postman's extra token, auth and refresh parameters are sent. In the UI, the Auth tab's OAuth 2.0 form scrolls, shows the current token and when it expires, and has Get new token and Forget token; signing in waits in a dialog that opens the browser again, copies the address, or takes the address pasted. termrest send fetches tokens as it goes (the sign-in address is printed on stderr). Copy as cURL and send --curl add a token fetched earlier, or warn that there is none. A client certificate may have no URL patterns: it goes to no server, and signs client assertions.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
The README's new OAuth 2.0 section describes the grants, when a token is fetched, signing in with a loopback or pasted callback, client authentication (Basic, in the body, or a JWT signed with a client certificate), Postman's extra parameters, how token requests are sent, and Copy as cURL; it lists Postman's settings and TermRest's own (private_key_jwt and the x-clientAssertion settings), with an example. The features, the Auth bullet and the compatibility table point to it; the client certificates section says an entry without patterns signs client assertions, send --curl says it fetches no token, the tests include an OAuth 2.0 server, and the roadmap moves on to saving responses as Postman examples.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@ShikeChen-MS
ShikeChen-MS merged commit cf4925a into main Nov 18, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant