From b98e1533774f1d4fb9145e7904d98bd45cd0609b Mon Sep 17 00:00:00 2001 From: giordyb Date: Mon, 3 Aug 2020 09:59:43 +0200 Subject: [PATCH 1/4] Update keycloak_admin.py added capabilities to add and get user's social login --- keycloak/keycloak_admin.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/keycloak/keycloak_admin.py b/keycloak/keycloak_admin.py index 6293d5b..232c5d3 100644 --- a/keycloak/keycloak_admin.py +++ b/keycloak/keycloak_admin.py @@ -42,7 +42,8 @@ from .urls_patterns import URL_ADMIN_SERVER_INFO, URL_ADMIN_CLIENT_AUTHZ_RESOURC URL_ADMIN_GROUP_MEMBERS, URL_ADMIN_USER_STORAGE, URL_ADMIN_GROUP_PERMISSIONS, URL_ADMIN_IDPS, \ URL_ADMIN_USER_CLIENT_ROLES_AVAILABLE, URL_ADMIN_USERS, URL_ADMIN_CLIENT_SCOPES, \ URL_ADMIN_CLIENT_SCOPES_ADD_MAPPER, URL_ADMIN_CLIENT_SCOPE, URL_ADMIN_CLIENT_SECRETS, \ - URL_ADMIN_USER_REALM_ROLES, URL_ADMIN_REALM, URL_ADMIN_COMPONENTS, URL_ADMIN_COMPONENT, URL_ADMIN_KEYS + URL_ADMIN_USER_REALM_ROLES, URL_ADMIN_REALM, URL_ADMIN_COMPONENTS, URL_ADMIN_COMPONENT, URL_ADMIN_KEYS \ + URL_ADMIN_USER_FEDERATED_IDENTITY, URL_ADMIN_USER_FEDERATED_IDENTITIES class KeycloakAdmin: @@ -454,6 +455,29 @@ class KeycloakAdmin: data_raw = self.raw_get(URL_ADMIN_USER_CONSENTS.format(**params_path)) return raise_error_from_response(data_raw, KeycloakGetError) + def get_user_social_logins(self, user_id): + """ + Returns a list of federated identities/social logins of which the user has been associated with + :param user_id: User id + :return: federated identities list + """ + params_path = {"realm-name": self.realm_name, "id": user_id} + data_raw = self.raw_get(URL_ADMIN_USER_FEDERATED_IDENTITIES.format(**params_path)) + return raise_error_from_response(data_raw, KeycloakGetError) + + def add_user_social_login(self, user_id, provider_id, provider_userid, provider_username): + + """ + Add a federated identity / social login provider to the user + :param user_id: User id + :param provider: Social login provider id + :param realm: realm name + :return: + """ + payload = {"identityProvider": provider_id, "userId": provider_userid, "userName": provider_username} + params_path = {"realm-name": self.realm_name, "id": user_id, "provider": provider_id} + data_raw = self.raw_post(URL_ADMIN_USER_FEDERATED_IDENTITY.format(**params_path), data=json.dumps(payload)) + def send_update_account(self, user_id, payload, client_id=None, lifespan=None, redirect_uri=None): """ Send an update account email to the user. An email contains a From 707637bfd6e68c7eeec40d693855497a6f08848a Mon Sep 17 00:00:00 2001 From: giordyb Date: Mon, 3 Aug 2020 10:00:46 +0200 Subject: [PATCH 2/4] Update urls_patterns.py added url patterns for social login --- keycloak/urls_patterns.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/keycloak/urls_patterns.py b/keycloak/urls_patterns.py index f08a422..bb22af7 100644 --- a/keycloak/urls_patterns.py +++ b/keycloak/urls_patterns.py @@ -86,3 +86,6 @@ URL_ADMIN_FLOWS_EXECUTIONS = "admin/realms/{realm-name}/authentication/flows/{fl URL_ADMIN_COMPONENTS = "admin/realms/{realm-name}/components" URL_ADMIN_COMPONENT = "admin/realms/{realm-name}/components/{component-id}" URL_ADMIN_KEYS = "admin/realms/{realm-name}/keys" + +URL_ADMIN_USER_FEDERATED_IDENTITIES = "admin/realms/{realm-name}/users/{id}/federated-identity" +URL_ADMIN_USER_FEDERATED_IDENTITY = "admin/realms/{realm-name}/users/{id}/federated-identity/{provider}" From 3838ed036ba2e13dc6f5774fb7bded77bcbd015d Mon Sep 17 00:00:00 2001 From: biagio Date: Mon, 3 Aug 2020 10:08:43 +0200 Subject: [PATCH 3/4] fix comma --- keycloak/keycloak_admin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/keycloak/keycloak_admin.py b/keycloak/keycloak_admin.py index 232c5d3..86e321f 100644 --- a/keycloak/keycloak_admin.py +++ b/keycloak/keycloak_admin.py @@ -42,7 +42,7 @@ from .urls_patterns import URL_ADMIN_SERVER_INFO, URL_ADMIN_CLIENT_AUTHZ_RESOURC URL_ADMIN_GROUP_MEMBERS, URL_ADMIN_USER_STORAGE, URL_ADMIN_GROUP_PERMISSIONS, URL_ADMIN_IDPS, \ URL_ADMIN_USER_CLIENT_ROLES_AVAILABLE, URL_ADMIN_USERS, URL_ADMIN_CLIENT_SCOPES, \ URL_ADMIN_CLIENT_SCOPES_ADD_MAPPER, URL_ADMIN_CLIENT_SCOPE, URL_ADMIN_CLIENT_SECRETS, \ - URL_ADMIN_USER_REALM_ROLES, URL_ADMIN_REALM, URL_ADMIN_COMPONENTS, URL_ADMIN_COMPONENT, URL_ADMIN_KEYS \ + URL_ADMIN_USER_REALM_ROLES, URL_ADMIN_REALM, URL_ADMIN_COMPONENTS, URL_ADMIN_COMPONENT, URL_ADMIN_KEYS, \ URL_ADMIN_USER_FEDERATED_IDENTITY, URL_ADMIN_USER_FEDERATED_IDENTITIES From 03522bb5904d48933f115b878a0c2d3c4d9d4e94 Mon Sep 17 00:00:00 2001 From: biagio Date: Mon, 3 Aug 2020 10:15:08 +0200 Subject: [PATCH 4/4] added description --- keycloak/keycloak_admin.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/keycloak/keycloak_admin.py b/keycloak/keycloak_admin.py index 86e321f..58e01e2 100644 --- a/keycloak/keycloak_admin.py +++ b/keycloak/keycloak_admin.py @@ -470,8 +470,9 @@ class KeycloakAdmin: """ Add a federated identity / social login provider to the user :param user_id: User id - :param provider: Social login provider id - :param realm: realm name + :param provider_id: Social login provider id + :param provider_userid: userid specified by the provider + :param provider_username: username specified by the provider :return: """ payload = {"identityProvider": provider_id, "userId": provider_userid, "userName": provider_username}