Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.

public final class CookieCsrfTokenRepository

  1. Object
  2. CookieCsrfTokenRepository

ImplementsCsrfTokenRepository

Keeps the token in a cookie, XSRF-TOKEN, for a page whose script reads the cookie and sends its value back in the X-XSRF-TOKEN header – the convention Angular and axios follow. The token is also kept in the session so a sibling subdomain cannot inject a cookie and submit a matching value.

http.csrf(csrf -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()));

A script can only read the cookie when it is not HttpOnly, which is what withHttpOnlyFalse is for. Because the script has the token as the cookie holds it, a chain with this repository accepts the token unmasked in the header as well as masked. A cookie is trusted only when it matches the token issued to the current session. Tokens copied from other sessions are rejected.

Constructors

public CookieCsrfTokenRepository()

Methods

public static CookieCsrfTokenRepository withHttpOnlyFalse()A repository whose cookie a script can read.
public void setCookieName(String cookieName)
public void setHeaderName(String headerName)
public void setParameterName(String parameterName)
public void setCookiePath(String cookiePath)The cookie’s path; / unless set.
public void setCookieHttpOnly(boolean cookieHttpOnly)
public CsrfToken generateToken(HttpServer.Request request)A new token, not yet stored.
public void saveToken(CsrfToken token, HttpServer.Request request)Stores token for the client; null forgets the stored one.
public CsrfToken loadToken(HttpServer.Request request)The stored token, or null when the client has none.

Inherited methods

Constructor details

CookieCsrfTokenRepository

public CookieCsrfTokenRepository()

Method details

withHttpOnlyFalse

public static CookieCsrfTokenRepository withHttpOnlyFalse()
A repository whose cookie a script can read.

setCookieName

public void setCookieName(String cookieName)

setHeaderName

public void setHeaderName(String headerName)

setParameterName

public void setParameterName(String parameterName)

setCookiePath

public void setCookiePath(String cookiePath)
The cookie’s path; / unless set.

setCookieHttpOnly

public void setCookieHttpOnly(boolean cookieHttpOnly)

generateToken

public CsrfToken generateToken(HttpServer.Request request)
A new token, not yet stored.

saveToken

public void saveToken(CsrfToken token, HttpServer.Request request)
Stores token for the client; null forgets the stored one.

loadToken

public CsrfToken loadToken(HttpServer.Request request)
The stored token, or null when the client has none.