Search in sources :

Example 6 with BrokeredIdentityContext

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

the class StackoverflowIdentityProvider method extractIdentityFromProfile.

@Override
protected BrokeredIdentityContext extractIdentityFromProfile(EventBuilder event, JsonNode node) {
    JsonNode profile = node.get("items").get(0);
    BrokeredIdentityContext user = new BrokeredIdentityContext(getJsonProperty(profile, "user_id"));
    String username = extractUsernameFromProfileURL(getJsonProperty(profile, "link"));
    user.setUsername(username);
    user.setName(unescapeHtml3(getJsonProperty(profile, "display_name")));
    // email is not provided
    // user.setEmail(getJsonProperty(profile, "email"));
    user.setIdpConfig(getConfig());
    user.setIdp(this);
    AbstractJsonUserAttributeMapper.storeUserProfileForMapper(user, profile, getConfig().getAlias());
    return user;
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) BrokeredIdentityContext(org.keycloak.broker.provider.BrokeredIdentityContext)

Example 7 with BrokeredIdentityContext

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

the class GitLabIdentityProvider method gitlabExtractFromProfile.

private BrokeredIdentityContext gitlabExtractFromProfile(JsonNode profile) {
    String id = getJsonProperty(profile, "id");
    BrokeredIdentityContext identity = new BrokeredIdentityContext(id);
    String name = getJsonProperty(profile, "name");
    String preferredUsername = getJsonProperty(profile, "username");
    String email = getJsonProperty(profile, "email");
    AbstractJsonUserAttributeMapper.storeUserProfileForMapper(identity, profile, getConfig().getAlias());
    identity.setId(id);
    identity.setName(name);
    identity.setEmail(email);
    identity.setBrokerUserId(getConfig().getAlias() + "." + id);
    if (preferredUsername == null) {
        preferredUsername = email;
    }
    if (preferredUsername == null) {
        preferredUsername = id;
    }
    identity.setUsername(preferredUsername);
    return identity;
}
Also used : BrokeredIdentityContext(org.keycloak.broker.provider.BrokeredIdentityContext)

Example 8 with BrokeredIdentityContext

use of org.keycloak.broker.provider.BrokeredIdentityContext 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 9 with BrokeredIdentityContext

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

the class MicrosoftIdentityProvider method extractIdentityFromProfile.

@Override
protected BrokeredIdentityContext extractIdentityFromProfile(EventBuilder event, JsonNode profile) {
    String id = getJsonProperty(profile, "id");
    BrokeredIdentityContext user = new BrokeredIdentityContext(id);
    String email = getJsonProperty(profile, "mail");
    if (email == null && profile.has("userPrincipalName")) {
        String username = getJsonProperty(profile, "userPrincipalName");
        if (Validation.isEmailValid(username)) {
            email = username;
        }
    }
    user.setUsername(email != null ? email : id);
    user.setFirstName(getJsonProperty(profile, "givenName"));
    user.setLastName(getJsonProperty(profile, "surname"));
    if (email != null)
        user.setEmail(email);
    user.setIdpConfig(getConfig());
    user.setIdp(this);
    AbstractJsonUserAttributeMapper.storeUserProfileForMapper(user, profile, getConfig().getAlias());
    return user;
}
Also used : BrokeredIdentityContext(org.keycloak.broker.provider.BrokeredIdentityContext)

Example 10 with BrokeredIdentityContext

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

the class BitbucketIdentityProvider method extractUserInfo.

private BrokeredIdentityContext extractUserInfo(String subjectToken, JsonNode profile) {
    BrokeredIdentityContext user = new BrokeredIdentityContext(getJsonProperty(profile, "account_id"));
    String username = getJsonProperty(profile, "username");
    user.setUsername(username);
    user.setName(getJsonProperty(profile, "display_name"));
    user.setIdpConfig(getConfig());
    user.setIdp(this);
    AbstractJsonUserAttributeMapper.storeUserProfileForMapper(user, profile, getConfig().getAlias());
    try {
        JsonNode emails = SimpleHttp.doGet(USER_EMAIL_URL, session).header("Authorization", "Bearer " + subjectToken).asJson();
        // {"pagelen":10,"values":[{"is_primary":true,"is_confirmed":true,"type":"email","email":"bburke@redhat.com","links":{"self":{"href":"https://api.bitbucket.org/2.0/user/emails/bburke@redhat.com"}}}],"page":1,"size":1}
        JsonNode emailJson = emails.get("values");
        if (emailJson != null) {
            if (emailJson.isArray()) {
                emailJson = emailJson.get(0);
            }
            if (emailJson != null && "email".equals(getJsonProperty(emailJson, "type"))) {
                user.setEmail(getJsonProperty(emailJson, "email"));
            }
        }
    } catch (Exception ignore) {
        logger.debug("failed to get email from BitBucket", ignore);
    }
    return user;
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) BrokeredIdentityContext(org.keycloak.broker.provider.BrokeredIdentityContext) IOException(java.io.IOException) IdentityBrokerException(org.keycloak.broker.provider.IdentityBrokerException) OAuthErrorException(org.keycloak.OAuthErrorException) ErrorResponseException(org.keycloak.services.ErrorResponseException)

Aggregations

BrokeredIdentityContext (org.keycloak.broker.provider.BrokeredIdentityContext)40 IOException (java.io.IOException)12 IdentityBrokerException (org.keycloak.broker.provider.IdentityBrokerException)12 JsonNode (com.fasterxml.jackson.databind.JsonNode)11 SerializedBrokeredIdentityContext (org.keycloak.authentication.authenticators.broker.util.SerializedBrokeredIdentityContext)6 ErrorResponseException (org.keycloak.services.ErrorResponseException)6 OAuthErrorException (org.keycloak.OAuthErrorException)5 AuthenticationSessionModel (org.keycloak.sessions.AuthenticationSessionModel)5 Map (java.util.Map)3 AuthenticationFlowException (org.keycloak.authentication.AuthenticationFlowException)3 SimpleHttp (org.keycloak.broker.provider.util.SimpleHttp)3 HashMap (java.util.HashMap)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 MediaType (javax.ws.rs.core.MediaType)2 Response (javax.ws.rs.core.Response)2 Test (org.junit.Test)2 IdentityProvider (org.keycloak.broker.provider.IdentityProvider)2 JWSInput (org.keycloak.jose.jws.JWSInput)2