AbstractOAuth2Authenticator implements IAuthenticator { @Override public final UserIdentity authenticate(final Authenticator authenticator, final MultivaluedMap<String, String> parameters, final URI callback) { URI redirect = buildRedirect(callback); if (parameters.containsKey("error")) { throw new ThirdPartyErrorException(parameters); } String code = parameters.getFirst("code"); if (Strings.isNullOrEmpty(code)) { throw new InvalidRequestException(); } String clientId = getClientId(authenticator); String clientSecret = getClientSecret(authenticator); OAuth2IdPToken token = resolveIdPToken(clientId, clientSecret, code, redirect); if (Strings.isNullOrEmpty(token.getAccessToken())) { throw new ThirdPartyErrorException(); } OAuth2User oUser = loadUserIdentity(token); if (Strings.isNullOrEmpty(oUser.getId())) { throw new ThirdPartyErrorException(); } Application application = authenticator.getClient().getApplication(); Criteria criteria = session.createCriteria(UserIdentity.class) .createAlias("user", "u") .add(Restrictions.eq("type", authenticator.getType())) .add(Restrictions.eq("remoteId", oUser.getId())) .add(Restrictions.eq("u.application", application)) .setFirstResult(0) .setMaxResults(1); UserIdentity identity = (UserIdentity) criteria.uniqueResult(); if (identity == null) { User u = new User(); u.setApplication(application); u.setRole(application.getDefaultRole()); identity = new UserIdentity(); identity.setUser(u); identity.setType(authenticator.getType()); identity.setRemoteId(oUser.getId()); identity.setClaims(oUser.getClaims()); session.save(u); session.save(identity); } else { identity.getClaims().putAll(oUser.getClaims()); session.save(identity); } return identity; }  final Client getClient(); final void setClient(final Client client); final Session getSession(); final void setSession(final Session session); @Override final Response delegate(final Authenticator authenticator,
                                   final URI callback); @Override final void validate(final Authenticator authenticator); @Override final UserIdentity authenticate(final Authenticator authenticator,
                                           final MultivaluedMap<String, String>
                                                   parameters,
                                           final URI callback); static final GenericType<HashMap<String, String>> MAP_TYPE; static final String CLIENT_ID_KEY; static final String CLIENT_SECRET_KEY; }
@Test public void testAuthenticate() { OAuth2IdPToken result = new OAuth2IdPToken(); result.setAccessToken("facebook_access_token"); doReturn(result).when(postResponse).readEntity(OAuth2IdPToken.class); OAuth2User testUser = new OAuth2User(); testUser.setId(RandomStringUtils.randomAlphanumeric(10)); testUser.getClaims().put("name", "Some Random Name"); testUser.getClaims().put("email", "lol@example.com"); doReturn(testUser).when(getResponse) .readEntity(OAuth2User.class); MultivaluedStringMap params = new MultivaluedStringMap(); params.putSingle("code", "valid_code"); UserIdentity identity = authenticator.authenticate(config, params, validCallback); assertEquals(testUser.getId(), identity.getRemoteId()); assertEquals("Some Random Name", testUser.getClaims().get("name")); assertEquals("lol@example.com", testUser.getClaims().get("email")); }
@Test(expected = ThirdPartyErrorException.class) public void testAuthenticateWithRemoteError() { MultivaluedStringMap params = new MultivaluedStringMap(); params.putSingle("error", "test"); params.putSingle("error_description", "description"); authenticator.authenticate(config, params, validCallback); }
@Test(expected = InvalidRequestException.class) public void testAuthenticateWithNoAuthCode() { MultivaluedStringMap params = new MultivaluedStringMap(); authenticator.authenticate(config, params, validCallback); }
@Test(expected = ThirdPartyErrorException.class) public void testAuthenticateWithTokenError() { Map<String, String> response = new HashMap<>(); response.put("error", "test"); response.put("error_description", "description"); doReturn(Status.BAD_REQUEST).when(postResponse).getStatusInfo(); doReturn(response).when(postResponse) .readEntity(AbstractOAuth2Authenticator.MAP_TYPE); MultivaluedStringMap params = new MultivaluedStringMap(); params.putSingle("code", "valid_code"); authenticator.authenticate(config, params, validCallback); }
@Test(expected = ThirdPartyErrorException.class) public void testAuthenticateWithUnparseableToken() { doThrow(ProcessingException.class) .when(postResponse).readEntity(OAuth2IdPToken.class); MultivaluedStringMap params = new MultivaluedStringMap(); params.putSingle("code", "valid_code"); authenticator.authenticate(config, params, validCallback); }
@Test(expected = ThirdPartyErrorException.class) public void testAuthenticateWithNoTokenResponse() { OAuth2IdPToken result = new OAuth2IdPToken(); doReturn(result).when(postResponse).readEntity(OAuth2IdPToken.class); MultivaluedStringMap params = new MultivaluedStringMap(); params.putSingle("code", "valid_code"); authenticator.authenticate(config, params, validCallback); }
@Test(expected = Exception.class) public void testAuthenticateErrorOnTokenClose() { OAuth2IdPToken idPToken = new OAuth2IdPToken(); idPToken.setAccessToken("facebook_access_token"); doReturn(idPToken).when(postResponse) .readEntity(OAuth2IdPToken.class); doThrow(Exception.class).when(postResponse).close(); MultivaluedStringMap params = new MultivaluedStringMap(); params.putSingle("code", "valid_code"); authenticator.authenticate(config, params, validCallback); }
@Test(expected = ThirdPartyErrorException.class) public void testAuthenticateWithUserError() { OAuth2IdPToken result = new OAuth2IdPToken(); result.setAccessToken("facebook_access_token"); doReturn(result).when(postResponse).readEntity(OAuth2IdPToken.class); Map<String, String> response = new HashMap<>(); response.put("error", "test"); response.put("error_description", "description"); doReturn(Status.BAD_REQUEST).when(getResponse).getStatusInfo(); doReturn(response).when(getResponse) .readEntity(AbstractOAuth2Authenticator.MAP_TYPE); MultivaluedStringMap params = new MultivaluedStringMap(); params.putSingle("code", "valid_code"); authenticator.authenticate(config, params, validCallback); }
@Test(expected = ThirdPartyErrorException.class) public void testAuthenticateWithUnparseableUser() { OAuth2IdPToken result = new OAuth2IdPToken(); result.setAccessToken("facebook_access_token"); doReturn(result).when(postResponse).readEntity(OAuth2IdPToken.class); doThrow(ProcessingException.class) .when(getResponse).readEntity(OAuth2User.class); MultivaluedStringMap params = new MultivaluedStringMap(); params.putSingle("code", "valid_code"); authenticator.authenticate(config, params, validCallback); }
@Test(expected = ThirdPartyErrorException.class) public void testAuthenticateWithNoUserResponse() { OAuth2IdPToken idPToken = new OAuth2IdPToken(); idPToken.setAccessToken("facebook_access_token"); doReturn(idPToken).when(postResponse) .readEntity(OAuth2IdPToken.class); OAuth2User result = new OAuth2User(); doReturn(result).when(getResponse).readEntity(OAuth2User.class); MultivaluedStringMap params = new MultivaluedStringMap(); params.putSingle("code", "valid_code"); authenticator.authenticate(config, params, validCallback); }
@Test(expected = Exception.class) public void testAuthenticateErrorOnClose() { OAuth2IdPToken idPToken = new OAuth2IdPToken(); idPToken.setAccessToken("facebook_access_token"); doReturn(idPToken).when(postResponse) .readEntity(OAuth2IdPToken.class); OAuth2User testUser = new OAuth2User(); testUser.setId(RandomStringUtils.randomAlphanumeric(10)); testUser.getClaims().put("name", "Some Random Name"); testUser.getClaims().put("email", "lol@example.com"); doReturn(testUser).when(getResponse) .readEntity(OAuth2User.class); doThrow(Exception.class).when(getResponse).close(); MultivaluedStringMap params = new MultivaluedStringMap(); params.putSingle("code", "valid_code"); authenticator.authenticate(config, params, validCallback); }
@Test public void testAuthenticateUpdateExistingUser() { ApplicationContext testContext = context.getBuilder() .user() .identity("remote_identity") .build(); assertEquals(0, testContext.getUserIdentity().getClaims().size()); OAuth2IdPToken result = new OAuth2IdPToken(); result.setAccessToken("facebook_access_token"); doReturn(result).when(postResponse).readEntity(OAuth2IdPToken.class); OAuth2User testUser = new OAuth2User(); testUser.setId("remote_identity"); testUser.getClaims().put("name", "Some Random Name"); testUser.getClaims().put("email", "lol@example.com"); doReturn(testUser).when(getResponse) .readEntity(OAuth2User.class); MultivaluedStringMap params = new MultivaluedStringMap(); params.putSingle("code", "valid_code"); UserIdentity identity = authenticator.authenticate(config, params, validCallback); assertEquals(identity, testContext.getUserIdentity()); assertEquals("lol@example.com", identity.getClaims().get("email")); assertEquals("Some Random Name", identity.getClaims().get("name")); }