Skip to content

Commit 1dd6c81

Browse files
authoredApr 7, 2024··
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.
1 parent 1823ac2 commit 1dd6c81

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed
 

‎src/keycloak/openid_connection.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -114,19 +114,18 @@ def __init__(
114114
self.client_secret_key = client_secret_key
115115
self.user_realm_name = user_realm_name
116116
self.timeout = timeout
117+
self.headers = {}
118+
self.custom_headers = custom_headers
117119

118120
if self.token is None:
119121
self.get_token()
120122

121-
self.headers = (
122-
{
123+
if self.token is not None:
124+
self.headers = {
125+
**self.headers,
123126
"Authorization": "Bearer " + self.token.get("access_token"),
124127
"Content-Type": "application/json",
125128
}
126-
if self.token is not None
127-
else {}
128-
)
129-
self.custom_headers = custom_headers
130129

131130
super().__init__(
132131
base_url=self.server_url, headers=self.headers, timeout=60, verify=self.verify
@@ -301,6 +300,7 @@ def keycloak_openid(self) -> KeycloakOpenID:
301300
verify=self.verify,
302301
client_secret_key=self.client_secret_key,
303302
timeout=self.timeout,
303+
custom_headers=self.custom_headers,
304304
)
305305

306306
return self._keycloak_openid

0 commit comments

Comments
 (0)
Please sign in to comment.