Skip to content

Added code to force authentication on first request instead of authentication-challenge#238

Open
enothen wants to merge 1 commit intohttplib2:masterfrom
enothen:master
Open

Added code to force authentication on first request instead of authentication-challenge#238
enothen wants to merge 1 commit intohttplib2:masterfrom
enothen:master

Conversation

@enothen
Copy link
Copy Markdown

@enothen enothen commented May 31, 2023

Added "forceauth" parameter to address issue #175.

Current default behavior is kept:

>>> h = httplib2.Http(".cache")
>>> h.add_credentials(user, password)
>>> headersDict={'Content-Type':'application/json','accept':'application/json'}
>>> response, content = h.request(url, 'GET', headers=headersDict)
>>> 
>>> response.status
401
>>> 

The webserver in the example above does not return a "www-authenticate" header, and so authentication is not tried (even though credentials are provided and a 401 is received in the first attempt):

>>> response
{'date': 'Wed, 31 May 2023 12:29:44 GMT', 'server': 'Apache', 'content-type': 'application/json; charset=utf-8', 'apipie-checksum': '5346b3362dcb7d38bd1dd226bc763427f7fe8200', 'cache-control': 'no-cache', 'x-request-id': 'e27cfd25-00ad-466a-b03f-c1a849001697', 'x-runtime': '0.026089', 'strict-transport-security': 'max-age=631139040; includeSubdomains', 'x-frame-options': 'sameorigin', 'x-content-type-options': 'nosniff', 'x-xss-protection': '1; mode=block', 'x-download-options': 'noopen', 'x-permitted-cross-domain-policies': 'none', 'content-security-policy': "default-src 'self'; child-src 'self'; connect-src 'self' ws: wss:; img-src 'self' data:; script-src 'unsafe-eval' 'unsafe-inline' 'self'; style-src 'unsafe-inline' 'self'", 'via': '1.1 satellite.keller.lab', 'transfer-encoding': 'chunked', 'status': '401'}

And from the webserver side:

192.168.122.149 - - [31/May/2023:12:29:44 +0000] "GET /api/hosts HTTP/1.1" 401 58 "-" "Python-httplib2/0.13.1 (gzip)"

Forcing auth allows successful login on the first try:

>>> h2 = httplib2.Http(forceauth="basic")
>>> h2.add_credentials(user, password)
>>> response, content = h2.request(url, 'GET', headers=headersDict)
>>> 
>>> response.status
200
>>> 
192.168.122.149 - - [31/May/2023:12:45:05 +0000] "GET /api/hosts HTTP/1.1" 200 69189 "-" "Python-httplib2/0.13.1 (gzip)"

Feedback/corrections are welcome, as this is my first take of an issue here in httplib2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant