OAuthAuthenticateLoginCallbackHandler extends OAuthAuthenticateCallbackHandler<OAuthBearerTokenCallback> { protected void handleCallback(OAuthBearerTokenCallback oauthBearerTokenCallback) throws IOException { log.debug("Starting to handle OAuth bearer token login callback."); log.debug("Validate method parameters."); Objects.requireNonNull(oauthBearerTokenCallback); if (oauthBearerTokenCallback.token() != null) { String errMsg = "OAuth bearer token callback already had a token."; log.error(errMsg); throw new IllegalArgumentException(errMsg); } log.debug("Acquire access token for OAuth bearer token callback."); OAuthBearerTokenJwt token = this.getOauthService().requestAccessToken(); log.debug("Check to see if an access token as returned."); if (token == null) { String errMsg = "Access token was not returned."; log.error(errMsg); throw new IllegalArgumentException(errMsg); } log.info("Access token was returned, set the access token on the OAuth bearer token callback."); oauthBearerTokenCallback.token(token); log.debug("Finished handling OAuth bearer token login callback."); } OAuthAuthenticateLoginCallbackHandler();   }
@Test(expected = IllegalArgumentException.class) public void handleCallback_TokenResultNull() throws IOException { OAuthAuthenticateLoginCallbackHandler loginCallbackHandler = Mockito.spy(new OAuthAuthenticateLoginCallbackHandler()); 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).requestAccessToken(); OAuthBearerTokenCallback oauthBearerTokenCallback = new OAuthBearerTokenCallback(); loginCallbackHandler.handleCallback(oauthBearerTokenCallback); }
@Test public void handleCallback_SuccessfulToken() throws IOException { OAuthAuthenticateLoginCallbackHandler loginCallbackHandler = Mockito.spy(new OAuthAuthenticateLoginCallbackHandler()); 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).requestAccessToken(); OAuthBearerTokenCallback oauthBearerTokenCallback = new OAuthBearerTokenCallback(); loginCallbackHandler.handleCallback(oauthBearerTokenCallback); assertEquals(jwt, oauthBearerTokenCallback.token()); }
@Test(expected = IllegalArgumentException.class) public void handleCallback_TokenParamNotNull() throws IOException { OAuthAuthenticateLoginCallbackHandler loginCallbackHandler = Mockito.spy(new OAuthAuthenticateLoginCallbackHandler()); 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"); OAuthBearerTokenCallback oauthBearerTokenCallback = new OAuthBearerTokenCallback(); oauthBearerTokenCallback.token(jwt); loginCallbackHandler.handleCallback(oauthBearerTokenCallback); }