Browse Source

Merged in surbas/python-keycloak (pull request #3)

Allow passing a payload to create_user again, and minor clean up.

Approved-by: Marcos Pereira <marcospereira.mpj@gmail.com>
hotfix/merge
surbas 7 years ago
committed by Marcos Pereira
parent
commit
1e34394a75
  1. 4
      keycloak/exceptions.py
  2. 25
      keycloak/keycloak_admin.py
  3. 3
      requirements.txt

4
keycloak/exceptions.py

@ -16,7 +16,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import requests import requests
from simplejson import JSONDecodeError
class KeycloakError(Exception): class KeycloakError(Exception):
@ -73,10 +72,9 @@ def raise_error_from_response(response, error, expected_code=200):
if expected_code == response.status_code: if expected_code == response.status_code:
if expected_code == requests.codes.no_content: if expected_code == requests.codes.no_content:
return {} return {}
try: try:
return response.json() return response.json()
except JSONDecodeError as e:
except ValueError:
return response.content return response.content
try: try:

25
keycloak/keycloak_admin.py

@ -106,7 +106,7 @@ class KeycloakAdmin:
data_raw = self.connection.raw_get(URL_ADMIN_USERS.format(**params_path), **query) data_raw = self.connection.raw_get(URL_ADMIN_USERS.format(**params_path), **query)
return raise_error_from_response(data_raw, KeycloakGetError) return raise_error_from_response(data_raw, KeycloakGetError)
def create_user(self, username, email='', firstName='', lastName='', emailVerified=False, enabled=True):
def create_user(self, payload):
""" """
Create a new user Username must be unique Create a new user Username must be unique
@ -115,17 +115,11 @@ class KeycloakAdmin:
:param payload: UserRepresentation :param payload: UserRepresentation
:return: UserRepresentation
""" """
data={}
data["username"]=username
data["email"]=email
data["firstName"]=firstName
data["lastName"]=lastName
data["emailVerified"]=emailVerified
data["enabled"]=enabled
params_path = {"realm-name": self.realm_name} params_path = {"realm-name": self.realm_name}
data_raw = self.connection.raw_post(URL_ADMIN_USERS.format(**params_path), data_raw = self.connection.raw_post(URL_ADMIN_USERS.format(**params_path),
data=json.dumps(data))
data=json.dumps(payload))
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=201) return raise_error_from_response(data_raw, KeycloakGetError, expected_code=201)
def users_count(self): def users_count(self):
@ -138,7 +132,6 @@ class KeycloakAdmin:
data_raw = self.connection.raw_get(URL_ADMIN_USERS_COUNT.format(**params_path)) data_raw = self.connection.raw_get(URL_ADMIN_USERS_COUNT.format(**params_path))
return raise_error_from_response(data_raw, KeycloakGetError) return raise_error_from_response(data_raw, KeycloakGetError)
def get_user_id(self, username): def get_user_id(self, username):
""" """
Get internal keycloak user id from username Get internal keycloak user id from username
@ -175,7 +168,7 @@ class KeycloakAdmin:
data_raw = self.connection.raw_get(URL_ADMIN_USER.format(**params_path)) data_raw = self.connection.raw_get(URL_ADMIN_USER.format(**params_path))
return raise_error_from_response(data_raw, KeycloakGetError) return raise_error_from_response(data_raw, KeycloakGetError)
def update_user(self, user_id, username, email='', firstName='', lastName='', emailVerified=False, enabled=True):
def update_user(self, user_id, payload):
""" """
Update the user Update the user
@ -184,17 +177,9 @@ class KeycloakAdmin:
:return: Http response :return: Http response
""" """
data={}
data["username"]=username
data["email"]=email
data["firstName"]=firstName
data["lastName"]=lastName
data["emailVerified"]=emailVerified
data["enabled"]=enabled
params_path = {"realm-name": self.realm_name}
params_path = {"realm-name": self.realm_name, "id": user_id} params_path = {"realm-name": self.realm_name, "id": user_id}
data_raw = self.connection.raw_put(URL_ADMIN_USER.format(**params_path), data_raw = self.connection.raw_put(URL_ADMIN_USER.format(**params_path),
data=json.dumps(data))
data=json.dumps(payload))
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204) return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)
def delete_user(self, user_id): def delete_user(self, user_id):

3
requirements.txt

@ -1,4 +1,3 @@
requests==2.18.3
requests==2.18.4
httmock==1.2.5 httmock==1.2.5
python-jose==1.3.2 python-jose==1.3.2
simplejson
Loading…
Cancel
Save