@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); }
@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; }
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; } }
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; }  }
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); }
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; }