Search in sources :

Example 1 with IdentityBrokerException

use of org.keycloak.broker.provider.IdentityBrokerException in project keycloak by keycloak.

the class OpenshiftV4IdentityProviderTest method testHttpClientErrors.

@Test
public void testHttpClientErrors() {
    // given
    OpenshiftV4IdentityProviderConfig config = new OpenshiftV4IdentityProviderConfig(new IdentityProviderModel());
    // when
    try {
        new OpenshiftV4IdentityProvider(null, config) {

            @Override
            InputStream getOauthMetadataInputStream(KeycloakSession session, String baseUrl) {
                throw new RuntimeException("Failed : HTTP error code : 500");
            }
        };
        Assert.fail();
    } catch (IdentityBrokerException e) {
    // then
    // OK
    }
}
Also used : KeycloakSession(org.keycloak.models.KeycloakSession) IdentityBrokerException(org.keycloak.broker.provider.IdentityBrokerException) IdentityProviderModel(org.keycloak.models.IdentityProviderModel) Test(org.junit.Test)

Example 2 with IdentityBrokerException

use of org.keycloak.broker.provider.IdentityBrokerException in project keycloak by keycloak.

the class OpenshiftV4IdentityProvider method getAuthJson.

Map<String, Object> getAuthJson(KeycloakSession session, String baseUrl) {
    try {
        InputStream response = getOauthMetadataInputStream(session, baseUrl);
        Map<String, Object> map = mapMetadata(response);
        return map;
    } catch (Exception e) {
        throw new IdentityBrokerException("Could not initialize oAuth metadata", e);
    }
}
Also used : InputStream(java.io.InputStream) IdentityBrokerException(org.keycloak.broker.provider.IdentityBrokerException) IOException(java.io.IOException) IdentityBrokerException(org.keycloak.broker.provider.IdentityBrokerException)

Example 3 with IdentityBrokerException

use of org.keycloak.broker.provider.IdentityBrokerException in project keycloak by keycloak.

the class InstagramIdentityProvider method doGetFederatedIdentity.

protected BrokeredIdentityContext doGetFederatedIdentity(String accessToken) {
    try {
        // try to get the profile incl. legacy Instagram ID to allow existing users to log in
        JsonNode profile = fetchUserProfile(accessToken, true);
        // ig_id field will get deprecated in the future and eventually might stop working (returning error)
        if (!profile.has("id")) {
            logger.debugf("Could not fetch user profile from instagram. Trying without %s.", LEGACY_ID_FIELD);
            profile = fetchUserProfile(accessToken, false);
        }
        logger.debug(profile.toString());
        // it's not documented whether the new ID system can or cannot have conflicts with the legacy system, therefore
        // we're using a custom prefix just to be sure
        String id = "graph_" + getJsonProperty(profile, "id");
        String username = getJsonProperty(profile, "username");
        String legacyId = getJsonProperty(profile, LEGACY_ID_FIELD);
        BrokeredIdentityContext user = new BrokeredIdentityContext(id);
        user.setUsername(username);
        user.setIdpConfig(getConfig());
        user.setIdp(this);
        if (legacyId != null && !legacyId.isEmpty()) {
            user.setLegacyId(legacyId);
        }
        AbstractJsonUserAttributeMapper.storeUserProfileForMapper(user, profile, getConfig().getAlias());
        return user;
    } catch (Exception e) {
        throw new IdentityBrokerException("Could not obtain user profile from instagram.", e);
    }
}
Also used : IdentityBrokerException(org.keycloak.broker.provider.IdentityBrokerException) JsonNode(com.fasterxml.jackson.databind.JsonNode) BrokeredIdentityContext(org.keycloak.broker.provider.BrokeredIdentityContext) IdentityBrokerException(org.keycloak.broker.provider.IdentityBrokerException) IOException(java.io.IOException)

Example 4 with IdentityBrokerException

use of org.keycloak.broker.provider.IdentityBrokerException in project keycloak by keycloak.

the class FacebookIdentityProvider method doGetFederatedIdentity.

protected BrokeredIdentityContext doGetFederatedIdentity(String accessToken) {
    try {
        final String fetchedFields = getConfig().getFetchedFields();
        final String url = StringUtil.isNotNull(fetchedFields) ? String.join(PROFILE_URL_FIELDS_SEPARATOR, PROFILE_URL, fetchedFields) : PROFILE_URL;
        JsonNode profile = SimpleHttp.doGet(url, session).header("Authorization", "Bearer " + accessToken).asJson();
        return extractIdentityFromProfile(null, profile);
    } catch (Exception e) {
        throw new IdentityBrokerException("Could not obtain user profile from facebook.", e);
    }
}
Also used : IdentityBrokerException(org.keycloak.broker.provider.IdentityBrokerException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IdentityBrokerException(org.keycloak.broker.provider.IdentityBrokerException)

Example 5 with IdentityBrokerException

use of org.keycloak.broker.provider.IdentityBrokerException in project keycloak by keycloak.

the class GitHubIdentityProvider method searchEmail.

private String searchEmail(String accessToken) {
    try {
        ArrayNode emails = (ArrayNode) SimpleHttp.doGet(EMAIL_URL, session).header("Authorization", "Bearer " + accessToken).asJson();
        Iterator<JsonNode> loop = emails.elements();
        while (loop.hasNext()) {
            JsonNode mail = loop.next();
            if (mail.get("primary").asBoolean()) {
                return getJsonProperty(mail, "email");
            }
        }
    } catch (Exception e) {
        throw new IdentityBrokerException("Could not obtain user email from github.", e);
    }
    throw new IdentityBrokerException("Primary email from github is not found.");
}
Also used : IdentityBrokerException(org.keycloak.broker.provider.IdentityBrokerException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) IdentityBrokerException(org.keycloak.broker.provider.IdentityBrokerException)

Aggregations

IdentityBrokerException (org.keycloak.broker.provider.IdentityBrokerException)27 IOException (java.io.IOException)13 BrokeredIdentityContext (org.keycloak.broker.provider.BrokeredIdentityContext)11 JsonNode (com.fasterxml.jackson.databind.JsonNode)8 OAuthErrorException (org.keycloak.OAuthErrorException)7 NotFoundException (javax.ws.rs.NotFoundException)5 WebApplicationException (javax.ws.rs.WebApplicationException)5 ErrorResponseException (org.keycloak.services.ErrorResponseException)5 AuthenticationSessionModel (org.keycloak.sessions.AuthenticationSessionModel)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 Path (javax.ws.rs.Path)4 IdentityProvider (org.keycloak.broker.provider.IdentityProvider)4 SocialIdentityProvider (org.keycloak.broker.social.SocialIdentityProvider)4 IdentityProviderModel (org.keycloak.models.IdentityProviderModel)4 RoleModel (org.keycloak.models.RoleModel)4 JsonWebToken (org.keycloak.representations.JsonWebToken)4 ErrorPageException (org.keycloak.services.ErrorPageException)4 GET (javax.ws.rs.GET)3 NoCache (org.jboss.resteasy.annotations.cache.NoCache)3 ClientModel (org.keycloak.models.ClientModel)3