lepture / authlib

The ultimate Python library in building OAuth, OpenID Connect clients and servers. JWS,JWE,JWK,JWA,JWT included.
https://authlib.org/
BSD 3-Clause "New" or "Revised" License
4.49k stars 448 forks source link

Incorrect signature for x-www-form-urlencoded POST requests with httpx OAuth1 client #517

Open shc261392 opened 1 year ago

shc261392 commented 1 year ago

Describe the bug

When using the exactly same credentials and API call signatures, the requests client could access protected resources from OAuth protected API, while the signature of requests made by httpx client is rejected.

This only happens for POST requests which sends non-binary data (Content-Type will be x-www-form-urlencoded).

Error Stacks

Error are raised at server side and is service-specific (depends on how API services respond to invalid OAuth signatures) so there's no error stack.

To Reproduce

from authlib.integrations.requests_client import OAuth1Session
from authlib.integrations.httpx_client import OAuth1Client

CLIENT_ID = '<client_id>'
CLIENT_SECRET = '<client_secret>'
TOKEN = '<token>'
TOKEN_SECRET = '<token_secret>'
ENDPOINT = '<protected_resource_api_endpoint>'
REQUEST_BODY = {'foo': 'bar'}

requests_client = OAuth1Session(CLIENT_ID, CLIENT_SECRET, token=TOKEN, token_secret=TOKEN_SECRET)
httpx_client = OAuth1Client(CLIENT_ID, CLIENT_SECRET, token=TOKEN, token_secret=TOKEN_SECRET)

r = requests_client.post(ENDPOINT, data=REQUEST_BODY)
print('REQUEST ', r.json(), '\n')

r = httpx_client.post(ENDPOINT, data=REQUEST_BODY)
print('HTTPX ', r.json())

Reproducing the issue with the above script requires access to OAuth1.0 protected API resources. In the above example, the requests client could access the protected resource without a problem, while the httpx client is rejected by an incorrect signature error.

Expected behavior

Both requests client and httpx client should have access to the protected resource when authenticated with access token.

Environment:

Additional context

I've tried to fix the issue and the fix seems working on my end, though I've only tested httpx OAuth1 client. Not sure whether it will break another clients or not.