Skip to main content

sdk.Auth()

The Auth helper implements the OpenID Connect login flow and token verification. It is a singleton created by initScenid. You do not construct it.

const auth = sdk.Auth()

For a walkthrough see User login and Verify tokens.

beginFlow({ redirectUri, state, scope })​

Returns the authorization URL to redirect the user to. Synchronous.

ParameterDescription
redirectUriMust match a redirect URI of the app
stateOpaque value you verify in the callback (CSRF protection)
scopeDefault openid profile email offline_access
const url = sdk.Auth().beginFlow({ redirectUri: 'http://localhost:3000/callback', state })

completeFlow(code, redirectUri)​

Exchanges the authorization code. Resolves { idToken, refreshToken, user }.

FieldDescription
idTokenID token (or the access token if none was issued)
refreshTokenPresent if the offline_access scope was granted. Valid 12 hours
userDecoded payload of idToken

Errors: auth/complete-flow-failed: <status> <body>, auth/complete-flow-no-token.

refreshFlow(refreshToken)​

Returns the same shape as completeFlow, using the refresh token.

Errors: auth/refresh-flow-failed: <status> <body>, auth/refresh-flow-no-token. Server codes: oauth/invalid-refresh-token, oauth/client-mismatch, oauth/account-pending, oauth/account-denied, oauth/account-deactivated.

verify(idToken)​

Verifies the RS256 signature via the JWKS, the issuer and the expiry. Resolves the payload.

ErrorMeaning
sdk/invalid-token-formatNot a JWT
auth/jwks-key-not-foundkid unknown
auth/jwks-fetch-failed: <status>Could not load the keys
auth/invalid-signatureSignature does not match
auth/invalid-issueriss differs from the service key issuer
auth/token-expiredexp in the past

The JWKS is cached for one hour. aud is not checked.

middleware()​

Express middleware. Sets req.user (user tokens) or req.serviceClient (service tokens) when a valid Bearer token is present. Always calls next(). See Verify tokens.

clearExchangeCache(sub)​

Drops the cached user tokens for a user, call it on logout.

exchangeToken(idToken) and getServiceToken()​

Called automatically by asUser and asAdmin. You rarely need them directly.

  • exchangeToken: RFC 8693 token exchange, cached per sub:client_id, expires 60 seconds early.
  • getServiceToken: client_credentials grant, cached per client_id, expires 30 seconds early.

Errors: auth/exchange-failed: <status> <body>, oauth/token-request-failed: <status> <body>.

AuthenticationError​

extends Error, message auth/unauthorized, httpCode = 401. Thrown by the guards.