BSTAuthenticator implements WebappAuthenticator { @Override public AuthenticationInfo authenticate(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) || ("".equals(context))) { authenticationInfo.setStatus(WebappAuthenticator.Status.CONTINUE); } try { String bearerToken = new String(Base64.getDecoder().decode(request.getAttribute("BST").toString())); 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(); @Override boolean canHandle(Request request); @Override AuthenticationInfo authenticate(Request request, Response response); @Override String getName(); @Override void setProperties(Properties properties); @Override Properties getProperties(); @Override String getProperty(String name);  }
@Test(description = "This method tests the authenticate method of BST Authenticator when only minimal information" + " is provided") public void testAuthenticateWithMinimalConditions() throws NoSuchFieldException, IllegalAccessException { Request request = new Request(); org.apache.coyote.Request coyoteRequest = new org.apache.coyote.Request(); request.setCoyoteRequest(coyoteRequest); AuthenticationInfo authenticationInfo = bstAuthenticator.authenticate(request, null); Assert.assertEquals(authenticationInfo.getStatus(), WebappAuthenticator.Status.CONTINUE, "Authentication status of authentication info is wrong"); }
@Test(description = "This method tests the authenticate method of BST Authenticator when all the relevant " + "details", dependsOnMethods = "testInitWithRemote") public void testAuthenticate() throws NoSuchFieldException, IllegalAccessException, IOException { Request request = createSoapRequest("CorrectBST.xml"); 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); bstAuthenticator.canHandle(request); AuthenticationInfo authenticationInfo = bstAuthenticator.authenticate(request, null); Assert.assertEquals(authenticationInfo.getStatus(), WebappAuthenticator.Status.CONTINUE, "Authentication status of authentication info is wrong"); Assert.assertEquals(authenticationInfo.getUsername(), "admin", "User name in the authentication info is different than original user"); OAuth2TokenValidationResponseDTO unAuthorizedValidationRespose = new OAuth2TokenValidationResponseDTO(); unAuthorizedValidationRespose.setValid(false); unAuthorizedValidationRespose.setErrorMsg("User is not authorized"); Mockito.doReturn(oAuth2ClientApplicationDTO).when(oAuth2TokenValidationService) .findOAuthConsumerIfTokenIsValid(Mockito.any()); oAuth2ClientApplicationDTO.setAccessTokenValidationResponse(unAuthorizedValidationRespose); AuthenticatorFrameworkDataHolder.getInstance().setOAuth2TokenValidationService(oAuth2TokenValidationService); authenticationInfo = bstAuthenticator.authenticate(request, null); Assert.assertEquals(authenticationInfo.getStatus(), WebappAuthenticator.Status.FAILURE, "Un-authorized user got authenticated with BST"); }