Signing in with another site's account
The other way round from the provider: people sign in to your app with an account they have on another site, an OpenID Connect provider (a KScript app with http.auth.provider is one). Your app never sees their password or passkey. It sends them to the provider, they come back with a one-time code, and your server swaps it for a signed ID token that says who they are. Register the app with the provider (its redirect_uris and a client secret), then say where the provider is in kscript.json:
{
"http": {
"auth": {
"sign_in_with": {
"issuer": "https://accounts.example.com",
"client_id": "game",
"redirect_uri": "https://game.example.com/auth/callback",
"logout_redirect_uri": "https://game.example.com/"
}
}
}
}| Setting | Default | What it does |
|---|---|---|
issuer |
The provider's https address (http only for localhost). Its discovery document says where everything else is. | |
client_id |
The id the provider knows the app by. | |
secret_env |
OIDC_CLIENT_SECRET |
The environment variable holding the app's client secret. |
redirect_uri |
The app's callback address, exactly as the provider has it. | |
logout_redirect_uri |
none | Where the provider sends people after they sign out there. |
scopes |
["profile"] |
The scopes asked for beyond openid. |
lifetime |
10m |
How long someone has to finish signing in at the provider. |
Four commands do the rest. @openid_start(return_to) keeps a fresh state, nonce and PKCE verifier in the session and gives the address to redirect to. @openid_finish(), on the callback route, checks the state, swaps the code and checks the ID token's signature (ES256, from the provider's keys), issuer, audience, times and nonce. It gives a map whose status is signed_in (with sub, name, email and all the claims), declined (they said no at the provider), expired (no sign-in this browser started is waiting, or it is used already) or failed (with error saying what didn't check out), and return_to, always a path on this site. It doesn't sign anyone in: find or add your user by sub, the one thing about them that never changes, then @login() them.
<import package="KScript.Http" />
<import package="KScript.Auth" />
<import path="../models/user.kscript" />
<fn name="start" params="query: map" value="@redirect(@openid_start($query.return_to))" />
<fn name="callback">
<let name="result" value="@openid_finish()" />
<if ($result.status != 'signed_in')>
<return value="@redirect($result.return_to)" />
</if>
<!-- The users are kept in a model ("users": "User") with a sub field. -->
<let name="user" value="User->update_or_create({ sub: $result.sub }, { name: $result.name ?? '' })" />
<do>@login($user)</do>
<return value="@redirect($result.return_to)" />
</fn>
<fn name="sign_out">
<do>@logout()</do>
<return value="@redirect(@openid_logout_url() ?? '/')" />
</fn>
<Routes>
<Get path="/auth/login" to="start" />
<Get path="/auth/callback" to="callback" />
<Post path="/auth/logout" to="sign_out" />
</Routes>import KScript.Http
import KScript.Auth
import "../models/user.kscript"
fn start(query: map) => @redirect(@openid_start($query.return_to))
fn callback() {
$result = @openid_finish()
if ($result.status != 'signed_in') {
return @redirect($result.return_to)
}
// The users are kept in a model ("users": "User") with a sub field.
$user = User->update_or_create({ sub: $result.sub }, { name: $result.name ?? '' })
@login($user)
return @redirect($result.return_to)
}
fn sign_out() {
@logout()
return @redirect(@openid_logout_url() ?? '/')
}
Routes {
Get(path: /auth/login, to: @start)
Get(path: /auth/callback, to: @callback)
Post(path: /auth/logout, to: @sign_out)
}When someone deletes their account at the provider, it can tell your app: it POSTs a signed notice (a Security Event Token, application/secevent+jwt) to the address it has for the app, such as the account_deletion_uri of a KScript provider. @openid_notice($body) checks it: the signature, that it is from the provider and for this app alone, that it is recent and can't be an ID token, and that it carries one event. It gives event: purged with sub, the account that is gone, so delete what you hold for it; or verification, the provider's dry run, which deletes nothing. A notice that doesn't check out gives error (invalid_request, authentication_failed, invalid_issuer or invalid_audience) and description instead, to answer 400 with. Answer a good notice 202, even for a sub you don't know, so the provider stops sending it. Put the route in an API routes file, since the provider sends no CSRF token.
A provider can also ask about one person (a KScript provider does, from @oidc_data_exports() and @oidc_subscriptions()): it POSTs a signed question as application/jwt to the app's data_export_uri or subscriptions_uri. @openid_question($body) checks it the same way, and that it hasn't run out and was signed in the last two minutes. It gives question (data_export or subscriptions) and sub, the account asked about; answer with what you hold about them, or {} for an account you don't know. A question that doesn't check out gives error and description, as a notice does.