Skip to content

Commit

Permalink
Fix parsing of www-authenticate header
Browse files Browse the repository at this point in the history
For multi-valued www-authenticate headers the previous code could throw:

    trino.exceptions.TrinoAuthError: Error: header info didn't have x_token_server
  • Loading branch information
JonMerlevede authored and hashhar committed Sep 26, 2024
1 parent f8c7a32 commit 8eade11
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
24 changes: 19 additions & 5 deletions tests/unit/oauth_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,25 @@ def __call__(self, request, uri, response_headers):
if authorization and authorization.replace("Bearer ", "") in self.tokens:
return [200, response_headers, json.dumps(self.sample_post_response_data)]
elif self.redirect_server is None and self.token_server is not None:
return [401, {'Www-Authenticate': f'Bearer x_token_server="{self.token_server}"',
'Basic realm': '"Trino"'}, ""]
return [401, {'Www-Authenticate': f'Bearer x_redirect_server="{self.redirect_server}", '
f'x_token_server="{self.token_server}"',
'Basic realm': '"Trino"'}, ""]
return [401,
{
'Www-Authenticate': (
'Bearer realm="Trino", token_type="JWT", '
f'Bearer x_token_server="{self.token_server}"'
),
'Basic realm': '"Trino"'
},
""]
return [401,
{
'Www-Authenticate': (
'Bearer realm="Trino", token_type="JWT", '
f'Bearer x_redirect_server="{self.redirect_server}", '
f'x_token_server="{self.token_server}"'
),
'Basic realm': '"Trino"'
},
""]


class GetTokenCallback:
Expand Down
2 changes: 1 addition & 1 deletion trino/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ def _attempt_oauth(self, response: Response, **kwargs: Any) -> None:
auth_info_headers = self._parse_authenticate_header(auth_info)

auth_server = auth_info_headers.get('bearer x_redirect_server', auth_info_headers.get('x_redirect_server'))
token_server = auth_info_headers.get('x_token_server')
token_server = auth_info_headers.get('bearer x_token_server', auth_info_headers.get('x_token_server'))
if token_server is None:
raise exceptions.TrinoAuthError("Error: header info didn't have x_token_server")

Expand Down

0 comments on commit 8eade11

Please sign in to comment.