OAuthAuthenticator implements WebappAuthenticator { public AuthenticationInfo authenticate(org.apache.catalina.connector.Request request, Response response) { String requestUri = request.getRequestURI(); String requestMethod = request.getMethod(); AuthenticationInfo authenticationInfo = new AuthenticationInfo(); if ((requestUri == null) || ("".equals(requestUri))) { authenticationInfo.setStatus(WebappAuthenticator.Status.CONTINUE); return authenticationInfo; } StringTokenizer tokenizer = new StringTokenizer(requestUri, "/"); String context = tokenizer.nextToken(); if ((context == null) || (context.isEmpty())) { authenticationInfo.setStatus(WebappAuthenticator.Status.CONTINUE); } try { String bearerToken = getBearerToken(request); String resource = requestUri + ":" + requestMethod; OAuthValidationResponse oAuthValidationResponse = this.tokenValidator.validateToken(bearerToken, resource); authenticationInfo = Utils.setAuthenticationInfo(oAuthValidationResponse, authenticationInfo); } catch (AuthenticationException e) { log.error("Failed to authenticate the incoming request", e); } catch (OAuthTokenValidationException e) { log.error("Failed to authenticate the incoming request due to oauth token validation error.", e); } return authenticationInfo; }  void init(); boolean canHandle(org.apache.catalina.connector.Request request); AuthenticationInfo authenticate(org.apache.catalina.connector.Request request, Response response); String getName(); String getProperty(String name); Properties getProperties(); void setProperties(Properties properties);  }
@Test(description = "This method tests the authenticate under different parameters", dependsOnMethods = {"testInit"}) public void testAuthenticate() throws Exception { Request request = createOauthRequest(BEARER_HEADER); Assert.assertEquals(oAuthAuthenticator.authenticate(request, null).getStatus(), WebappAuthenticator.Status.CONTINUE, "Authentication status mismatched"); request = createOauthRequest(BEARER_HEADER + "abc"); org.apache.coyote.Request coyoteRequest = request.getCoyoteRequest(); Field uriMB = org.apache.coyote.Request.class.getDeclaredField("uriMB"); uriMB.setAccessible(true); MessageBytes bytes = MessageBytes.newInstance(); bytes.setString("test"); uriMB.set(coyoteRequest, bytes); request.setCoyoteRequest(coyoteRequest); Field tokenValidator = OAuthAuthenticator.class.getDeclaredField("tokenValidator"); tokenValidator.setAccessible(true); GenericObjectPool genericObjectPool = Mockito.mock(GenericObjectPool.class, Mockito.CALLS_REAL_METHODS); RemoteOAuthValidator remoteOAuthValidator = Mockito .mock(RemoteOAuthValidator.class, Mockito.CALLS_REAL_METHODS); tokenValidator.set(oAuthAuthenticator, remoteOAuthValidator); Field stubs = RemoteOAuthValidator.class.getDeclaredField("stubs"); stubs.setAccessible(true); stubs.set(remoteOAuthValidator, genericObjectPool); OAuth2TokenValidationResponseDTO oAuth2TokenValidationResponseDTO = new OAuth2TokenValidationResponseDTO(); oAuth2TokenValidationResponseDTO.setValid(true); oAuth2TokenValidationResponseDTO.setAuthorizedUser("admin@carbon.super"); OAuth2ClientApplicationDTO oAuth2ClientApplicationDTO = Mockito .mock(OAuth2ClientApplicationDTO.class, Mockito.CALLS_REAL_METHODS); Mockito.doReturn(oAuth2TokenValidationResponseDTO).when(oAuth2ClientApplicationDTO) .getAccessTokenValidationResponse(); OAuth2TokenValidationServiceStub oAuth2TokenValidationServiceStub = Mockito .mock(OAuth2TokenValidationServiceStub.class, Mockito.CALLS_REAL_METHODS); Mockito.doReturn(oAuth2ClientApplicationDTO).when(oAuth2TokenValidationServiceStub) .findOAuthConsumerIfTokenIsValid(Mockito.any()); Mockito.doReturn(oAuth2TokenValidationServiceStub).when(genericObjectPool).borrowObject(); oAuthAuthenticator.canHandle(request); AuthenticationInfo authenticationInfo = oAuthAuthenticator.authenticate(request, null); Assert.assertEquals(authenticationInfo.getUsername(), "admin"); }