KScript docs
EN

Sign-in: KScript.Auth

Signing in to other apps (OpenID Connect)

An app can be the place other apps sign people in: they send the browser to it, it signs the person in with its own pages (passkeys, a password, anything), asks whether the app may know who they are, and sends them back with a code the app swaps for an ID token. That is OpenID Connect's code flow, with PKCE required. Turn it on with provider:

JSON
{ "http": { "auth": { "passkey_origin": "https://accounts.example.com", "provider": { "ui_locales": ["en-GB", "fr"] } } } }

KScript then answers these addresses itself:

Address What it is
/.well-known/openid-configuration Discovery: what apps need to know, such as where everything else is.
/jwks The public keys that sign ID tokens.
/auth Where apps send the browser to sign in.
/token Where apps swap a code for tokens, from their server.
/me Who the access token belongs to (userinfo).
/token/revocation Where an app gives a token back.
/session/end Where an app sends the browser to sign out.

One address may make 60 requests a minute to /auth and its steps, counted together; past that it gets a 429 with Retry-After. auth_limit changes the number, and 0 turns the limit off. Like throttle, it counts the visitor's own address when http.proxy is set, and an IPv6 visitor by their /64.

Tokens are signed with ES256. The keys come from an environment variable, OIDC_KEYS by default: a JWKS of private EC P-256 keys. kscript make provider-keys prints a new one. The first key signs; all are published, so to change keys add the new one first, wait a day, then move it to the front and later remove the old one. Never put the keys in a file in the project.

ID tokens carry sub, amr, the nonce, and the fields each granted scope gives: profile gives name unless scopes says otherwise ({"profile": ["name", "picture"]}). sub is the user's id unless subject names another field; whichever it is must never change or be reused. There are no refresh tokens: an access token lasts ten minutes and an ID token an hour, and the app signs in again (usually without the person seeing anything) when it needs to.

The apps

clients.json, beside kscript.json, lists the apps that may sign people in:

JSON
[
  {
    "client_id": "shop",
    "client_name": "The Shop",
    "redirect_uris": ["https://shop.example.com/auth/callback"],
    "post_logout_redirect_uris": ["https://shop.example.com/"],
    "client_secret_env": "SHOP_SECRET",
    "account_deletion_uri": "https://shop.example.com/auth/deleted"
  },
  {
    "client_id": "desktop",
    "client_name": "Desktop app",
    "redirect_uris": ["http://127.0.0.1:53682/callback"],
    "jwks_uri": "https://desktop.example.com/.well-known/jwks.json"
  }
]

An app proves itself at /token with a secret (client_secret_env names the environment variable that holds it, at least 32 characters), or with an assertion signed by its own key (jwks or jwks_uri, so you only ever hold its public key). An address must match a redirect_uris entry exactly. The file is read when the server starts, and a mistake in it, or missing keys, stops it starting.

The pages

/auth sends the browser to {interaction}/{uid} (/interaction/{uid} by default): your page for that sign-in step. @oidc_interaction($uid) gives the step, or null when this browser didn't start it or it ran out. Its prompt says what to show: login (sign in or create an account) or consent (may this app know who they are). Once someone has signed in, send the browser to the step's resume. For consent, post a form to your own route and answer it with @oidc_consent($uid, allow: true), which gives the address to go to next. @oidc_abort($uid) cancels.

<!-- routes.kscript -->
<KScript.Routes>
  <import package="KScript.Auth" />

  <Get path="/interaction/{uid}">
    <let name="sign_in" value="@oidc_interaction($uid) ?? @abort(400, message: 'That sign-in has run out. Start again from the app.')" />
    <return value="@view($sign_in.prompt == 'login' ? 'sign-in' : 'consent', sign_in: $sign_in)" />
  </Get>
</KScript.Routes>
// routes.kscript
import KScript.Http
import KScript.Auth

Routes {
  Get(path: /interaction/{uid}) {
    $sign_in = @oidc_interaction($uid) ?? @abort(400, message: 'That sign-in has run out. Start again from the app.')
    return @view($sign_in.prompt == 'login' ? 'sign-in' : 'consent', sign_in: $sign_in)
  }
}

@passkey(action, step, body) runs a passkey ceremony from your own routes, so a sign-in page can post to an address of its own.

A step works only in the browser that started it: /auth sets a host-only __Host-ixb_{uid} cookie, and the step's pages and its resume check it, so a page on another subdomain can't slip someone else's sign-in into a visitor's browser.

The provider draws three pages of your site, if they exist: _logout.html (asked before signing out, with $form, the form to put in with {! $form }, and $client_name), _logged-out.html, and _error.html (with $code, for a request that can't go back to its app). Only the error's code reaches the page.

What people have agreed to

A grant belongs to the person, not to one browser: agreeing once counts on every device, for a year. @oidc_connections() lists the apps the signed-in user has let in, and @oidc_disconnect($client_id) takes one away: its tokens stop working at once, and it asks again next time. A code used twice takes away the grant it came from, since it was stolen or leaked.

When an account is deleted, call @oidc_account_deleted($user). Each app with an account_deletion_uri is sent a signed Security Event Token (RFC 8417, pushed as RFC 8935 says) with the account-purged event and the user's sub, retried after a minute, five minutes, half an hour and so on up to a day, until it answers with 2xx.

Before deleting, an app can say which subscriptions the person has with it. Give it a subscriptions_uri in clients.json, and @oidc_subscriptions($user) asks every such app at once: it POSTs a JWT (typ subscriptions-query+jwt, signed with the ID token key, naming the person in sub_id and lasting a minute) as application/jwt, and the app answers {"subscriptions": [{"name", "store", "renews", "ends_at", "manage_url"}]}. You get one map per app: client_id, client_name, checked (false when it couldn't be asked within five seconds or answered with something else) and its subscriptions. An app with "block_deletion_with_subscriptions": true also says blocks while one of them still renews; ask again with blocking_only: true just before deleting, since one can start after the page was drawn. An app that couldn't be asked never blocks.

To let someone download their data, an app can hand over its own copy. Give it a data_export_uri, and @oidc_data_exports($user) asks every such app at once, the same way: a JWT with typ data-export-query+jwt, lasting a minute. The app answers with a JSON object of what it holds about that person ({} when it doesn't know them). You get one map per app: client_id, client_name, included (false when it couldn't be asked within 30 seconds, answered with more than 25 MB or with something that isn't an object) and data. Put it all in one file:

<fn name="download">
  <let name="apps" value="@oidc_data_exports(@user())" />
  <return value="@response({
      apps: $apps->filter(fn(a) => $a.included)->map(fn(a) => { app: $a.client_name, data: $a.data }),
      not_included: $apps->filter(fn(a) => not $a.included)->map(.client_name),
    })->with_header('Content-Disposition', 'attachment; filename=&quot;my-data.json&quot;')" />
</fn>
fn download() {
  $apps = @oidc_data_exports(@user())
  return @response({
    apps: $apps->filter(fn(a) => $a.included)->map(fn(a) => { app: $a.client_name, data: $a.data }),
    not_included: $apps->filter(fn(a) => not $a.included)->map(.client_name),
  })->with_header('Content-Disposition', 'attachment; filename="my-data.json"')
}