From 1dd6c81f523a3159f5d0874c4d0154592ea2b97c Mon Sep 17 00:00:00 2001 From: Matt Collecutt Date: Sun, 7 Apr 2024 23:27:44 +1200 Subject: [PATCH] fix: incorporate custom headers into default header setup (#533) * refactor: incorporate custom headers into default header setup Modify the default headers construction to merge in custom headers if provided. This change ensures that custom headers, such as Cloudflare access tokens, are included in the request before self.get_token() is called and without overwriting the default headers like 'Authorization' and 'Content-Type'. * refactor: Fixing linting issue Fixing linting issue by adding trailing comma to key in dict * refactor: adding custom headers to KeycloakOpenID Adding `custom_headers` to KeycloakOpenID instantiation. --- src/keycloak/openid_connection.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/keycloak/openid_connection.py b/src/keycloak/openid_connection.py index 081cbaf..583afcd 100644 --- a/src/keycloak/openid_connection.py +++ b/src/keycloak/openid_connection.py @@ -114,19 +114,18 @@ class KeycloakOpenIDConnection(ConnectionManager): self.client_secret_key = client_secret_key self.user_realm_name = user_realm_name self.timeout = timeout + self.headers = {} + self.custom_headers = custom_headers if self.token is None: self.get_token() - self.headers = ( - { + if self.token is not None: + self.headers = { + **self.headers, "Authorization": "Bearer " + self.token.get("access_token"), "Content-Type": "application/json", } - if self.token is not None - else {} - ) - self.custom_headers = custom_headers super().__init__( base_url=self.server_url, headers=self.headers, timeout=60, verify=self.verify @@ -301,6 +300,7 @@ class KeycloakOpenIDConnection(ConnectionManager): verify=self.verify, client_secret_key=self.client_secret_key, timeout=self.timeout, + custom_headers=self.custom_headers, ) return self._keycloak_openid