Connecting via SDK
An app connects to masks with one of six SDKs. An SDK (software development kit) is a library that masks publishes for one language. With an SDK, an app:
- registers itself through a handshake
- signs people in
- acts for a person at another provider
- trades a token for a narrower one
Choosing an SDK
Section titled “Choosing an SDK”Pick the SDK for the place your app runs. Each language’s guide walks through the handshake, sign-in, and checking tokens.
| SDK | For |
|---|---|
Masks::Rails |
A Rails app. An engine that mounts sign-in, the handshake, and token checks. See Rails apps. |
Masks::Client |
Any Ruby process. Masks::Rails is built on it. |
@masks/client |
A single-page app, in the browser. See static sites. |
@masks/server |
A JavaScript server: Next.js, Auth.js, Express, Hono, or any app that takes a Request. See JavaScript apps. |
masks-client |
A FastAPI, Django, or Flask app, a Python service or machine, and a Python API. |
github.com/masksrb/masks/go |
A Go web app, service, or machine, and a Go API. |
The masks gem holds both Ruby SDKs. Masks::Rails loads only when Rails does.
What each SDK covers
Section titled “What each SDK covers”Each check mark is a link to the test that proves it. A number points to a note under the table. ./dev reference builds the grid from docs/parity.json, and CI fails when a test it names is gone.
CI also runs the Ruby, JavaScript, Python, and Go SDKs against a real masks, booted from the image
that run just built: the handshake, a sign-in, a refresh, a step-up, a back-channel logout, and a
DPoP-bound machine sign-in. Each CI run’s summary shows that grid, one row per SDK and one column
per scenario, and
test/live/README.md says how
the run works.
| What the SDK does | Rails | Ruby | Browser | JavaScript | Python | Go |
|---|---|---|---|---|---|---|
| Signing in with the code flow and PKCE | ✓ | ✓ | ✓1 | ✓ | ✓ | ✓ |
| The handshake | ✓ | ✓ | ✓2 | ✓ | ✓ | ✓ |
| Redeeming an initial access token | ✓ | ✓ | – | ✓ | ✓ | ✓ |
| Enrolling and unenrolling machines | – | ✓ | – | ✓ | ✓ | ✓ |
| Refreshing tokens | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
| Token exchange | – | ✓ | – | ✓ | ✓ | ✓ |
| Delegation | ✓3 | ✓ | – | ✓ | ✓ | ✓ |
| Client credentials | – | ✓ | – | ✓ | ✓ | ✓ |
| Revocation | ✓4 | ✓ | ✓4 | ✓ | ✓ | ✓ |
| Introspection | – | ✓ | – | ✓ | ✓ | ✓ |
| The device flow | – | ✓ | – | ✓ | ✓ | ✓ |
private_key_jwt client authentication | – | ✓ | – | ✓ | ✓ | ✓ |
| Sending DPoP proofs as a client | – | ✓ | ✓5 | ✓ | ✓ | ✓ |
| Checking access tokens, including DPoP-bound ones | ✓ | ✓ | – | ✓ | ✓ | ✓ |
| One store of seen DPoP proofs for every process | ✓6 | ✓7 | – | ✓7 | ✓7 | ✓7 |
Refusing a sign-in older than the max_age the app asked for | ✓ | ✓ | – | ✓ | ✓ | ✓ |
| Serving the last good issuer and keys while masks is down, with backoff | ✓8 | ✓ | – | ✓ | ✓ | ✓ |
| Raising the same error for the same failure, worded from the shared table | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
| Verifying an access, ID, or logout token the same way, from the shared token cases | ✓ | ✓ | ✓9 | ✓ | ✓ | ✓ |
| Testing an app against the fake masks the SDK ships | ✓10 | ✓ | – | ✓ | ✓ | ✓ |
- Yes, through a backend or in the page
- Starts it through a backend
- Asks for it in its handshake
- Revokes its refresh token at sign-out
- Yes, with
dpop: true - Yes, in
Rails.cache - With a store you supply
- Through Masks::Client
- ID tokens only, as the browser verifies nothing else
- Through
Masks::Client::Fake, which the gem ships for both Ruby SDKs
100 of 100 cells are proven by a test. Each check mark opens the test.
The terms in this table mean:
| Code flow and PKCE | The browser signs in at masks and comes back to the app with a one-time code. The app trades the code for tokens. PKCE (Proof Key for Code Exchange) lets only the app that asked trade the code. |
| Handshake | The way an app registers itself with masks. See connecting an app. |
| Initial access token | A one-time token from masks that lets an app or a machine register itself as a client. |
| Token exchange | Trading one token for another. See exchanging a token. |
| Client credentials | A client asking for a token as itself, with nobody signed in. |
| Revocation | Telling masks to end a token. |
| Introspection | Asking masks whether a token is active. |
| Device flow | A device with no browser shows a code. A person approves it on another device. |
private_key_jwt |
A client proves who it is with a message signed by its private key. It sends no secret. |
| DPoP | Ties a token to a key. Each request carries a proof signed by that key, so a copied token does not work without the key. |
| Store of seen DPoP proofs | An API refuses a proof it has seen before. Without a shared store, each process of the API remembers only the proofs it has seen itself. |
| Shared token cases | One list of access, ID, and logout tokens, each with one defect, and the refusal each one ends in. Every SDK signs the list at test time and must refuse each token the same way. |
Connecting an app
Section titled “Connecting an app”- Install the SDK. For Rails, run
bin/rails generate masks:install. - Set the issuer. This is the address masks answers at, such as
https://acme.auth.example.com. - Set the app’s resource identifier. This is the app’s own address, such as
https://app.example.com. The app’s redirect URIs (the addresses masks sends the browser back to) must share its origin: the same scheme, host, and port. - Open the handshake. The browser goes to masks’ approval screen.
- Approve it as a person holding
masks:manageormasks:handshake. masks sends the browser back to the app with a one-time initial access token. - The app redeems the token with masks, server to server, and receives its client ID and secret. The secret never passes through the browser.
The app is then an approved client. It signs people in with the SDK’s login URL, and its API checks access tokens with the SDK’s verifier.
Handshake parameters
Section titled “Handshake parameters”The SDK sends these in the query of the handshake URL. Every address must share the resource’s origin.
client_name |
The name the approval screen shows. |
resource |
The app’s resource identifier. Required. |
scope |
The scopes the app asks for, separated by spaces. Required. |
redirect_uris |
Where masks sends the browser after a sign-in. Repeat it for each address. Required. |
return_to |
Where masks sends the browser with the initial access token. Required. |
state |
A value masks hands back unchanged to return_to. |
token_endpoint_auth_method |
How the app authenticates. The default is client_secret_basic. |
backchannel_logout_uri |
Optional. Where masks posts a logout token when it ends a session. |
post_logout_redirect_uri |
Optional. Where masks sends the browser after the person signs out of masks. Without it, a sign-out at masks cannot send the browser back to the app. |
A single-page app
Section titled “A single-page app”A single-page app with no backend runs the code flow in the page with @masks/client, as a public
client that keeps its tokens in sessionStorage. See static sites.
An app with a backend keeps the tokens on the backend, and the page holds only a cookie.
@masks/client’s session mode talks to that backend, which runs Masks::Rails, @masks/server,
the Python SDK, or the Go SDK.
Delegation
Section titled “Delegation”A delegation lets an app act as one person at a provider while nobody is signed in. For example, an app can sync a person’s Google Drive at night, or call their Notion MCP server.
- masks stores and refreshes the provider’s tokens.
- The app gets a token for one person’s account, after that person consented to that app.
- The app calls the provider directly. It comes back to masks only when its token expires.
Each person sees every app using each of their accounts on their account page. They can stop any of them there.
The JavaScript, Python, and Go guides show their SDK’s delegation helpers. This section shows the requests underneath, and the Ruby calls.
Set up the provider
Section titled “Set up the provider”In /manage, on the provider’s Clients tab:
- Switch on Let clients use an actor’s Google account. The label names the provider.
- Fill in What clients may do with the provider’s scopes beyond sign-in.
- Fill in Extra parameters when connecting if the provider needs them for a refresh token.
| Provider | What clients may do | Extra parameters |
|---|---|---|
https://www.googleapis.com/auth/drive.readonly |
access_type=offline prompt=consent |
|
| Microsoft | offline_access Files.Read.All |
|
| An MCP server | Leave it empty for whatever the server grants. |
An MCP server always allows delegation, so it has no switch. Delegation uses the provider’s
sign-in redirect URI, /login/provider/<key>/callback.
Changing What clients may do asks everybody to connect again the next time an app sends them
to /authorize.
Let the app ask
Section titled “Let the app ask”Only an approved client can receive a delegation. Besides openid and offline_access, it needs:
allowed_scopes |
masks:delegate: (every provider) or masks:delegate:google (one) |
grant_types |
authorization_code, refresh_token, and urn:ietf:params:oauth:grant-type:token-exchange |
A handshake that asks for a masks:delegate: scope sets the grant types. With Masks::Rails, set:
config.delegates = trueconfig.delegation_redirect_uri = ->(request) { "#{request.base_url}/connect/callback" }Masks::Rails adds the scope and the redirect URI to its handshake. Your app answers the redirect
URI itself, as in from Ruby.
Connect an account
Section titled “Connect an account”-
Send the browser to
/authorize. Keepstateand the PKCE verifier in the session.GET https://auth.example.com/authorize?response_type=code&client_id=CLIENT_ID&redirect_uri=https://app.example.com/connect/callback&scope=openid offline_access masks:delegate:google&state=STATE&code_challenge=CHALLENGE&code_challenge_method=S256 -
masks signs the person in and asks for consent. If it holds no usable token, it sends them to Google. Then it redirects back:
GET https://app.example.com/connect/callback?code=CODE&state=STATE -
Check
state, then redeem the code as the client:POST https://auth.example.com/tokenAuthorization: Basic base64(CLIENT_ID:CLIENT_SECRET)Content-Type: application/x-www-form-urlencodedgrant_type=authorization_code&code=CODE&redirect_uri=https://app.example.com/connect/callback&code_verifier=VERIFIER{"access_token": "eyJ…","refresh_token": "Xk2…","delegations": [{"connection": "3f1c9a0e-…","provider": "google","provider_name": "Google","label": "ada@example.com","subject": "8f4c…"}]} -
Store
delegations[].connection(the account) andrefresh_token(the secret), encrypted.
subject is the same sub your sign-in receives. Use it to refuse a connection that someone other
than the person who started it finished.
error |
Means |
|---|---|
access_denied |
The person declined, took more than 15 minutes, or chose a provider account that is not theirs in masks. The provider refused, or is no longer available. |
login_required |
A first connection to a provider that also signs people in needs a sign-in within the last 15 minutes. Send them again with prompt=login. |
interaction_required |
You sent prompt=none, and masks has to ask something. |
unauthorized_client |
The client is not approved. |
invalid_scope |
No provider with that key allows delegation. |
Get a provider token
Section titled “Get a provider token”Follow these steps when you have no provider token, or when it is about to expire.
-
Refresh the secret. Send no
scope.POST https://auth.example.com/tokenAuthorization: Basic base64(CLIENT_ID:CLIENT_SECRET)grant_type=refresh_token&refresh_token=SECRET{ "access_token": "eyJ…", "refresh_token": "NEW_SECRET", "expires_in": 3600 }Save
NEW_SECRETfirst. The old one is spent. -
Exchange the masks access token for the provider’s:
POST https://auth.example.com/tokenAuthorization: Basic base64(CLIENT_ID:CLIENT_SECRET)grant_type=urn:ietf:params:oauth:grant-type:token-exchange&subject_token=eyJ…&subject_token_type=urn:ietf:params:oauth:token-type:access_token&requested_token_type=urn:masks:params:oauth:token-type:upstream_access_token&audience=3f1c9a0e-…{"access_token": "ya29.…","issued_token_type": "urn:masks:params:oauth:token-type:upstream_access_token","token_type": "Bearer","expires_in": 3542,"scope": "https://www.googleapis.com/auth/drive.readonly"}
Call Google with ya29.… until the time in expires_in has passed. On a 401, discard the token and
repeat these steps.
| Response | Means | Do |
|---|---|---|
400 invalid_grant |
The person stopped the app or disconnected, or the provider refused. | Ask them to connect again. |
400 insufficient_scope |
The token lacks masks:delegate:<provider>. A refresh that sent a narrower scope drops it from every later secret. |
Ask them to connect again. |
503 temporarily_unavailable |
The provider did not respond. | Retry later. Nothing was lost. |
Rules for the secret
Section titled “Rules for the secret”- Keep every new secret. Each refresh replaces it, even when the exchange after it fails.
- Refresh each secret from one place at a time. Using a secret twice revokes every secret in its family, the newest one included, and the person has to connect again. Lock around the refresh if more than one worker can reach it.
- Use it within the client’s refresh token lifetime, 30 days by default. Each refresh starts that time again.
MCP servers
Section titled “MCP servers”An MCP server that is its own OAuth server, such as https://mcp.notion.com/mcp, is a provider with
the protocol mcp. Given its URL, masks finds its authorization server and registers itself there.
The server must offer PKCE with S256 and a registration endpoint.
To call the MCP server:
- Connect an account, as in connect an account.
- Get a token, as in get a provider token.
- Send the token to the MCP server as
Authorization: Bearer.
From Ruby
Section titled “From Ruby”delegations = Masks::Client.delegations(issuer, client_id:, client_secret:, redirect_uri:)
started = delegations.start(provider: "google")session[:connecting] = startedredirect_to started["url"]
held = delegations.finish(params: params, started: session.delete(:connecting))save(held.connection, held.secret)
upstream = delegations.token(secret, connection: connection)save(connection, upstream.secret)upstream.access_tokentokenrefreshes and exchanges in one call.upstream.secretreplaces the secret passed in.Delegations::Refusedmeans the person has to connect again.- Retry after
Delegations::Unavailable. - Both errors carry
secretwhen masks had already rotated it.
Delegations::Fake stands in for masks in tests. See Masks::Client.
Calling a provider while the person is present
Section titled “Calling a provider while the person is present”An app that calls a provider only while the person uses it can run its own OAuth flow there. The
identities scope adds the person’s connected accounts to the claims, with an email address only
when it is confirmed. The flow is then a redirect with a hint:
{ "sub": "8f4c…", "identities": [ { "provider": "google", "protocol": "oidc", "sub": "10769150350006150715", "email": "ada@gmail.com" } ]}https://accounts.google.com/o/oauth2/v2/auth?…&login_hint=ada@gmail.comA pairwise client never receives identities. A pairwise
client sees a different subject for each person than every other client does. An upstream subject is
the same for every client, so it would undo that separation.
Exchanging a token
Section titled “Exchanging a token”A token exchange (RFC 8693) trades one token for another
at /token. For example:
- a gateway gives a downstream service a token for that service only
- an agent holds a token recording that it acts for a person
- a backend trades its frontend’s ID token for an access token of its own
The client must be registered for urn:ietf:params:oauth:grant-type:token-exchange.
curl -u "$CLIENT_ID:$CLIENT_SECRET" https://auth.example.com/token \ -d grant_type=urn:ietf:params:oauth:grant-type:token-exchange \ -d subject_token="$ACCESS_TOKEN" \ -d subject_token_type=urn:ietf:params:oauth:token-type:access_token \ -d scope="catalog:read" \ -d resource=https://search.example.comEach SDK sends the same request:
| SDK | Call |
|---|---|
| Ruby | Masks::Client::Session#exchange |
@masks/server |
client.exchange |
| Python | client.exchange |
| Go | Client.Exchange |
A client that holds its tokens to a DPoP key proves it on the exchange too.
What can be exchanged
Section titled “What can be exchanged”subject_token_type |
At most | Expires no later than | Refused when |
|---|---|---|---|
…:access_token |
The subject token’s scopes and resources, and the client’s own scopes. | The subject token. | It is not live, or another issuer signed it. |
…:id_token |
What the person consented to for this client. | The ID token. | Its session ended, it was issued to another client, or the client is public. |
urn:masks:params:oauth:token-type:upstream_access_token (requested) |
See delegation. |
An exchange only narrows a token. scope, resource, and requested_lifetime can only ask for
less. Revoking the subject token revokes everything exchanged from it.
Who is acting
Section titled “Who is acting”A token exchanged from an access token has an act claim naming its holder. When a token is
exchanged again, the earlier act is nested inside the new one, so every holder stays listed:
{ "sub": "8b1f…", "client_id": "search-service", "act": { "sub": "search-service", "act": { "sub": "gateway" } }}A client acting as a more specific party sends that party’s token as actor_token, with
actor_token_type. The actor token must be live and issued to this client.
An agent that signs in as itself and acts for a person sends:
- its own token as the actor
- the person’s token as the subject
A client exchanging its own ID token acts for itself. The new token has no act unless an actor
token was sent.
masks does not exchange refresh tokens, SAML assertions, or other issuers’ tokens. It does not read
may_act.
Testing your app
Section titled “Testing your app”Each SDK ships a fake masks for an app’s own test suite. The fake serves discovery and keys, registers clients, signs a person in through the code flow, rotates refresh tokens, revokes and introspects tokens, answers userinfo, and mints access and logout tokens with its own signing key. The SDK’s own tests run against the same fake, so the fake keeps up with the SDK.
| SDK | Fake |
|---|---|
Masks::Client and Masks::Rails |
require "masks/client/fake", then Masks::Client::Fake.start |
@masks/server |
import { Fake } from "@masks/server/testing", then await Fake.start() |
masks-client |
from masks.testing import Fake, FakeClient, then Fake() |
| Go | import "github.com/masksrb/masks/go/masktest", then masktest.New(t) |
A test registers the app with the fake, points the SDK at the fake, and signs in. In Ruby:
require "masks/client/fake"
fake = Masks::Client::Fake.startfake.add("app", method: "client_secret_basic", secret: "s3cret")
session = Masks::Client::Session.new( issuer: fake.url, client_id: "app", client_secret: "s3cret", redirect_uri: "https://app.test/callback")
started = session.startreturned = URI.decode_www_form(URI.parse(fake.authorize(started[:url])).query).to_htokens = session.complete(code: returned["code"], verifier: started[:verifier])identity = session.identity(tokens, nonce: started[:nonce])
fake.stopauthorize takes the URL the SDK built for the browser and returns the URL masks sends the browser
back to, with a code for ada. It signs in as somebody else when the test names a subject, and it
comes back with an error when the test asks for one. The Ruby, JavaScript, and Go fakes listen on a
local port. The Python fake answers through an httpx transport, so a test passes fake.http() to
masks.discover and masks.Client.
The fake records what the SDK sent. A test reads the last form sent to an endpoint with last in
Ruby, tokenRequests and handshakes in JavaScript, seen in Python, and Seen in Go. Each
fake also mints a back-channel logout token for a client, and each SDK’s README shows the fake in
that language.
Without an SDK
Section titled “Without an SDK”masks is a standard OpenID Connect provider. An app in a language with no SDK signs people in with its usual library, using the code flow with PKCE.
Registering the app
Section titled “Registering the app”- In
/manage, open Clients and choose Add client. - Choose Signs actors in.
- Name the app.
- Enter its redirect URI.
- Copy the client secret. masks shows it only once. The
client_idstays on the client’s page.
The app also needs:
- the issuer, which is the address masks answers at, such as
https://auth.example.com - discovery, which is at
/.well-known/openid-configurationunder the issuer
Ask for groups as well to receive the groups claim.
A service with nobody signed in is a client that Signs in as itself. It holds no redirect URI. It asks for its own token with client credentials.
Checking access tokens
Section titled “Checking access tokens”An access token from masks is a JWT (JSON Web Token) of type at+jwt. masks signs it with a key
published at /.well-known/jwks.json. An API checks:
- its signature
- its issuer
- that
audnames the API
An app asks for a token for the API by sending resource=https://api.example.com when it signs in.
The token’s scope holds the granted scopes, separated by spaces.
Each SDK’s verifier makes all of those checks. It also checks the proof that comes with a DPoP-bound token. The proof must be:
- signed by the token’s key
- made for this method and URL
- made within the last 90 seconds, which allows for clocks that differ
- not seen before
A proof names the URL the client used. Behind a proxy, give the verifier the API’s public URL:
publicURL in @masks/server, public_url in Python, PublicURL in Go, and url: on
Masks::Client::Resource#authenticate in Ruby. Masks::Rails uses the request’s own URL, so it
depends on Rails trusting the proxy’s headers.
In a language with no SDK, a JWT library checks a plain bearer token, a token that works for anyone who holds it. With jose:
import { createRemoteJWKSet, jwtVerify } from "jose";
const keys = createRemoteJWKSet( new URL("https://auth.example.com/.well-known/jwks.json"),);
const { payload } = await jwtVerify(token, keys, { issuer: "https://auth.example.com", audience: "https://api.example.com", typ: "at+jwt",});