Log in to an OAuth provider
The commands, prompts and messages below match the code, and the test suite runs these steps against a test provider. Neither shipped definition has been checked against the real provider yet, so check that a login and a refresh work before you rely on them.
Let an application call Gmail, Google Calendar or the GitHub API through Iron Vault without holding an OAuth token.
You sign the provider account in once with iron-vault login. Iron Vault keeps the refresh token in an encrypted
OAuth account record, refreshes the access token itself, and puts it in each request on a route that names the
record. The application holds a caller token and a placeholder, as for a static key.
Two kinds of record take part:
- An OAuth client record holds one provider app’s client ID and client secret, and the provider definition
that says how to talk to that provider.
add --kind oauth-clientmakes it. No route may name it. - An OAuth account record holds one signed-in account’s refresh token, granted scopes and verified account, and
names its client record.
loginmakes it, and routes name it like any credential.
Login is paste-back: the service shows a consent URL, you open it in any browser, and after you consent the browser
is sent to http://127.0.0.1:42213/?code=…&state=…. Nothing listens there, so the browser shows a connection error;
you copy the whole address from its address bar and paste it at the prompt. The service checks the state and redeems
the code with a PKCE verifier only it holds, so the address is no use to anyone else. This works the same from a
protected install, a Compose deployment or a remote machine, since the browser need not run where the service does.
Run every iron-vault command through sudo on a protected install (see sudo in a protected
installation), or without it in development mode.
Before you start
- The service running and Ready, with a listener and a caller, as in getting started or the Compose recipe.
- The approval passphrase, for
config approve, and for a login that changes the account or the client, or adds a scope or origin. - An account with the provider, and the rights to register an app there.
The shipped google definition asks for openid and email on every login, proves the account by the id_token
Google returns, and refreshes without rotation: a refresh writes nothing to disk. Its resource hosts are
gmail.googleapis.com and www.googleapis.com (Calendar). Another Google API host needs a definition of its
own.
1. Register a Desktop client
In the Google Cloud console, in a project of your Workspace:
- Enable the APIs you will call, such as the Gmail API and the Google Calendar API.
- Set up the OAuth consent screen with the Internal user type. Google gives refresh tokens that expire after seven days to an External app in Testing (Google: refresh token expiration), which would end unattended running within a week. The Internal user type needs a Google Workspace.
- Create an OAuth client of type Desktop app. Google accepts any loopback port for a Desktop client, so there is no redirect to register. Keep the client ID and client secret to hand.
2. Enroll the client
sudo iron-vault add google_client --kind oauth-client --provider google
The service first shows the definition the record will seal, with every default filled in, and the origins the client secret may be sent to, then prompts for the client ID (echo on) and the client secret (echo off):
OAuth client google_client will seal this provider definition:
authorize = "https://accounts.google.com/o/oauth2/v2/auth"
token = "https://oauth2.googleapis.com/token"
redirect = "http://127.0.0.1:42213/"
scopes = "required"
add_scopes = ["email", "openid"]
rotation = "never"
client_auth = "client_secret_post"
resource_hosts = ["gmail.googleapis.com", "www.googleapis.com"]
min_retry_seconds = 60
refresh_margin_seconds = 300
[authorize_params]
access_type = "offline"
prompt = "consent"
[account]
style = "id_token"
issuers = ["accounts.google.com", "https://accounts.google.com"]
[revoke]
style = "rfc7009"
url = "https://oauth2.googleapis.com/revoke"
Its permitted origins, the only ones the record can reach: accounts.google.com:443, oauth2.googleapis.com:443.
Client ID for google_client:
Client secret for google_client:
added google_client (oauth_client) as record 5f0c…
The definition is sealed: editing the file or a config later cannot move the client secret to another endpoint. To change the client secret or the definition, enroll a new client record and sign each account in again with it.
3. Sign the account in
Name every scope the application needs, and every resource host its routes reach:
sudo iron-vault login google_naomi --client google_client \
--scope https://www.googleapis.com/auth/gmail.send \
--scope https://www.googleapis.com/auth/calendar.events \
--scope https://www.googleapis.com/auth/calendar.freebusy \
--origin gmail.googleapis.com --origin www.googleapis.com \
--account naomi@example.com
Open this address in a browser and consent:
https://accounts.google.com/o/oauth2/v2/auth?…
Address your browser was sent to:
Open the URL, sign in as naomi@example.com, and leave every scope ticked. Paste the address the browser ends on:
exchanging the code
signed in google_naomi as naomi@example.com (https://accounts.google.com): added
granted scopes: email openid https://www.googleapis.com/auth/calendar.events …
The login is refused, and nothing is written, if the pasted address carries another login’s state
(state-mismatch) or an error (consent-refused), if Google verified another account than --account
(account-mismatch), or if you unticked a scope (scopes-missing). Each needs a fresh login; a code is never
redeemed twice. See login errors.
list shows the account record with its client, verified account and scopes, never a token:
google_client oauth_client (token oauth2.googleapis.com, rotation never) accounts.google.com:443,oauth2.googleapis.com:443 healthy
google_naomi oauth_refresh (client google_client, account "naomi@example.com", issuer "https://accounts.google.com", scopes …) gmail.googleapis.com:443,www.googleapis.com:443 healthy
4. Route to it
An account record fills a header slot. Add a route to your config that names it, and grant the route to the caller:
[routes.gmail]
prefix = "/gmail"
upstream = "https://gmail.googleapis.com"
credential = "google_naomi"
slot = { header = "Authorization", format = "Bearer {}" }
methods = ["GET", "POST"]
paths = ["/gmail/v1/users/me/**"]
rate_limit = { requests = 120, per_seconds = 60, burst = 20 }
sudo iron-vault config approve config.toml
The approval summary ends with the account each route reaches, so you see whose mailbox it is before you sign:
accounts:
route gmail: credential google_naomi reaches "naomi@example.com" (issuer "https://accounts.google.com", subject "1084…") through client google_client
The broad path grant is a choice: it lets the caller do anything those scopes allow under /gmail/v1/users/me/.
Narrow paths and methods to what the application needs.
5. Check that it worked
curl -s -H "Proxy-Authorization: Bearer $(cat ~/.iron-vault-tokens/me)" \
http://127.0.0.1:7878/gmail/gmail/v1/users/me/profile
Gmail answers with the account’s profile. The first request refreshes the access token, which the service then reuses
until five minutes before it expires. A credential.refreshed event in the audit log
records each refresh.
When the caller gets 503
A route that names an account record answers 503 with the body service unavailable when no access token can be had.
The audit log’s request.completed event gives the reason:
-
credential_needs_login: the provider refused the refresh token. Google does this when the user changes their password (Gmail scopes), when the grant is revoked, or when the token went unused for six months. Acredential.needs-loginevent records it once, andstatusshows it:credential google_naomi: needs login (refused); unavailable routes: gmailRun the same
loginagain. Signing the same account in again with the same client and no new scope or origin needs no approval passphrase; other routes keep working meanwhile. -
token_unavailable: the last refresh failed, or the upstream answered401and the service dropped the token. The service waits 60 seconds after a refresh attempt before the next one, so a caller that keeps getting401can’t make it refresh on every request. Retry after a minute. If it persists,credential.refresh-failedsays why.
A 401 from Gmail itself goes to the caller as it came, and the request is never sent again.
GitHub App
The shipped github-app definition takes no scopes (a GitHub App’s permissions are set on the app), proves the account
by one request to https://api.github.com/user, and rotates: GitHub sends a new refresh token on every refresh, and the
service stores it before it uses the new access token, so a crash never leaves it holding a spent token. Its resource
host is api.github.com.
1. Register the app
Under Settings → Developer settings → GitHub Apps → New GitHub App:
- Set the Callback URL to exactly
http://127.0.0.1:42213/. GitHub documents a flexible loopback port for OAuth Apps but not for GitHub Apps, so register this address as it is. - Leave Expire user authorization tokens ticked. The service needs a refresh token, which GitHub sends only for expiring user tokens.
- Clear Webhook → Active; the service needs no webhook.
- Give the app the repository or organization permissions the application needs, then create it.
- Generate a client secret, and keep the client ID and secret to hand.
- Install the app on the account or organization whose repositories it will reach.
2. Enroll and sign in
sudo iron-vault add github_client --kind oauth-client --provider github-app
sudo iron-vault login github_octo --client github_client --origin api.github.com --account octocat
--account is the GitHub login. Give no --scope: the definition refuses it.
signed in github_octo as octocat (https://api.github.com): added
granted scopes: none reported
Route to it as for Google, with upstream = "https://api.github.com". GitHub’s access token lives eight hours; the
service refreshes it from half its lifetime on, and waits five minutes between refresh attempts.
Revoking a GitHub App grant
GitHub’s revocation call isn’t the standard one, so the definition says revoke = { style = "none" }. When rm removes
the account record, or a login moves it to another account or client, the service tells you to revoke at the provider
instead of doing it. Revoke under Settings → Applications → Authorized GitHub Apps → Revoke on the account that
signed in.
If a rotation’s outcome is unknown, say the service crashed after sending the refresh, the record needs login with cause
uncertain, rather than risk sending a spent refresh token. Run login again.
Another provider
Adding a provider needs a definition file, not a code change. The provider definition reference lists every field and rule; this section writes one for an OpenID Connect provider, Acme, field by field. Read the provider’s OAuth documentation for each value.
authorize = "https://id.acme.example/authorize"
token = "https://id.acme.example/token"
scopes = "optional"
add_scopes = ["openid", "email"]
account = { style = "id_token", issuers = ["https://id.acme.example"] }
rotation = "never"
client_auth = "client_secret_basic"
revoke = { style = "rfc7009", url = "https://id.acme.example/revoke" }
resource_hosts = ["api.acme.example"]
authorizeandtoken: the authorization and token endpoints, plainhttpsURLs with no query or fragment. Fixed consent-URL parameters go inauthorize_params, never in the URL.redirect(left out here): the loopback address the browser is sent to,http://127.0.0.1:42213/unless you set another port. Register it with the provider if it asks.scopes:requiredif every login must name a--scope,optional, ornoneif the provider sets permissions on the app.add_scopes: scopes every login asks for. Anid_tokenprovider needsopenidhere, and usuallyemail, so the token names the account.account: how the service learns who signed in.id_tokenreads the token response’s OpenID Connectid_token, whoseissmust be one ofissuersand whoseemailmust be verified. A provider without one takes{ style = "userinfo", url = "…", subject = "id", display = "login" }: one GET with the new access token, naming the JSON fields that hold a stable ID and the name--accountmust match.rotation:alwaysif the provider ever sends a new refresh token on a refresh, elsenever. If unsure, choosealways: a wrongneverfails closed, with the record needing login, but costs a login.client_auth:client_secret_basicif the token endpoint wants the client ID and secret as HTTP Basic,client_secret_postfor the form body.revoke: the provider’s RFC 7009 revocation endpoint, or{ style = "none" }if it has none, and the service will tell you to revoke at the provider.resource_hosts: the API hosts an account’s tokens may be sent to: a host name, or*.and a host name, which matches exactly one more label. A public suffix such as*.comor*.github.iois refused.
Enroll it from the file; a value with a / or ending in .toml is a path:
sudo iron-vault add acme_client --kind oauth-client --provider ./acme.toml
sudo iron-vault login acme_naomi --client acme_client --scope read --origin api.acme.example --account naomi@acme.example
A definition that breaks a rule is refused before any prompt, with credential-invalid and a message naming the field:
invalid provider definition: unknown field `token_lifetime`, expected one of `authorize`, `token`, … at line 10, column 1
invalid provider definition: an endpoint follows the rules for a route's upstream URL: upstream URL must start with "https://" at line 2, column 9
Reference
- Provider definitions: every field, default and rule.
addandloginin the CLI reference, and theloginerrors.- Request refusals:
credential_needs_loginandtoken_unavailable. - OAuth design: login, refresh, the rotation transaction and the threats they meet.