Skip to content

Commit 5f10177

Browse files
hovaescohashhar
authored andcommitted
Apply new logic for parsing WWW-Authenticate header
1 parent 169226e commit 5f10177

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

tests/unit/test_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,8 @@ def test_oauth2_authentication_missing_headers(header, error):
539539
'Bearer x_token_server="{token_server}", x_redirect_server="{redirect_server}"',
540540
'Basic realm="Trino", Bearer x_redirect_server="{redirect_server}", x_token_server="{token_server}"',
541541
'Bearer x_redirect_server="{redirect_server}", x_token_server="{token_server}", Basic realm="Trino"',
542+
'Basic realm="Trino", Bearer realm="Trino", token_type="JWT", Bearer x_redirect_server="{redirect_server}", '
543+
'x_token_server="{token_server}"'
542544
])
543545
@httprettified
544546
def test_oauth2_header_parsing(header, sample_post_response_data):

trino/auth.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
from requests import PreparedRequest, Request, Response, Session
2424
from requests.auth import AuthBase, extract_cookies_to_jar
25-
from requests.utils import parse_dict_header
2625

2726
import trino.logging
2827
from trino.client import exceptions
@@ -421,10 +420,13 @@ def _attempt_oauth(self, response: Response, **kwargs: Any) -> None:
421420
if not _OAuth2TokenBearer._BEARER_PREFIX.search(auth_info):
422421
raise exceptions.TrinoAuthError(f"Error: header info didn't match {auth_info}")
423422

424-
auth_info_headers = parse_dict_header(
425-
_OAuth2TokenBearer._BEARER_PREFIX.sub("", auth_info, count=1)) # type: ignore
423+
# Example www-authenticate header value:
424+
# 'Basic realm="Trino", Bearer realm="Trino", token_type="JWT",
425+
# Bearer x_redirect_server="https://trino.com/oauth2/token/uuid4",
426+
# x_token_server="https://trino.com/oauth2/token/uuid4"'
427+
auth_info_headers = self._parse_authenticate_header(auth_info)
426428

427-
auth_server = auth_info_headers.get('x_redirect_server')
429+
auth_server = auth_info_headers.get('bearer x_redirect_server', auth_info_headers.get('x_redirect_server'))
428430
token_server = auth_info_headers.get('x_token_server')
429431
if token_server is None:
430432
raise exceptions.TrinoAuthError("Error: header info didn't have x_token_server")
@@ -510,6 +512,21 @@ def _construct_cache_key(host: Optional[str], user: Optional[str]) -> Optional[s
510512
else:
511513
return f"{host}@{user}"
512514

515+
@staticmethod
516+
def _parse_authenticate_header(header: str) -> Dict[str, str]:
517+
split_challenge = header.split(" ", 1)
518+
trimmed_challenge = split_challenge[1] if len(split_challenge) > 1 else ""
519+
auth_info_headers = {}
520+
521+
for item in trimmed_challenge.split(","):
522+
comps = item.split("=")
523+
if len(comps) == 2:
524+
key = comps[0].strip(' "')
525+
value = comps[1].strip(' "')
526+
if key:
527+
auth_info_headers[key.lower()] = value
528+
return auth_info_headers
529+
513530

514531
class OAuth2Authentication(Authentication):
515532
def __init__(self, redirect_auth_url_handler: CompositeRedirectHandler = CompositeRedirectHandler([

0 commit comments

Comments
 (0)