OAuthAuthenticateValidatorCallbackHandler extends OAuthAuthenticateCallbackHandler<OAuthBearerValidatorCallback> { protected void handleCallback(OAuthBearerValidatorCallback callback) throws IOException { log.debug("Starting to handle OAuth bearer token validation callback."); log.debug("Validate method parameters."); Objects.requireNonNull(callback); String accessToken = callback.tokenValue(); if (accessToken == null) { String errMsg = "Callback missing required token value."; log.error(errMsg); throw new IllegalArgumentException(errMsg); } log.debug("Validate access token."); OAuthBearerTokenJwt token = this.getOauthService().validateAccessToken(accessToken); if (token == null) { log.info("The access token is not valid or has expired."); } else { log.info("The access token is valid, set token on the callback."); } callback.token(token); log.debug("Finished handling OAuth bearer token validation callback."); } OAuthAuthenticateValidatorCallbackHandler();   }
@Test public void handleCallback_SuccessfulToken() throws IOException { OAuthAuthenticateValidatorCallbackHandler loginCallbackHandler = Mockito.spy(new OAuthAuthenticateValidatorCallbackHandler()); OAuthServiceImpl oauthServiceImplSpy = Mockito.spy(new OAuthServiceImpl()); Map<String, Object> response = new HashMap<>(); response.put("active", true); response.put("jti", ""); response.put("iat", 1); response.put("exp", 1); OAuthBearerTokenJwt jwt = new OAuthBearerTokenJwt(response, "test"); Mockito.doReturn(oauthServiceImplSpy).when(loginCallbackHandler).getOauthService(); Mockito.doReturn(jwt).when(oauthServiceImplSpy).validateAccessToken("test"); OAuthBearerValidatorCallback oauthBearerTokenCallback = new OAuthBearerValidatorCallback("test"); loginCallbackHandler.handleCallback(oauthBearerTokenCallback); assertEquals(jwt, oauthBearerTokenCallback.token()); }
@Test(expected = NullPointerException.class) public void handleCallback_NullToken() throws IOException { OAuthAuthenticateValidatorCallbackHandler loginCallbackHandler = Mockito.spy(new OAuthAuthenticateValidatorCallbackHandler()); OAuthServiceImpl oauthServiceImplSpy = Mockito.spy(new OAuthServiceImpl()); Map<String, Object> response = new HashMap<>(); response.put("active", true); response.put("jti", ""); response.put("iat", 1); response.put("exp", 1); Mockito.doReturn(oauthServiceImplSpy).when(loginCallbackHandler).getOauthService(); Mockito.doReturn(null).when(oauthServiceImplSpy).validateAccessToken("test"); OAuthBearerValidatorCallback oauthBearerTokenCallback = new OAuthBearerValidatorCallback("test"); loginCallbackHandler.handleCallback(oauthBearerTokenCallback); }
@Test(expected = IllegalArgumentException.class) public void handleCallback_NullParamToken() throws IOException { OAuthAuthenticateValidatorCallbackHandler loginCallbackHandler = Mockito.spy(new OAuthAuthenticateValidatorCallbackHandler()); OAuthBearerValidatorCallback oauthBearerTokenCallback = Mockito.spy(new OAuthBearerValidatorCallback("test")); Mockito.doReturn(null).when(oauthBearerTokenCallback).tokenValue(); loginCallbackHandler.handleCallback(oauthBearerTokenCallback); }