@andres - Here are a few python functions that will return the user UUID
from django.conf import settings
from urllib.parse import quote
import json
import requests
proxies = {
'http': 'http://127.0.0.1:8080',
'https': 'http://127.0.0.1:8080',
}
headers = {
'Accept': 'application/json',
}
def get_token():
"""
POST /auth/realms/master/protocol/openid-connect/token HTTP/1.1
Host: IPADDR:8443
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:74.0) Gecko/20100101 Firefox/74.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-type: application/x-www-form-urlencoded
Content-Length: 79
Origin: http://IPADDR:8443
Connection: close
Referer: http://IPADDR:8443/auth/admin/master/console/
username=admin&password=secret&grant_type=password&client_id=admin-cli
:return:
"""
body = 'username=%s&password=%s&grant_type=password&client_id=admin-cli' % (settings.KEYCLOAK_ADMIN_USER, quote(settings.KEYCLOAK_ADMIN_PASSWD))
headers['Content-type'] = 'application/x-www-form-urlencoded'
r = requests.post(
url='https://%s:%s/auth/realms/master/protocol/openid-connect/token' % (settings.KEYCLOAK_HOST, settings.KEYCLOAK_PORT),
data=body,
headers=headers,
# proxies=proxies,
)
return r.json()
def create_user(username, email, first_name, last_name):
"""
POST /auth/admin/realms/trolleye/users HTTP/1.1
Host: IPADDR:8443
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:74.0) Gecko/20100101 Firefox/74.0
Accept: application/json, text/plain, */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: application/json;charset=utf-8
Authorization: Bearer TOKEN
Content-Length: 181
Origin: http://IPADDR:8443
Connection: close
Referer: http://IPADDR:8443/auth/admin/master/console/
{"enabled":true,"attributes":{},"username":"bitsaboy","emailVerified":true,"email":"bob@domain.com","firstName":"Bobhadababy","lastName":"Itsaboy","requiredActions":["CONFIGURE_TOTP"]}
:return:
"""
access_token = get_token()['access_token']
body = {"enabled": True, "attributes": {},
"username": username,
"emailVerified": True,
"email": email,
"firstName": first_name,
"lastName": last_name,
"requiredActions": ["CONFIGURE_TOTP"]
}
headers['Authorization'] = 'Bearer %s' % access_token
headers['Content-type'] = 'application/json'
r = requests.post(
url='https://%s:%s/auth/admin/realms/%s/users' % (settings.KEYCLOAK_HOST, settings.KEYCLOAK_PORT, settings.CLIENT),
data=json.dumps(body),
headers=headers,
# proxies=proxies,
)
return r.headers.get('Location').split('/')[-1]
And to create the user just do this:
user_uuid = create_user('bobhadababy.itsaboy', 'bobhadababy.itsaboy@domain.com', 'Bobhadababy', 'Itsaboy')