Skip to content

Commit

Permalink
Added fields to keycloak export (#4360)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysyngsun authored Oct 28, 2024
1 parent 8102097 commit 039c540
Showing 1 changed file with 23 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from django.contrib.auth import get_user_model
import requests
import calendar
import json
import sys

from django.conf import settings
from django.utils.http import urlencode
import calendar
from django.contrib.auth import get_user_model
from django.db.models import Q

from django.core.management import BaseCommand, CommandError
from django.utils.http import urlencode
import requests

from keycloak_user_export.models import UserExportToKeycloak

User = get_user_model()
Expand Down Expand Up @@ -118,6 +120,15 @@ def _get_access_token(
headers = {"Content-Type": "application/x-www-form-urlencoded"}

response = requests.request("POST", url, headers=headers, data=payload)

if response.status_code != 200:
self.stderr.write(
self.style.ERROR(
f"Got error requesting access token: {response.json()}"
)
)
sys.exit(1)

return response.json()["access_token"]

def _generate_keycloak_user_payload(self, user, keycloak_group_path):
Expand All @@ -132,12 +143,9 @@ def _generate_keycloak_user_payload(self, user, keycloak_group_path):
Returns:
dict: user representation for use with the Keycloak partialImport Admin REST API endpoint.
"""
first_name, last_name = self._get_user_names(user)
user_keycloak_payload = {
"createdTimestamp": calendar.timegm(user.date_joined.timetuple()),
"username": user.email,
"firstName": first_name,
"lastName": last_name,
"enabled": True,
"totp": False,
"emailVerified": True,
Expand All @@ -147,31 +155,16 @@ def _generate_keycloak_user_payload(self, user, keycloak_group_path):
"requiredActions": [],
"realmRoles": ["default-roles-master"],
"notBefore": 0,
"groups": [keycloak_group_path],
"groups": [keycloak_group_path] if keycloak_group_path else [],
"attributes": {
"fullName": user.profile.name,
"emailOptIn": 1 if user.profile.email_optin else 0,
}
if hasattr(user, "profile")
else {},
}
return user_keycloak_payload

def _get_user_names(self, user):
"""
Return the first and last name of a user.
If there is only one name in the user's profile this returns empty strings.
Args:
user (models.User): A Django User model record.
Returns:
(str, str): the first and last names of the user
"""
profile = getattr(user, "profile", None)
name = profile.name if profile is not None else ""
names = name.split(maxsplit=1)

if len(names) == 2:
return tuple(names)

return "", ""

def _verify_environment_variables_configured(self):
"""
Verify that KEYCLOAK_BASE_URL and KEYCLOAK_REALM_NAME are configured as environment
Expand Down

0 comments on commit 039c540

Please sign in to comment.