OAuth 2.0

Write the OAuth settings once, as a named profile of the workspace, and let every request that needs them pick the profile. When you press Send, Postino gets or renews the token of that profile for the active environment, and signs you in in your browser when it has to. The token is never written in the workspace.

How it works

  1. A profile holds the authorization server and the client: the grant type, the token URL, the client ID and secret, the scope and a few options. It lives in .postino/project.toml, so it is shared through git with the workspace. See Profiles in the file.
  2. A request uses it with the OAuth 2.0 type of the Auth tab. In the request file that is a ::: auth oauth2 section with one line, profile = acme-sso.
  3. On Send, before the pre-request script, Postino looks for a token of that profile for the active environment:
    • A token that does not expire in the next 30 seconds is sent as it is.
    • An expired token is renewed with its refresh token. When there is none, or the server rejects it (invalid_grant), the grant of the profile runs again.
    • With no token, the grant runs. For Client credentials and Password it is one request to the token URL. For Authorization code your browser opens to sign in, see Signing in in the browser.
  4. The token is added as Authorization: Bearer <token>, or where the profile says (see Advanced), and the request is sent.

Each profile has one token per environment: switching from dev to prod never sends the token of dev. All the requests that use a profile share its token. A request that writes its own Scope gets a token of its own.

Creating a profile

In the Auth tab, choose OAuth 2.0. With no profile in the workspace yet, the tab shows No OAuth profiles in this workspace and a Create profile button. Type a Name (letters, digits, - and _), pick the Grant type and press Create or Enter. The new profile is saved to .postino/project.toml at once, chosen for the request, and its settings open.

Once there are profiles, the Profile picker lists them with their grant type and how many requests use each. At its end, New profile… creates another one and New profile from acme-sso… copies the current one under a new name. Choosing another profile only changes this request.

The Postman, Bruno and OpenAPI imports create profiles on their own from the OAuth settings they find. A profile the workspace already has with the same settings is reused; a name taken by other settings gets a suffix, such as acme-sso-2.

Profile settings

Profile settings, next to the picker, unfolds the profile inside the Auth tab, between the picker and the token card. The panel stays open or closed for every request until you press it again.

  • The header shows the profile name, a pencil to rename it, and Used by N requests, which lists them; click one to open it.
  • The profile is shared: a change applies to every request that uses it. Changes mark the request's tab as unsaved, and Ctrl S (⌘ S on macOS) saves the request and the profile together.
  • Fields take {{ }} variables, as everywhere else. Use them for what changes per environment, such as {{authBaseUrl}}/oauth/token or {{clientId}}. When a field is just one variable, the column on its right names the file of the active environment it comes from, in green when it is the .local.env file.
  • A secret written in clear in Client secret or Password shows Move to .local.env in orange: the profile is shared through git. Clicking it saves the value as a variable in the active environment's .local.env file (the name is offered, such as acmeSsoPassword, and can be changed) and writes {{acmeSsoPassword}} in the field instead.
  • Duplicate copies the profile under a new name. Delete… asks first, listing the requests that use it; they keep naming the deleted profile, in red, until another one is chosen.
  • Sign in (dev) or Get token (dev) gets a new token right away with the current settings, for the active environment.
  • Renaming with the pencil happens at once: the profile and the profile = line of every request that uses it are rewritten. The tokens of the old name are not kept.

Grant types and fields

Grant typeFieldsWho signs in
Authorization codeAuthorization URL, Token URL, Client ID, Client secret (optional with PKCE), Scope, PKCE, RedirectYou, in the system browser
Client credentialsToken URL, Client ID, Client secret, ScopeNobody: the client itself
PasswordToken URL, Client ID, Client secret, Username, Password, ScopeNobody: the user name and password go to the token URL

The implicit grant is not offered: OAuth 2.1 removes it. With Authorization code:

  • PKCE is on by default and uses SHA-256, with a new verifier on each sign-in. A public client, with no secret, needs it.
  • Redirect is where the provider sends the browser back after you sign in:
    • Loopback (default): http://127.0.0.1:{port}/callback, on a free port chosen on each sign-in, as RFC 8252 recommends. Providers such as Google or Microsoft accept it without registering the port.
    • Fixed URL: for providers that require the exact URL, port included, such as http://localhost:8765/callback. Postino listens on that port while it waits.
    • Paste manually: when the provider only allows a web URL. Postino opens the browser, and you paste the URL it ended at, with the code.

Advanced

  • Client auth: how the client authenticates to the token URL. Basic header (default) sends Authorization: Basic with the client ID and secret; In body sends client_id and client_secret in the form. A client with no secret always sends its ID in the body.
  • Send token as: Header (default), Authorization with a Prefix (Bearer by default), or Query param, with its Name (access_token by default).
  • Extra params: parameters the provider needs beyond the standard ones, such as audience. Each one goes in every Token request or in the Auth request, the URL opened in the browser. Type in the empty last row to add one; a row with no key is left out.

As with the other auth types, an enabled Authorization header written in the Headers tab is sent instead of the token.

The token card

Under the profile, a card shows the token of the active environment, with the environment's name and color on its right:

  • Token valid, with how long until it expires and how it will be renewed. Refresh now renews it.
  • Token expired 12 min ago, in amber: Send will renew it first. Refresh now does it right away.
  • No token for dev yet: Send gets one, signing you in if needed. Sign in now (or Get token now) does it right away.

With a token, the card shows the start of the access token with Copy, when it was obtained and whether there is a refresh token and an id token, and the sub, aud and exp claims when the token is a JWT. All claims shows every claim as highlighted JSON, read-only, with the local time of each time claim (exp, iat, nbf, auth_time) in a comment at the end of its line. Its Copy copies the plain JSON, without the comments. Sign in again (or Get new token) runs the grant from the start, and Clear forgets the token. The line under the card shows what Send adds, such as Authorization: Bearer eyJhbGciOi….

In the sidebar, a lock marks the requests that use a profile.

Signing in in the browser

When Send needs a sign-in for the authorization code grant, your system browser opens on the provider's page, and the response panel shows Sign in to acme-sso in your browser. Postino does not show the page inside the app: your browser already has your sessions and your password manager.

  1. Sign in as usual. The provider redirects the browser to Postino, which answers with a page that says Signed in to acme-sso. You can close that tab.
  2. Postino exchanges the code for a token, stores it, and sends the request.
  • Open browser again opens the page again, and Copy sign-in link copies it, to open it in another browser.
  • Paste redirect URL takes the URL the browser ended at, with the code, when the provider cannot redirect to 127.0.0.1.
  • Cancel or Esc stops waiting. Postino only listens on 127.0.0.1, only while it waits, and gives up after 5 minutes. Nothing is sent then.

Client credentials and password never open the browser: the token is requested silently.

In the response

  • When the token was obtained for the first time, a green line above the body says New token for acme-sso · dev · expires in 60 min.
  • When an expired token was renewed, a Token renewed chip shows next to the status. Hover it for the profile, the environment and the new expiry, click it to open the console on the token request.
  • Each token request appears in the Console as a POST entry of its own, before the request, with its grant_type. oauth rows tell what happened: token expired 12 min ago, refreshing it, new token, expires at 11:42 (60 min). In these token requests, the client secret, the code, the tokens and the passwords are masked. The network entry of the request itself is not: it shows the Authorization header with the token, exactly as it was sent.

When it fails

  • Couldn't get a token for acme-sso: the token URL answered with an error, such as invalid_client: the client ID or secret is wrong for dev., or a variable of the profile is missing in the environment. Nothing is sent. Edit profile opens Profile settings and Open in Console shows the token request.
  • The provider rejected the redirect URL: register http://127.0.0.1 (any port) in the provider, or switch the profile to Fixed URL. Copy redirect URL copies what to register.
  • The API rejected the token: the API answered 401, so the token may have been revoked, or lack a scope. The response is shown as it came, and Postino never retries by itself, since a 401 can also mean missing permissions. Get new token and resend renews it and sends again; Dismiss hides the card.

Tokens elsewhere in a request

For an API that wants the token somewhere else, such as another header or the body, write the template function {{ oauthToken("acme-sso") }}, or {{ oauthToken("acme-sso", "id_token") }} for the OpenID Connect id token. The token is obtained or renewed on Send exactly as with the OAuth 2.0 type, before the pre-request script, which has no network.

A load test gets the token of each profile once, before its virtual users start, and renews it when it expires. It never opens the browser, so sign in first for the authorization code grant.

Where tokens are kept

Tokens are kept in memory only, per workspace, profile and environment. Postino never writes them to the workspace, to disk or to the system credential store, so they are gone when it closes. The first Send after opening Postino gets a new token: silently for client credentials and password, and through your browser for authorization code, where a session still open in the browser needs no password.

Postino logs that a token expired or was obtained, with the profile, the environment and the grant, never a token, a secret or a code.

Profiles in the file

Each profile is an [oauth.<name>] table of .postino/project.toml:

[oauth.acme-sso]
auth_url = "{{authBaseUrl}}/authorize"
client_id = "{{clientId}}"
grant = "authorization_code"
pkce = true
redirect = "loopback"
scope = "openid profile payments:read"
token_url = "{{authBaseUrl}}/oauth/token"

[oauth.acme-sso.token_params]
audience = "https://api.acme.dev"

Every key is listed in Files and folders. A request that uses it:

charges/list-charges.postino
GET {{baseUrl}}/v2/charges
Accept: application/json

::: auth oauth2
profile = acme-sso

Only profile is required. scope = payments:write asks for another scope than the profile's, with a token of its own.

Found a mistake? Edit this page on GitHub or open an issue.

↑↓to move ↵to open /or Ctrl Kto search from any page