From 0b8d45e148f2ae796cd771b4134ebeb9f1eb478f Mon Sep 17 00:00:00 2001 From: Matt Collecutt Date: Wed, 13 Mar 2024 22:52:23 +1300 Subject: [PATCH] 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'. --- src/keycloak/openid_connection.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/keycloak/openid_connection.py b/src/keycloak/openid_connection.py index 081cbaf..96ea296 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", + "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