- OCaml 100%
auth #
User sign-in and server-side sessions for OCaml web applications.
A web application that lets people sign in with GitHub or Google needs the
same handful of steps every time: redirect to the provider, check the state
parameter that comes back, exchange the code for a token, find or create the
user, and hand the browser a session cookie that can later be revoked. auth
does these steps for applications built on respond. It keeps users and
sessions in SQLite, so a session can be revoked on the server. Accounts are
keyed by the provider and its user id; whether a sign-in needs a verified
email address, records one if there is one, or never asks for one is the
application's choice (~email:Required, `` Optional or `Ignore ``).
Session tokens are 32 random bytes, hex-encoded, and each token is the key of
one row in the sessions table. The row's value is a JSON Web Token (RFC 7519)
whose sub is the user's id and whose exp ends the session, signed with
csrf as a compact JWS under HS256 (RFC 7515, RFC 7518) with a key that
HKDF-SHA-256 derives from the cookie secret, so an edited value is refused when
the session is looked up. The token itself is not covered: a valid value copied
from one row into another is accepted.
The session cookie carries three attributes. HttpOnly hides it from the
page's JavaScript, so a script injected into the page cannot read the session.
SameSite=Lax stops the browser sending it on requests that another site
triggers, except a top-level navigation with GET. Secure, set when the base
URL is HTTPS, stops it being sent over plain HTTP.
The OAuth callback defends against login CSRF (cross-site request forgery), an
attack in which a hostile page makes the victim's browser finish a sign-in with
the attacker's account, so that the victim then works in a session the
attacker can read (RFC 6749, section 10.12). The callback requires the state
parameter to match the cookie of that sign-in, set in the browser that started
it and holding the state in a JWT that expires with the cookie, so a callback
started from another browser fails. Sign-out requires a same-origin Origin or
Referer header.
A user holds at most 10 sessions; signing in again evicts the oldest.
The SQLite store assumes a single process. It must not sit on a network file
system such as NFS or be shared between instances, and expired sessions that
are never looked up again stay in the table until
Auth.Store.purge_expired_sessions removes them.
Installation #
Install with opam:
$ opam install auth
If opam cannot find the package, it has probably not reached the public
opam-repository yet. Add the overlay repository, then install it:
$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install auth
Quick start #
let run () =
Eio_main.run @@ fun env ->
Eio.Switch.run @@ fun sw ->
let fs = Eio.Stdenv.fs env in
let http =
Requests_eio.v ~sw ~mono:(Eio.Stdenv.mono_clock env) ~clock:(Eio.Stdenv.clock env) (Eio.Stdenv.net env)
in
let store = Auth.Store.v ~sw Eio.Path.(fs / "data" / "auth.db") in
let cfg =
Auth.config
~oauth_provider:Oauth.Provider.Github
~client_id:"Iv1.abc"
~client_secret:"secret"
~base_url:"https://app.com"
~cookie_secret:"a-cookie-secret-of-at-least-32-chars"
~http ~email:`Required ()
in
let routes = Auth.routes cfg store in
Respond_eio.run ~max_connections:1024 ~mono:(Eio.Stdenv.mono_clock env)
~net:(Eio.Stdenv.net env)
~addr:(`Tcp (Eio.Net.Ipaddr.V4.any, 8080))
~root:Eio.Path.(fs / "static")
routes
The library registers three routes, where <provider> is the provider's slug
(for example github):
GET /auth/<provider>redirects to the OAuth provider.GET /auth/<provider>/callbackhandles the provider's callback and creates the session.POST /auth/signoutrevokes the session and clears the cookie.
A provider whose endpoints are read from a discovery document, served by a
process that may start after this one, is given by Auth.deferred_config with
its slug and a resolver: the first sign-in that needs it reads it, a failed
reading answers 502 naming why, and a successful one is held.
Auth.callback_url is the URL the provider's client registration must hold.
Auth.current_user reads the signed-in user of a request from the sid
cookie in its headers, and Auth.current_session the session with it.
Licence #
ISC