FacebookAuthenticator extends AbstractOAuth2Authenticator { @Override protected OAuth2User loadUserIdentity(final OAuth2IdPToken token) { Response r = getClient() .target(USER_ENDPOINT) .request() .header(HttpHeaders.AUTHORIZATION, HttpUtil.authHeaderBearer(token.getAccessToken())) .get(); try { if (r.getStatusInfo().getFamily().equals(Family.SUCCESSFUL)) { FacebookUserEntity fbUser = r.readEntity(FacebookUserEntity.class); if (Strings.isNullOrEmpty(fbUser.getId())) { throw new ThirdPartyErrorException(); } return fbUser.asGenericUser(); } else { Map<String, String> params = r.readEntity(MAP_TYPE); throw new ThirdPartyErrorException(params); } } catch (ProcessingException e) { throw new ThirdPartyErrorException(); } finally { r.close(); } }    }
@Test public void testLoadUser() { OAuth2IdPToken result = new OAuth2IdPToken(); result.setAccessToken("facebook_access_token"); FacebookUserEntity user = new FacebookUserEntity(); user.setId("test_id"); user.setName("test name"); doReturn(user).when(getResponse).readEntity(FacebookUserEntity.class); OAuth2User returnedUser = fbAuth.loadUserIdentity(result); assertEquals(returnedUser.getId(), user.getId()); assertEquals("test name", returnedUser.getClaims().get("name")); }
@Test(expected = ThirdPartyErrorException.class) public void testLoadUserWithRemoteError() { OAuth2IdPToken result = new OAuth2IdPToken(); result.setAccessToken("facebook_access_token"); 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(FacebookAuthenticator.MAP_TYPE); fbAuth.loadUserIdentity(result); }
@Test(expected = ThirdPartyErrorException.class) public void testLoadUserUnparseable() { OAuth2IdPToken result = new OAuth2IdPToken(); result.setAccessToken("facebook_access_token"); doThrow(ProcessingException.class) .when(getResponse).readEntity(FacebookUserEntity.class); fbAuth.loadUserIdentity(result); }
@Test(expected = ThirdPartyErrorException.class) public void testLoadUserNoResponse() { OAuth2IdPToken idPToken = new OAuth2IdPToken(); idPToken.setAccessToken("facebook_access_token"); FacebookUserEntity result = new FacebookUserEntity(); doReturn(result).when(getResponse).readEntity(FacebookUserEntity.class); fbAuth.loadUserIdentity(idPToken); }
@Test(expected = Exception.class) public void testLoadUserErrorOnClose() { OAuth2IdPToken idPToken = new OAuth2IdPToken(); idPToken.setAccessToken("facebook_access_token"); FacebookUserEntity testUser = new FacebookUserEntity(); testUser.setId(RandomStringUtils.randomAlphanumeric(10)); testUser.setName("Some Random Name"); testUser.setEmail("lol@example.com"); doReturn(testUser).when(getResponse) .readEntity(FacebookUserEntity.class); doThrow(Exception.class).when(getResponse).close(); MultivaluedStringMap params = new MultivaluedStringMap(); params.putSingle("code", "valid_code"); fbAuth.loadUserIdentity(idPToken); }