-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use HttpUrlOperations.probe_url for realm retrival
This commit changes the test utilities to use existing functionality in HttpUrlOperations to determine the authentication realm of a URL.
- Loading branch information
1 parent
30960e4
commit 4b208b7
Showing
1 changed file
with
16 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,24 @@ | ||
from __future__ import annotations | ||
|
||
import requests | ||
import www_authenticate | ||
from datalad_next.url_operations.http import HttpUrlOperations | ||
|
||
|
||
def get_restricted_realm(url: str) -> str | None: | ||
"""A very simple function to get the realm for restricted access | ||
"""Get the realm for basic auth-restricted access | ||
This will only work, if the server returns a 'www-authenticate'- | ||
header, and if the authentication method is 'basic' | ||
""" | ||
Parameters | ||
---------- | ||
url: str | ||
URL to probe | ||
r = requests.get(url) | ||
Returns | ||
------- | ||
str | None | ||
The name of the realm for basic authentication | ||
or None, if either no authentication is required, | ||
or if the authentication type is not "basic" | ||
""" | ||
|
||
auth_header_name = 'WWW-Authenticate' | ||
auth_header = r.headers.get(auth_header_name, None) | ||
if auth_header: | ||
parsed = www_authenticate.parse(auth_header) | ||
if 'Basic' in parsed: | ||
return parsed['Basic']['realm'] | ||
return None | ||
http_url_ops = HttpUrlOperations() | ||
_, url_properties = http_url_ops.probe_url(url) | ||
return url_properties.get('auth', {}).get('basic', {}).get('realm') |