Authentication
Everything starts at the token endpoint of the auth service.
/oauth/tokenauthThe body is JSON. Client credentials go in the body (client_secret_post). Rate limited.
Client credentials (machine tokens)
For backends acting as the app. This is what sdk.asAdmin() does.
- curl
- JavaScript
- Python
- R
curl -s https://auth.scenid.cloud/oauth/token \
-H 'Content-Type: application/json' \
-d '{
"grant_type": "client_credentials",
"client_id": "5b8c1f0e-…",
"client_secret": "…"
}'
const response = await fetch(tokenUri, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ grant_type: 'client_credentials', client_id, client_secret })
})
const { access_token, expires_in } = await response.json()
import os
import requests
response = requests.post(token_uri, json={
"grant_type": "client_credentials",
"client_id": os.environ["CLIENT_ID"],
"client_secret": os.environ["CLIENT_SECRET"],
})
token = response.json()["access_token"]
library(httr2)
response <- request(token_uri) |>
req_body_json(list(
grant_type = "client_credentials",
client_id = Sys.getenv("CLIENT_ID"),
client_secret = Sys.getenv("CLIENT_SECRET")
)) |>
req_perform()
token <- resp_body_json(response)$access_token
{
"access_token": "eyJhbGciOiJSUzI1NiIs…",
"token_type": "Bearer",
"expires_in": 3600,
"scope": ""
}
Claims of the access token: sub (client id), tenantId, token_type: "service", client_type: "tenant_app", client_name, scope, iss. No aud.
The app must have a service key (created in the admin app). Without one, the app needs client_credentials in its grant types and must be a confidential client.
| Status | Body | Cause |
|---|---|---|
| 400 | oauth/invalid-client | Unknown client |
| 401 | oauth/invalid-client-secret | Wrong secret |
| 403 | oauth/public-client-not-allowed | Public client without service key |
| 403 | oauth/grant-not-allowed | Grant not enabled for the app |
| 400 | oauth/unsupported-grant-type | Unknown grant_type |
Authorization code (users)
- Send the user to
GET /oauth/authorize?response_type=code&client_id=…&redirect_uri=…&scope=openid profile email offline_access&state=…. - The user signs in and consents. Scenid redirects to your
redirect_uriwith?code=…&state=…. - Exchange the code:
curl -s https://auth.scenid.cloud/oauth/token \
-H 'Content-Type: application/json' \
-d '{
"grant_type": "authorization_code",
"code": "…",
"redirect_uri": "http://localhost:3000/callback",
"client_id": "…",
"client_secret": "…"
}'
Response: access_token, id_token (with the openid scope), refresh_token (with offline_access, valid 12 hours). PKCE (code_challenge_method=S256) is supported for public clients.
Errors: oauth/missing-code, oauth/invalid-code, oauth/client-mismatch, oauth/redirect-uri-mismatch, oauth/code-expired, oauth/invalid-code-verifier.
Refresh token
{ "grant_type": "refresh_token", "refresh_token": "…", "client_id": "…", "client_secret": "…" }
Token exchange (act as a user)
Trade the ID token of a signed-in user for a user access token that Customers, Insights and Files accept.
{
"grant_type": "urn:ietf:params:oauth:grant-type:token-exchange",
"subject_token": "<id_token>",
"subject_token_type": "urn:ietf:params:oauth:token-type:id_token",
"client_id": "…",
"client_secret": "…"
}
Requirements: the app has a service key, the ID token was issued to this app (aud = client_id) and belongs to the same tenant. The result is a one-hour user token with the user's claims, groups and ACLs.
Errors: oauth/service-key-not-provisioned, oauth/invalid-subject-token, oauth/token-audience-mismatch, oauth/tenant-mismatch, oauth/user-not-found, oauth/account-pending, oauth/account-denied, oauth/account-deactivated.
Discovery, keys and introspection
| Endpoint | Description |
|---|---|
GET /.well-known/openid-configuration | Discovery document. Auth method client_secret_post, PKCE S256 |
GET /oauth/.well-known/jwks.json | Public keys { keys: [{ kty, kid, use: 'sig', alg, … }] } for verifying tokens |
POST /oauth/introspect | { token } → { active, sub, scope, token_type, tenantId, iss, exp, iat } |
GET /oauth/userinfo | With a user Bearer token. 403 oauth/forbidden for other tokens |
GET /oauth/logout | OIDC RP-initiated logout |
Verify a token yourself
Fetch the JWKS, pick the key by the token's kid, verify the RS256 signature, then check iss equals your issuer and exp is in the future.
import { createPublicKey, createVerify } from 'crypto'
const verify = async (token, { jwksUri, issuer }) => {
const [h, p, s] = token.split('.')
const header = JSON.parse(Buffer.from(h, 'base64url'))
const { keys } = await (await fetch(jwksUri)).json()
const jwk = keys.find(key => key.kid === header.kid)
if (!jwk) throw new Error('unknown key')
const valid = createVerify('RSA-SHA256').update(`${h}.${p}`).verify(createPublicKey({ key: jwk, format: 'jwk' }), Buffer.from(s, 'base64url'))
const payload = JSON.parse(Buffer.from(p, 'base64url'))
if (!valid || payload.iss !== issuer || payload.exp * 1000 < Date.now()) throw new Error('invalid token')
return payload
}
Cache the JWKS for up to an hour.