Class Csrf.Builder

java.lang.Object
com.jsgalactic.axiom.security.Csrf.Builder
Enclosing class:
Csrf

public static final class Csrf.Builder extends Object
Builds Csrf; used during configuration, not thread-safe.
  • Method Details

    • headerName

      public Csrf.Builder headerName(String headerName)
      Sets the request header carrying the token (default X-CSRF-Token). Browsers only send custom headers from scripts, which cross-origin pages cannot do without CORS approval.
      Parameters:
      headerName - an HTTP header token
      Returns:
      this builder
    • allowedOrigins

      public Csrf.Builder allowedOrigins(String... origins)
      Sets the origins allowed to send unsafe requests, as exact scheme://host[:port] values in lower case. Without any, an Origin must match the Host header.
      Parameters:
      origins - one or more origins
      Returns:
      this builder
    • allowSameSite

      public Csrf.Builder allowSameSite(boolean allow)
      Accepts Sec-Fetch-Site: same-site (other origins of the same registrable domain); off by default.
      Parameters:
      allow - whether to accept it
      Returns:
      this builder
    • fetchMetadata

      public Csrf.Builder fetchMetadata(boolean enabled)
      Turns the Sec-Fetch-Site check on (default) or off.
      Parameters:
      enabled - whether to check it
      Returns:
      this builder
    • exemptWhen

      public Csrf.Builder exemptWhen(Predicate<Request> exempt)
      Skips all checks for requests the predicate accepts, for example those with an Authorization header, which a browser never adds by itself. The predicate decides on request facts only; anything an attacker's page can cause (cookies, form posts) must not make it return true.
      Parameters:
      exempt - the exemption rule
      Returns:
      this builder
    • binding

      public Csrf.Builder binding(Function<Context,String> binding)
      Sets what a double-submit token is bound to (default: the principal, or empty when anonymous). Ignored by the synchronizer variant.
      Parameters:
      binding - a pure function of the request that does not use client-controlled data
      Returns:
      this builder
    • maxAge

      public Csrf.Builder maxAge(Duration maxAge)
      Sets how long a double-submit token is accepted (default 12 hours).
      Parameters:
      maxAge - 1 minute to 30 days
      Returns:
      this builder
    • clock

      public Csrf.Builder clock(Clock clock)
      Sets the time source of double-submit tokens, for deterministic tests.
      Parameters:
      clock - the clock
      Returns:
      this builder
    • random

      public Csrf.Builder random(SecureRandom random)
      Sets the source of randomness, for tests.
      Parameters:
      random - a cryptographically strong generator
      Returns:
      this builder
    • cookieName

      public Csrf.Builder cookieName(String cookieName)
      Sets the double-submit cookie name (default __Host-csrf, or csrf without Secure).
      Parameters:
      cookieName - a cookie token
      Returns:
      this builder
    • secure

      public Csrf.Builder secure(boolean secure)
      Sets Secure on the double-submit cookie (default on; off only for local development).
      Parameters:
      secure - whether the cookie is Secure
      Returns:
      this builder
    • sameSite

      public Csrf.Builder sameSite(SetCookie.SameSite sameSite)
      Sets SameSite on the double-submit cookie (default Lax).
      Parameters:
      sameSite - the attribute
      Returns:
      this builder
    • httpOnly

      public Csrf.Builder httpOnly(boolean httpOnly)
      Makes the double-submit cookie invisible to scripts (default off). The page then needs the token from Csrf.token(Context) instead of reading the cookie.
      Parameters:
      httpOnly - whether the cookie is HttpOnly
      Returns:
      this builder
    • build

      public Csrf build()
      Builds the middleware.
      Returns:
      the CSRF protection
      Throws:
      IllegalArgumentException - if the cookie settings are inconsistent