public UserInfo authenticate ( HttpServletRequest httpServletRequest , AuthInfo authInfo , String serviceName ) { Preconditions . checkNotNull ( httpServletRequest ) ; Preconditions . checkNotNull ( authInfo ) ; Optional < String > maybeAuthToken = extractAuthToken ( httpServletRequest ) ; if ( ! maybeAuthToken . isPresent ( ) ) { throw new UnauthenticatedException ( "No auth token is contained in the HTTP request" ) ; } JwtClaims jwtClaims = this . authTokenDecoder . decode ( maybeAuthToken . get ( ) ) ; UserInfo userInfo = toUserInfo ( jwtClaims ) ; String issuer = userInfo . getIssuer ( ) ; if ( ! this . issuersToProviderIds . containsKey ( issuer ) ) { throw new UnauthenticatedException ( "Unknown issuer: " + issuer ) ; } String providerId = this . issuersToProviderIds . get ( issuer ) ; // Check whether the provider id is allowed. if ( ! authInfo . isProviderIdAllowed ( providerId ) ) { String message = "The requested method does not allowed this provider id: " + providerId ; throw new UnauthenticatedException ( message ) ; } checkJwtClaims ( jwtClaims ) ; // Check the audiences decoded from the auth token. The auth token is allowed when // 1) an audience is equal to the service name, // or 2) at least one audience is allowed in the method configuration. Set < String > audiences = userInfo . getAudiences ( ) ; boolean hasServiceName = audiences . contains ( serviceName ) ; Set < String > allowedAudiences = authInfo . getAudiencesForProvider ( providerId ) ; if ( ! hasServiceName && Sets . intersection ( audiences , allowedAudiences ) . isEmpty ( ) ) { throw new UnauthenticatedException ( "Audiences not allowed" ) ; } return userInfo ; }
Authenticate the current HTTP request .