Skip to main content

Authentication

Everything starts at the token endpoint of the auth service.

POST/oauth/tokenauth

The 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 -s https://auth.scenid.cloud/oauth/token \
-H 'Content-Type: application/json' \
-d '{
"grant_type": "client_credentials",
"client_id": "5b8c1f0e-…",
"client_secret": "…"
}'
Response 200
{
"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.

StatusBodyCause
400oauth/invalid-clientUnknown client
401oauth/invalid-client-secretWrong secret
403oauth/public-client-not-allowedPublic client without service key
403oauth/grant-not-allowedGrant not enabled for the app
400oauth/unsupported-grant-typeUnknown grant_type

Authorization code (users)​

  1. Send the user to GET /oauth/authorize?response_type=code&client_id=…&redirect_uri=…&scope=openid profile email offline_access&state=….
  2. The user signs in and consents. Scenid redirects to your redirect_uri with ?code=…&state=….
  3. 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​

EndpointDescription
GET /.well-known/openid-configurationDiscovery document. Auth method client_secret_post, PKCE S256
GET /oauth/.well-known/jwks.jsonPublic 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/userinfoWith a user Bearer token. 403 oauth/forbidden for other tokens
GET /oauth/logoutOIDC 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.