twonote / solid-file-python

solid-file-python is a Python library for creating and managing files and folders in Solid pods. https://www.w3.org/community/solid/
https://www.w3.org/community/solid/
MIT License
22 stars 11 forks source link

Login redirect raises HTTPStatusError #35

Closed Vidminas closed 11 months ago

Vidminas commented 1 year ago

A simple login snippet is problematic:

from getpass import getpass

USERNAME = getpass('Enter your user name:')
PASSWORD = getpass('Enter your password:')
IDP = 'https://solidcommunity.net'

from solid.solid_api import SolidAPI, Auth

auth = Auth()
api = SolidAPI(auth)
auth.login(IDP, USERNAME, PASSWORD)

running this raises HTTPStatusError: Redirect response '302 Found' for url 'https://solidcommunity.net/login/password'.

Full stack trace HTTPStatusError Traceback (most recent call last) Cell In[1], line 11 9 auth = Auth() 10 api = SolidAPI(auth) ---> 11 auth.login(IDP, USERNAME, PASSWORD) File [c:\Users\Vidminas\.conda\envs\solid\Lib\site-packages\solid\auth.py:25](file:///C:/Users/Vidminas/.conda/envs/solid/Lib/site-packages/solid/auth.py:25), in Auth.login(self, idp, username, password) 23 r = self.client.post(url, data=data) 24 # print(r) ---> 25 r.raise_for_status() 27 if not self.is_login: 28 raise Exception('Cannot login.') File [c:\Users\Vidminas\.conda\envs\solid\Lib\site-packages\httpx\_models.py:736](file:///C:/Users/Vidminas/.conda/envs/solid/Lib/site-packages/httpx/_models.py:736), in Response.raise_for_status(self) 734 error_type = error_types.get(status_class, "Invalid status code") 735 message = message.format(self, error_type=error_type) --> 736 raise HTTPStatusError(message, request=request, response=self) HTTPStatusError: Redirect response '302 Found' for url 'https://solidcommunity.net/login/password' Redirect location: 'https://solidrive.solidcommunity.net/' For more information check: https://httpstatuses.com/302

Looking at the source, the cause is in auth.py:

r = self.client.post(url, data=data)
r.raise_for_status()

I think this broke in https://github.com/twonote/solid-file-python/pull/29, when dependabot bumped the version of httpx. In the previous version (httpx 0.18.2), raise_for_status used to only raise an exception if the status code was a 4xx code (https://github.com/encode/httpx/blob/0.18.2/httpx/_models.py#L1384). In the newer version (httpx 0.23.0), raise_for_status raises an exception if the status is not a 2xx code, which includes redirects (https://github.com/encode/httpx/blob/0.23.0/httpx/_models.py#L701)

The code in auth.py should probably be wrapped in:

if not r.is_redirect:
    r.raise_for_status()
peter0083 commented 1 year ago

thank you @Vidminas for flagging this issue. Looks like it is related to #36 . httpx library version bump seems to be the cause. Any thoughts @hrchu ?

hrchu commented 1 year ago

@Vidminas nice catch, would you like to send a PR for this?

hrchu commented 11 months ago

The issue fixed in #39. @Vidminas whould you like to have a try?