From 9c51d02a63e367cacef5e463f47ca696be3ea2e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jer=C3=B3nimo=20Mendes?= Date: Fri, 3 Feb 2023 22:07:47 +0000 Subject: [PATCH] feat: update header if token is given --- src/keycloak/keycloak_admin.py | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/keycloak/keycloak_admin.py b/src/keycloak/keycloak_admin.py index d985c1d..2a17da5 100644 --- a/src/keycloak/keycloak_admin.py +++ b/src/keycloak/keycloak_admin.py @@ -154,10 +154,22 @@ class KeycloakAdmin: self.custom_headers = custom_headers self.timeout = timeout - # Get token Admin - if not self.token: + if self.token is None: self.get_token() + headers = { + "Authorization": "Bearer " + self.token.get("access_token"), + "Content-Type": "application/json", + } if self.token is not None else {} + + if self.custom_headers is not None: + # merge custom headers to main headers + headers.update(self.custom_headers) + + self.connection = ConnectionManager( + base_url=self.server_url, headers=headers, timeout=60, verify=self.verify + ) + @property def server_url(self): """Get server url. @@ -3378,22 +3390,8 @@ class KeycloakAdmin: self.token = self.keycloak_openid.token( self.username, self.password, grant_type=grant_type, totp=self.totp ) - - headers = { - "Authorization": "Bearer " + self.token.get("access_token"), - "Content-Type": "application/json", - } else: self.token = None - headers = {} - - if self.custom_headers is not None: - # merge custom headers to main headers - headers.update(self.custom_headers) - - self.connection = ConnectionManager( - base_url=self.server_url, headers=headers, timeout=60, verify=self.verify - ) def refresh_token(self): """Refresh the token.