Auth.Csrf
Auth
Double-submit CSRF cookies for browser apps.
Issue a token on bootstrap (HTML or API), set it with setCookie (readable by JS when httpOnly = False), and have the client echo it in the configured request header (default x-csrf-token) on mutating requests. protect skips safe methods (GET / HEAD / OPTIONS).
Types
Config
type Config = Config { cookieName : String
, headerName : String
, attrs : { httpOnly : Bool
, secure : Bool
, path : String
, sameSite : String
, maxAge : Maybe Int
}
}CSRF cookie configuration.
Examples
Auth.Csrf.config { cookieName = "_csrf" }Error
type Error
= MissingToken
| MismatchWhy CSRF verification failed.
Examples
Err Auth.Csrf.MismatchValues
config
config : { cookieName : String } -> ConfigBuild CSRF config. Default cookie attrs are Path=/, SameSite=Lax, **not** HttpOnly (so JS can mirror the value into a header).
Examples
csrf =
Auth.Csrf.config { cookieName = "_csrf" }withAttrs
withAttrs : { httpOnly : Bool , secure : Bool , path : String , sameSite : String , maxAge : Maybe Int } -> Config -> ConfigOverride cookie attributes.
Examples
Auth.Csrf.withAttrs { Cookies.defaults | secure = True, httpOnly = False } csrfwithHeaderName
withHeaderName : String -> Config -> ConfigOverride the request header name (default x-csrf-token).
Examples
Auth.Csrf.withHeaderName "x-xsrf-token" csrftoken
token : StringMint a cryptographically random CSRF token.
Examples
let t = Auth.Csrf.token in
Auth.Csrf.setCookie csrf tsetCookie
setCookie : Config -> String -> HeaderSet the CSRF cookie to value.
Examples
Auth.Csrf.setCookie csrf Auth.Csrf.tokenverify
verify : Config -> Request -> Result Error ()Compare the CSRF cookie to the request header.
Examples
Auth.Csrf.verify csrf reqprotect
protect : Config -> MiddlewareReject unsafe methods when CSRF verification fails (403).
Examples
Server.Middleware.stack [ Auth.Csrf.protect csrf ] handleerrorToString
errorToString : Error -> StringHuman-readable form of an Error for logs.
Examples
Auth.Csrf.errorToString MissingToken