Says who may do what to a model's records. Each <fn> inside is an ability, named for what it allows (create, read, update, delete, or your own), that takes the user and then the record and returns true or false: policy for Post { fn update(user: User, post: Post) => $user.id == $post.author_id }. A user typed User? lets a guest reach the function with null; any other type turns a guest away first. fn before(user) runs ahead of every ability: true or false decides, null carries on. Keep one policy per model, in the project's policies folder, where @can finds it by the model's name.Says who may do what to a model's records. Each fn inside is an ability, named for what it allows (create, read, update, delete, or your own), that takes the user and then the record and returns true or false: policy for Post { fn update(user: User, post: Post) => $user.id == $post.author_id }. A user typed User? lets a guest reach the function with null; any other type turns a guest away first. fn before(user) runs ahead of every ability: true or false decides, null carries on. Keep one policy per model, in the project's policies folder, where @can finds it by the model's name.
Says who may do what to a model's records. Write policy for Post { fn update(user: User, post: Post) => $user.id == $post.author_id }.
Holds statements.Holds statements in { }. Scripts only.
In a test, sets who @can, @authorize and @user() ask about until the test ends, in the test and in routes it calls with @api(/...): @acting_as($editor). Pass null to act as a guest.
In a test, acts as a guest until the test ends, in the test and in routes it calls with @api(/...) or @visit(): @guest() is this id (a new one when it is left out), and no one is signed in until a request sends the session cookie of someone who signed in, as a browser would after sign-in. Returns the guest id.
Makes an API token for a user and returns it. Show it once: only its hash is kept. Requests that send it as Authorization: Bearer <token> are signed in as the user (the auth middleware lets them through). expires is how long it works for, such as 30d; by default it lasts until it is revoked.
Signs someone in with an email and password: returns the user, or null when they don't match. session: false only checks them, for an API that answers with a token instead of a cookie. After http.auth.attempts (5) failures for the same email and address in http.auth.lockout (a minute), it throws a TooManyRequests error that says how long to wait (kscript serve answers it with 429).
Parameter
Takes
emailrequired
any value
passwordrequired
any value
rememberoptional
a flag: remember or not remember
sessionoptional
a flag: session or not session
Arguments can be named: @attempt(…, password: …, remember: …).
Stops unless the model's policy lets the user use the ability: a Forbidden error (403 in a route), or Unauthorized (401, or the sign-in page) for a guest the ability turns away. Returns the target, so it can be chained.
True when the model's policy lets the user use the ability on a record: @can(update, $post). For an ability with no record yet, name the model: @can(create, Post). The user is the signed-in one, or null for a guest.
The users kept as files in the app's data folder, as before http.auth.users kept them in a model: each with its id, password_hash and its created_at and updated_at as dates. For a migration's Run block that copies them into the User model with the ids they had.
Example
<echo>{ @file_users()->count() } people signed up before the users moved into a model</echo>
echo "{ @file_users()->count() } people signed up before the users moved into a model"
The browser's guest id: the key of its Guest (a uuid, unless your copy of Guest says otherwise), kept in a long-lived cookie (http.auth.guest_lifetime, a year by default) and made the first time it is read. Null once someone is signed in, and outside a request. Ownable records a guest makes belong to it.
True when the signed-in user signed in or checked their passkey within this long (http.auth.confirm_within, or 5 minutes, by default). Ask for a passkey check before something that can't be undone, such as deleting the account.
Signs a user in: the session gets a new id and the user. remember keeps them signed in for http.auth.remember (30 days) after the browser closes. The user is a map with an id: from @register(), @users()->find(), or a store of your own.
Call as a user's account is deleted: their app grants go, and each app with an account_deletion_uri is sent a signed account-purged notice, retried until it answers. Gives how many notices were queued.
Answers a consent step: allow lets the app see what it asked for, from now on; otherwise the app is told access_denied. Gives the address to send the browser to next, or null when the step isn't this browser's.
Parameter
Takes
uidrequired
any value
allowrequired
a flag: allow or not allow
Arguments can be named: @oidc_consent(…, allow: …).
The sign-in step an app sent this browser to (http.auth.provider), for the page at {interaction}/{uid}: uid, prompt (login or consent), client_id, client_name, return_host, scopes, ui_locales, login_hint and resume, the address to send the browser to once the step is done. Null unless this browser started it and it hasn't run out.
#@oidc_subscriptions(user, blocking_only?: bool)→ list
Asks each app with a subscriptions_uri which subscriptions the user has there, all at once, before their account is deleted: one map per app with client_id, client_name, blocking, checked (false when the app couldn't be asked), subscriptions (name, store, renews, ends_at, manage_url) and blocks (it has block_deletion_with_subscriptions and one still renews). blocking_only asks only those apps.
Parameter
Takes
userrequired
any value
blocking_onlyoptional
a flag: blocking_only or not blocking_only
Arguments can be named: @oidc_subscriptions(…, blocking_only: …).
Runs one step of a passkey ceremony from one of the app's own POST routes, as /_ks/passkeys/{action}/{step} does: action is login, register or signup, step is options or verify, and body is the JSON the browser sent. Gives the answer for the browser as a map. A failed check throws (Forbidden, Validation or Unauthorized).
Arguments can be named: @passkey(…, step: …, body: …).
Lists the signed-in session user's passkeys for this site: each one's id, created_at, last_used_at (null until it signs in) and synced (true for a passkey that is backed up, such as one in a password manager).
Adds a user and returns it (without signing them in). The email and password are checked by the email and password types, with your project's rules ("types": { "password": { "min": 12 } } in kscript.json); a problem, or an email that is taken, is a Validation error naming the field. The password is kept only as an argon2id hash. Other named values are kept as the user's fields.
Parameter
Takes
emailrequired
any value
passwordrequired
any value
fields
User, any number of them
Arguments can be named: @register(…, password: …, fields: …).
Finishes a password reset: gives the token's user the new password (checked by the password type) and ends their sessions. Returns the user, or null when the token is wrong, used or too old.
Arguments can be named: @reset_password(…, password: …).
Starts a password reset: a token to put in a link you email to the person, or null when no user has that email. It works once, for http.auth.reset_lifetime (an hour), and a new one replaces the last. Tell the person the same thing either way, so the page doesn't show who has an account.
Revokes an API token, given as the token or its id (from @api_tokens()). With user, only a token of theirs is revoked, so a page can let people revoke their own. True if there was one.
Arguments can be named: @revoke_token(…, user: …).
The signed-in user (a map with id, email and the fields they registered with), or null for a guest. It comes from the session cookie, or from an API token (Authorization: Bearer).
The app's users, kept in its data folder: ->find(id), ->find_email(email), ->all(), ->count(), ->verify(email, password), ->update(id, ...), ->set_password(id, password), ->delete(id).
Example
<echo>{ @users()->count() } people have signed up</echo>
echo "{ @users()->count() } people have signed up"
A token that proves a user owns their email address, to put in the link of a verify-your-email message: @route('verification.verify', token: @verification_token($user)). It is signed with the app's secret (KSCRIPT_SECRET), names the user and their email, and works until expires (an hour by default), or until their email changes. Nothing is kept: @verify_email() checks it.
Finishes email verification: sets the token's user's email_verified_at to now and returns the user, or null when the token is wrong, has run out, or was made for an email the user no longer has. A user who is already verified keeps the time they were first verified. The verified middleware lets only verified users through.
The request's session: values kept between requests by a cookie. $session->set('cart', $items), $session->get('cart'), and $session->flash('status', 'Saved') for a message after a redirect.
Models and mixins
KScript.Auth ships these, written in KScript:
User, a model: Someone with an account. KScript.Auth keeps the users in this model when kscript.json says "http": { "auth": { "users": "User" } }: @register makes one, and @user() is the one…
Guest, a model: Someone using the app without an account. Each browser gets one, kept in a long-lived cookie, and it is stored the first time it owns a record. Its key is what @guest() gives. It…
Ownable, a mixin: Rows that belong to one owner: the signed-in user, or the browser's guest. A model that extends Ownable gets a user and a guest, one of them filled in from who is making a record…
Every option a model takes and every built-in mixin: Models and mixins.