Ruby SDK

Verify access tokens

Check a user's Scute access token on your server with the app's signing keys, and optionally confirm the session is still live.

scute.tokens.verify checks an access token locally and returns a Scute::Session. It needs only the app id: the signing keys come from the app's public JWKS.

scute = Scute::Client.newsession = scute.tokens.verify(token)session.user_id        # the Scute app user idsession.impersonated?  # true when someone (support) is signed in as this usersession.actor          # who, when impersonated: { "kind" => "backend", "email" => "support@acme.com" }

What is checked#

  1. The token is a JWT with three parts and JSON header and claims. Otherwise :malformed.
  2. The algorithm is RS256. Anything else, including alg: none, is :algorithm.
  3. The signature matches one of the app's keys from GET /v1/auth/:app_id/.well-known/jwks.json. The key is picked by kid when the token has one. Otherwise :signature.
  4. exp is present (else :malformed) and not more than 30 seconds in the past (else :expired).
  5. aid is this app's public id (app_...). Otherwise :wrong_app.
  6. The token is a user's session: it has a uuid claim and no m2m claim. Otherwise :not_a_user.

Every failure raises Scute::InvalidToken with reason set to the symbol. An empty or nil token is :missing.

begin  session = scute.tokens.verify(token)rescue Scute::InvalidToken => e  e.reason  # :missing, :malformed, :algorithm, :signature, :expired, :wrong_app, :not_a_user, :revokedend

Revoked sessions#

Local verification cannot see a session that was revoked after the token was issued. Pass remote: true where that matters. The SDK then also calls GET /v1/auth/:app_id/current_user with the token (one call). A 401, 403 or 404 answer raises Scute::InvalidToken with reason :revoked. Other API errors raise Scute::APIError.

session = scute.tokens.verify(token, remote: true)

Key cache#

ConstantValueMeaning
Scute::Tokens::KEYS_TTL600Keys are re-read every 10 minutes.
Scute::Tokens::REFETCH_AFTER60On a signature that matches no cached key, keys are re-read at most once a minute (key rotation).
Scute::Tokens::LEEWAY30Seconds of clock skew allowed on exp.

The cache lives on the Tokens object, so reuse one client. A copy made with with_client_ip after the first tokens call shares it.

The session#

Scute::Session is an immutable Data object.

user_idString
The uuid claim: the Scute app user id.
app_idString
The aid claim: the app's public id.
workspace_idString
The wid claim.
expires_atTime
From exp.
actorHash or nil
The act claim when the session is impersonated, otherwise nil.
claimsHash
All claims of the token.
MethodReturns
impersonated?true when the imp claim is true.
authz_context{ "impersonated" => true, "actor" => ... } for an impersonated session, otherwise {}. Pass it as context: to authz.check so permissions marked "not while impersonating" are refused.

App id#

scute.tokens.public_app_id returns the id the app's tokens carry. When app_id already starts with app_ it is returned as is. Otherwise the SDK reads it once from GET /v1/apps/:app_id and caches it.

In a controller#

Scute::Authentication reads the token from the request and calls verify for you. See Rails integration.