From 619c2e7a27f805832082b860a876543f55af91ce Mon Sep 17 00:00:00 2001 From: Richard Nemeth Date: Sat, 1 Jun 2024 14:57:06 +0200 Subject: [PATCH] fix: set correct content type on token endpoint --- src/keycloak/keycloak_openid.py | 56 +++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/keycloak/keycloak_openid.py b/src/keycloak/keycloak_openid.py index 8c2a9cf..3a90d16 100644 --- a/src/keycloak/keycloak_openid.py +++ b/src/keycloak/keycloak_openid.py @@ -315,7 +315,14 @@ class KeycloakOpenID: payload["totp"] = totp payload = self._add_secret_key(payload) + content_type = self.connection.headers.get("Content-Type") + self.connection.add_param_headers("Content-Type", "application/x-www-form-urlencoded") data_raw = self.connection.raw_post(URL_TOKEN.format(**params_path), data=payload) + ( + self.connection.add_param_headers("Content-Type", content_type) + if content_type + else self.connection.del_param_headers("Content-Type") + ) return raise_error_from_response(data_raw, KeycloakPostError) def refresh_token(self, refresh_token, grant_type=["refresh_token"]): @@ -342,7 +349,14 @@ class KeycloakOpenID: "refresh_token": refresh_token, } payload = self._add_secret_key(payload) + content_type = self.connection.headers.get("Content-Type") + self.connection.add_param_headers("Content-Type", "application/x-www-form-urlencoded") data_raw = self.connection.raw_post(URL_TOKEN.format(**params_path), data=payload) + ( + self.connection.add_param_headers("Content-Type", content_type) + if content_type + else self.connection.del_param_headers("Content-Type") + ) return raise_error_from_response(data_raw, KeycloakPostError) def exchange_token( @@ -394,7 +408,14 @@ class KeycloakOpenID: "scope": scope, } payload = self._add_secret_key(payload) + content_type = self.connection.headers.get("Content-Type") + self.connection.add_param_headers("Content-Type", "application/x-www-form-urlencoded") data_raw = self.connection.raw_post(URL_TOKEN.format(**params_path), data=payload) + ( + self.connection.add_param_headers("Content-Type", content_type) + if content_type + else self.connection.del_param_headers("Content-Type") + ) return raise_error_from_response(data_raw, KeycloakPostError) def userinfo(self, token): @@ -668,7 +689,14 @@ class KeycloakOpenID: } self.connection.add_param_headers("Authorization", "Bearer " + token) + content_type = self.connection.headers.get("Content-Type") + self.connection.add_param_headers("Content-Type", "application/x-www-form-urlencoded") data_raw = self.connection.raw_post(URL_TOKEN.format(**params_path), data=payload) + ( + self.connection.add_param_headers("Content-Type", content_type) + if content_type + else self.connection.del_param_headers("Content-Type") + ) return raise_error_from_response(data_raw, KeycloakPostError) def has_uma_access(self, token, permissions): @@ -875,7 +903,14 @@ class KeycloakOpenID: payload["totp"] = totp payload = self._add_secret_key(payload) + content_type = self.connection.headers.get("Content-Type") + self.connection.add_param_headers("Content-Type", "application/x-www-form-urlencoded") data_raw = await self.connection.a_raw_post(URL_TOKEN.format(**params_path), data=payload) + ( + self.connection.add_param_headers("Content-Type", content_type) + if content_type + else self.connection.del_param_headers("Content-Type") + ) return raise_error_from_response(data_raw, KeycloakPostError) async def a_refresh_token(self, refresh_token, grant_type=["refresh_token"]): @@ -902,7 +937,14 @@ class KeycloakOpenID: "refresh_token": refresh_token, } payload = self._add_secret_key(payload) + content_type = self.connection.headers.get("Content-Type") + self.connection.add_param_headers("Content-Type", "application/x-www-form-urlencoded") data_raw = await self.connection.a_raw_post(URL_TOKEN.format(**params_path), data=payload) + ( + self.connection.add_param_headers("Content-Type", content_type) + if content_type + else self.connection.del_param_headers("Content-Type") + ) return raise_error_from_response(data_raw, KeycloakPostError) async def a_exchange_token( @@ -954,7 +996,14 @@ class KeycloakOpenID: "scope": scope, } payload = self._add_secret_key(payload) + content_type = self.connection.headers.get("Content-Type") + self.connection.add_param_headers("Content-Type", "application/x-www-form-urlencoded") data_raw = await self.connection.a_raw_post(URL_TOKEN.format(**params_path), data=payload) + ( + self.connection.add_param_headers("Content-Type", content_type) + if content_type + else self.connection.del_param_headers("Content-Type") + ) return raise_error_from_response(data_raw, KeycloakPostError) async def a_userinfo(self, token): @@ -1230,7 +1279,14 @@ class KeycloakOpenID: } self.connection.add_param_headers("Authorization", "Bearer " + token) + content_type = self.connection.headers.get("Content-Type") + self.connection.add_param_headers("Content-Type", "application/x-www-form-urlencoded") data_raw = self.connection.raw_post(URL_TOKEN.format(**params_path), data=payload) + ( + self.connection.add_param_headers("Content-Type", content_type) + if content_type + else self.connection.del_param_headers("Content-Type") + ) return raise_error_from_response(data_raw, KeycloakPostError) async def a_has_uma_access(self, token, permissions):