Kiln / Accounts
Start here01
The language02
- 01The basics
- 02Values and numbers
- 03Variables
- 04Calls and expressions
- 05Conditions and loops
- 06Strings
- 07Arrays and hashes
- 08Classes and records
- 09Traits and enums
- 10Blocks
- 11Methods
- 12Imports and exports
- 13Foreign files
- 14Failures
Programs and the world03
- 01Files, input and output
- 02JSON, time, URLs and random numbers
- 03Html and forms
- 04Templates: Ochre
- 05Built-in methods
Kiln04
04 Kiln / 05
Accounts
Passwords, who's signed in, guards for the pages that need someone, and forms that only the app's own pages can send.
Kiln keeps who's signed in, and refuses changes that didn't come from the app's own pages. Each app writes its own sign-in, its own tables of people, and its own pages, with these pieces.
Passwords
Passwords are the password package's: an app needs it (lode add password --git <its repository>), and its README says how it's used. Password.hash(password) gives what to keep (never the password itself), and Password.matches(password, hash: hash) checks one against it.
Signing in and out
A response signs someone in to the visitor's session, by their number, or signs them out. From then on, each request says who's signed in:
static def sign_in(request: Request): Response!
const form = try Form.decode(request.body, as: SignIn)
const matching = staff.filter do (member)
return member.email == form.email
end
const found = try matching.first()
if found == nil or not Password.matches(form.password, hash: found.password)
return Response.html(sign_in_page.render(wrong: true), status: 422)
end
return Session.sign_in(Response.redirect("/office", status: Redirect.see_other), as: found.id)
end
static def sign_out(request: Request): Response
return Session.sign_out(Response.redirect("/", status: Redirect.see_other))
endSession.of(request)is the visitor'sSession:signed_in, the number signed in with, or nil; it's the app's to say what the number is (a staff member's id, say).- And
visitor, the session's own number, for what a guest has, like a bag; nil until something changes for them (a form they post), or they sign in. It's random, so a session never gets one that another had before, even in an earlier run of the program. - Sessions are kept while the application runs: one started again starts with none, and everyone signs in again. A session's cookie holds a random token, and Kiln keeps only the token's hash.
- Signing in gives the session a new token, so one known before (from a shared computer) is worth nothing after. Signing out ends the session.
- A session not used for 30 days ends by itself.
import "@kiln"
const request = new Request(method: "GET", path: "/", query: "", headers: {}, body: "")
const session = Session.of(request)
print(session.signed_in == nil) # prints trueGuards
A route can name a method that finds who may go on: a guard. It takes the Request, and gives what the handler takes next, or fails. The handler then gets it, and can get it no other way:
def staff_member(request: Request): Staff!
const who = Session.of(request).signed_in
if who == nil
return failure("nobody's signed in", kind: ErrorKind.denied)
end
return try staff.find(who)
end
Route.get("/office/orders", to: Office.orders, as: staff_member)
class Office
static def orders(request: Request, staff: Staff): Response!
...
end
end- The guard runs first; when it fails, the handler doesn't run, and the failure is answered as a handler's is (with
failed:, if there is one). - The handler takes the guard's value right after the Request, then the path's parts. Kiln holds routes, guards and handlers to that when the program is checked, and a handler that takes a record or object after the Request without a guard is a mistake.
- Finer rules, like who may refund, are checks in the handler: it has who it is.
Forms from elsewhere
Every request that changes something (a POST, PUT, PATCH or DELETE) must carry the visitor's CSRF token, so a page on another site can't post to the app in someone's name. Nothing needs writing for it:
- Kiln puts the token in every
<form method="post">of an HTML response, as a hidden field, and on the page's<body>, for htmx's requests to send as a header (X-Kiln-CSRF). The visitor's cookie holds it. - A request that changes something without it, with another, or that the browser says came from another site (
Sec-Fetch-Site: cross-site) is refused before a handler runs: 403, or the pagefailed:makes (kindforbidden, so it isn't taken for someone who isn't signed in, whose guard givesdenied). - A test that posts sends the token as a browser does: the cookie
kiln_csrf=..., and the same value as the fieldkiln_csrfor the headerX-Kiln-CSRF.