Skip to content

Commit

Permalink
Fix parsing authentication header
Browse files Browse the repository at this point in the history
  • Loading branch information
mdesmet authored and hashhar committed Nov 20, 2024
1 parent 4c57774 commit fe1022b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
3 changes: 2 additions & 1 deletion tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,13 +543,14 @@ def test_oauth2_authentication_missing_headers(header, error):
'Bearer x_redirect_server="{redirect_server}", x_token_server="{token_server}", Basic realm="Trino"',
'Basic realm="Trino", Bearer realm="Trino", token_type="JWT", Bearer x_redirect_server="{redirect_server}", '
'x_token_server="{token_server}"'
'Bearer x_redirect_server="{redirect_server}",x_token_server="{token_server}",additional_challenge',
])
@httprettified
def test_oauth2_header_parsing(header, sample_post_response_data):
token = str(uuid.uuid4())
challenge_id = str(uuid.uuid4())

redirect_server = f"{REDIRECT_RESOURCE}/{challenge_id}"
redirect_server = f"{REDIRECT_RESOURCE}/{challenge_id}?role=test"
token_server = f"{TOKEN_RESOURCE}/{challenge_id}"

# noinspection PyUnusedLocal
Expand Down
18 changes: 9 additions & 9 deletions trino/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,17 +546,17 @@ def _construct_cache_key(host: Optional[str], user: Optional[str]) -> Optional[s

@staticmethod
def _parse_authenticate_header(header: str) -> Dict[str, str]:
split_challenge = header.split(" ", 1)
trimmed_challenge = split_challenge[1] if len(split_challenge) > 1 else ""
logger.debug(f"Authentication header: {header}")
components = header.split(",")
auth_info_headers = {}

for item in trimmed_challenge.split(","):
comps = item.split("=")
if len(comps) == 2:
key = comps[0].strip(' "')
value = comps[1].strip(' "')
if key:
auth_info_headers[key.lower()] = value
for component in components:
component = component.strip()
if "=" in component:
key, value = component.split("=", 1)
if value[0] == '"' and value[-1] == '"':
value = value[1:-1]
auth_info_headers[key.lower()] = value
return auth_info_headers


Expand Down

0 comments on commit fe1022b

Please sign in to comment.