protected Authentication getAuthenticationCookie ( HttpServerExchange exchange ) { Authentication authentication = Authentication . create ( ) . withSubject ( null ) ; String cookieValue = getCookieValue ( exchange , this . config . getAuthenticationCookieName ( ) ) ; if ( StringUtils . isNotBlank ( cookieValue ) ) { try { String decryptedValue = Application . getInstance ( Crypto . class ) . decrypt ( cookieValue , this . config . getAuthenticationCookieEncryptionKey ( ) ) ; JwtConsumer jwtConsumer = new JwtConsumerBuilder ( ) . setRequireSubject ( ) . setVerificationKey ( new HmacKey ( this . config . getAuthenticationCookieSignKey ( ) . getBytes ( StandardCharsets . UTF_8 ) ) ) . setJwsAlgorithmConstraints ( new AlgorithmConstraints ( ConstraintType . WHITELIST , AlgorithmIdentifiers . HMAC_SHA512 ) ) . build ( ) ; JwtClaims jwtClaims = jwtConsumer . processToClaims ( decryptedValue ) ; String expiresClaim = jwtClaims . getClaimValue ( ClaimKey . EXPIRES . toString ( ) , String . class ) ; if ( ( "-1" ) . equals ( expiresClaim ) ) { authentication = Authentication . create ( ) . withSubject ( jwtClaims . getSubject ( ) ) . twoFactorAuthentication ( jwtClaims . getClaimValue ( ClaimKey . TWO_FACTOR . toString ( ) , Boolean . class ) ) ; } else if ( LocalDateTime . parse ( jwtClaims . getClaimValue ( ClaimKey . EXPIRES . toString ( ) , String . class ) , DateUtils . formatter ) . isAfter ( LocalDateTime . now ( ) ) ) { authentication = Authentication . create ( ) . withExpires ( LocalDateTime . parse ( jwtClaims . getClaimValue ( ClaimKey . EXPIRES . toString ( ) , String . class ) , DateUtils . formatter ) ) . withSubject ( jwtClaims . getSubject ( ) ) . twoFactorAuthentication ( jwtClaims . getClaimValue ( ClaimKey . TWO_FACTOR . toString ( ) , Boolean . class ) ) ; } else { //Ignore this and use default authentication } } catch ( MalformedClaimException | InvalidJwtException e ) { LOG . error ( "Failed to parse authentication cookie" , e ) ; authentication . invalidate ( ) ; } } return authentication ; }
Retrieves the current authentication from the HttpServerExchange