Browse Source

Merged in rhoai/python-keycloak (pull request #28)

add ability to log into service account in KeycloakAdmin

Approved-by: Rafael Weingärtner <rafaelweingartner@gmail.com>
Approved-by: Marcos Pereira <marcospereira.mpj@gmail.com>
pull/12/head
Anthony Alves 6 years ago
committed by Marcos Pereira
parent
commit
143b497d65
  1. 10
      keycloak/keycloak_admin.py

10
keycloak/keycloak_admin.py

@ -41,7 +41,7 @@ from .urls_patterns import URL_ADMIN_SERVER_INFO, URL_ADMIN_CLIENT_AUTHZ_RESOURC
class KeycloakAdmin: class KeycloakAdmin:
def __init__(self, server_url, username, password, realm_name='master', client_id='admin-cli', verify=True):
def __init__(self, server_url, username, password, realm_name='master', client_id='admin-cli', verify=True, client_secret_key=None):
""" """
:param server_url: Keycloak server url :param server_url: Keycloak server url
@ -50,6 +50,7 @@ class KeycloakAdmin:
:param realm_name: realm name :param realm_name: realm name
:param client_id: client id :param client_id: client id
:param verify: True if want check connection SSL :param verify: True if want check connection SSL
:param client_secret_key: client secret key
""" """
self._username = username self._username = username
self._password = password self._password = password
@ -58,9 +59,12 @@ class KeycloakAdmin:
# Get token Admin # Get token Admin
keycloak_openid = KeycloakOpenID(server_url=server_url, client_id=client_id, realm_name=realm_name, keycloak_openid = KeycloakOpenID(server_url=server_url, client_id=client_id, realm_name=realm_name,
verify=verify)
self._token = keycloak_openid.token(username, password)
verify=verify, client_secret_key=client_secret_key)
grant_type = ["password"]
if client_secret_key:
grant_type = ["client_credentials"]
self._token = keycloak_openid.token(username, password, grant_type=grant_type)
self._connection = ConnectionManager(base_url=server_url, self._connection = ConnectionManager(base_url=server_url,
headers={'Authorization': 'Bearer ' + self.token.get('access_token'), headers={'Authorization': 'Bearer ' + self.token.get('access_token'),
'Content-Type': 'application/json'}, 'Content-Type': 'application/json'},

Loading…
Cancel
Save