protected void setFlashCookie ( HttpServerExchange exchange ) { Flash flash = this . attachment . getFlash ( ) ; Form form = this . attachment . getForm ( ) ; if ( flash . isDiscard ( ) || flash . isInvalid ( ) ) { final Cookie cookie = new CookieImpl ( this . config . getFlashCookieName ( ) ) . setHttpOnly ( true ) . setSecure ( this . config . isFlashCookieSecure ( ) ) . setPath ( "/" ) . setSameSite ( true ) . setSameSiteMode ( SAME_SITE_MODE ) . setDiscard ( true ) . setMaxAge ( 0 ) ; exchange . setResponseCookie ( cookie ) ; } else if ( flash . hasContent ( ) || form . flashify ( ) ) { try { JwtClaims jwtClaims = new JwtClaims ( ) ; jwtClaims . setClaim ( ClaimKey . DATA . toString ( ) , flash . getValues ( ) ) ; if ( form . flashify ( ) ) { jwtClaims . setClaim ( ClaimKey . FORM . toString ( ) , CodecUtils . serializeToBase64 ( form ) ) ; } LocalDateTime expires = LocalDateTime . now ( ) . plusSeconds ( SIXTY ) ; jwtClaims . setClaim ( ClaimKey . EXPIRES . toString ( ) , expires . format ( DateUtils . formatter ) ) ; JsonWebSignature jsonWebSignature = new JsonWebSignature ( ) ; jsonWebSignature . setKey ( new HmacKey ( this . config . getFlashCookieSignKey ( ) . getBytes ( StandardCharsets . UTF_8 ) ) ) ; jsonWebSignature . setPayload ( jwtClaims . toJson ( ) ) ; jsonWebSignature . setAlgorithmHeaderValue ( AlgorithmIdentifiers . HMAC_SHA512 ) ; String encryptedValue = Application . getInstance ( Crypto . class ) . encrypt ( jsonWebSignature . getCompactSerialization ( ) , this . config . getFlashCookieEncryptionKey ( ) ) ; final Cookie cookie = new CookieImpl ( this . config . getFlashCookieName ( ) ) . setValue ( encryptedValue ) . setSecure ( this . config . isFlashCookieSecure ( ) ) . setHttpOnly ( true ) . setSameSite ( true ) . setPath ( "/" ) . setSameSiteMode ( SAME_SITE_MODE ) . setExpires ( DateUtils . localDateTimeToDate ( expires ) ) ; exchange . setResponseCookie ( cookie ) ; } catch ( Exception e ) { //NOSONAR Intentionally catching exception here LOG . error ( "Failed to generate flash cookie" , e ) ; } } else { //Ignore and send no cookie to the client } }
Sets the flash cookie to current HttpServerExchange