daravel_session library
Laravel-compatible sessions for Daravel: the sessions table and file
format, encrypted cookies and CSRF protection, so a Laravel application
and a Daravel application with the same APP_KEY and store share a
visitor's session.
Application.configure(basePath: '.')
.withSession()
.withMigrations(const [CreateSessionsTable()])
.withRouting(web: (r) => r.group((r) {...}, middleware: ['web']))
Classes
- ArraySessionHandler
-
Sessions in this isolate's memory: tests and tools. Laravel's
arraydriver, which likewise forgets everything when the process ends. - CookieValuePrefix
-
Laravel's
CookieValuePrefix: an encrypted cookie's clear text starts withhmac_sha1(name . 'v2', key)in hex and|, so a cookie cannot be moved to another name. - CreateSessionsTable
-
Laravel's
sessionstable (laravel/laravel0001_01_01_000000_create_users_table, which creates it next tousers):id,user_id,ip_address,user_agent,payload,last_activity. It creates nothing when the table exists already, since a Laravel application's own migration then owns it. - DatabaseSessionHandler
-
Laravel's
sessionstable (id,user_id,ip_address,user_agent,payload,last_activity): the payload is theserialize()text in base64 andlast_activitya Unix timestamp, so a Laravel application on the same database shares the sessions. - EncryptCookiesMiddleware
-
Encrypts every cookie the response sets and decrypts the ones the
request carries (Laravel's
EncryptCookies, withCryptand CookieValuePrefix), so a Laravel application with the sameAPP_KEYreads and writes the same cookies. A cookie that does not decrypt, or was made for another name, is dropped from the request. except names cookies left in clear. - FileSessionHandler
-
One file per session under
storage/framework/sessions, named by the id and holding theserialize()payload: Laravel'sfiledriver, so both frameworks on one machine can read each other's files. Expiry is the file's modification time (wall clock, as Laravel reads it). - Session
-
One request's session (Laravel's
Illuminate\Session\Store): a map of attributes loaded from a SessionHandler at the start of the request and written back at its end, so a Laravel application sharing the store reads the same session. - SessionConfig
-
SESSION_*settings (Laravel'sconfig/session.php). -
SessionGuard<
T extends Authenticatable> -
Laravel's
SessionGuard: the user id lives in the session under the samelogin_web_<sha1>key, so a user logged in on either side of a shared store is logged in on both. - SessionHandler
-
Where sessions are kept (PHP's
SessionHandlerInterface, as Laravel's handlers implement it). read returns the serialized payload, ornullfor a missing or expired session. - SessionManager
-
Builds the SessionHandler of the configured driver and a Session
per request (Laravel's
SessionManager). - StartSessionMiddleware
-
Starts the session for a request and saves it after the response
(Laravel's
StartSession): the id comes from the session cookie (decrypted by EncryptCookiesMiddleware when that runs first), the attributes from the handler; the response gets the cookie back, the previous URL is recorded forGETpage requests, and 2 requests in 100 sweep expired sessions. - VerifyCsrfTokenMiddleware
-
Laravel's
VerifyCsrfToken: aPOST,PUT,PATCHorDELETErequest must carry the session's token as the_tokeninput, theX-CSRF-TOKENheader or theX-XSRF-TOKENheader (the encryptedXSRF-TOKENcookie, which this middleware sets on every response so Axios and Angular send it back). except lists paths that skip the check ('webhooks/*'). Needs StartSessionMiddleware before it.
Extensions
- RequestSession on Request
-
request.session: the session started byStartSessionMiddleware. - SessionApplicationBuilder on ApplicationBuilder
-
withSession()on the application builder. - SessionAuthManager on AuthManager
-
auth.session<User>('web', provider)inwithAuth.
Functions
-
redirectInvalidRequest(
ValidationException error, Request request) → Future< Response?> -
Handler::invalid(): a browser or an Inertia visit goes back with the errors and its input flashed. Clients expecting JSON, and requests without a session, fall through to the 422.
Exceptions / Errors
- TokenMismatchException
-
Laravel's
TokenMismatchException: rendered as419withCSRF token mismatch.