Class CSRFTokenFactory


  • public class CSRFTokenFactory
    extends Object
    Generates CSRF tokens and checks their validity.
    Author:
    Antonin Delpeuch
    • Field Detail

      • tokenCache

        protected final com.google.common.cache.LoadingCache<String,​Instant> tokenCache
        Maps each token to the time it was generated
      • timeToLive

        protected final long timeToLive
        Time to live for tokens, in seconds
      • tokenLength

        protected final int tokenLength
        Length of the tokens to generate
      • rng

        protected final SecureRandom rng
        Random number generator used to create tokens
    • Constructor Detail

      • CSRFTokenFactory

        public CSRFTokenFactory​(long timeToLive,
                                int tokenLength)
        Constructs a new CSRF token factory.
        Parameters:
        timeToLive - Time to live for tokens, in seconds
        tokenLength - Length of the tokens generated
    • Method Detail

      • getFreshToken

        public String getFreshToken()
        Generates a fresh CSRF token, which will remain valid for the configured amount of time.
      • validToken

        public boolean validToken​(String token)
        Checks that a given CSRF token is valid.
        Parameters:
        token - the token to verify
        Returns:
        true if the token is valid