public AuthContext authenticate ( Request request ) { final boolean hasAuthHeader = request . header ( HttpHeaders . AUTHORIZATION ) . isPresent ( ) ; if ( ! hasAuthHeader ) { return Optional :: empty ; } final String authHeader = request . header ( HttpHeaders . AUTHORIZATION ) . get ( ) ; if ( ! authHeader . startsWith ( BEARER_PREFIX ) ) { throw new ResponseException ( Response . forStatus ( Status . BAD_REQUEST . withReasonPhrase ( "Authorization token must be of type Bearer" ) ) ) ; } final GoogleIdToken googleIdToken ; try { googleIdToken = authenticator . authenticate ( authHeader . substring ( BEARER_PREFIX . length ( ) ) ) ; } catch ( IllegalArgumentException e ) { throw new ResponseException ( Response . forStatus ( Status . BAD_REQUEST . withReasonPhrase ( "Failed to parse Authorization token" ) ) , e ) ; } if ( googleIdToken == null ) { throw new ResponseException ( Response . forStatus ( Status . UNAUTHORIZED . withReasonPhrase ( "Authorization token is invalid" ) ) ) ; } return ( ) -> Optional . of ( googleIdToken ) ; }
Authentication an incoming Styx API request .