Token POST request hangs with ASGI
Describe the bug
We have a Django project which serves multiple sites via a virtualhost middleware setup (not via Django site framework). This allows us to serve multiple sites from a single Django project instance, using the same settings.py but dynamic urlconf.
In this setup, there is a single auth provider, imagine localhost. Other sites use localhost as auth provider. E.g. myapp1.localhost is an OAuth client for localhost (provider). We are using Django social auth pipeline with custom OAuth backend to setup custom OAuth provider.
Everything works fine (from CLI). However, token POST request calls hangs indefinitely under ASGI mode. After some digging, we were able to confirm this behavior by running under WSGI mode, under which everything works fine.
In gist, here is how the flow looks like:
- User visits
myapp1.localhost:8000/social/login/custom/ - This redirects user to
localhost:8000/o/authorize/?...... - User successfully logins and accepts OAuth flow on
localhost:8000 - User is redirected back to
myapp1.localhost:8000/social/complete/custom/ - This triggers a server side API token request to
localhost:8000/o/token/which unfortunately hangs indefinitely.
Looking at logs, it hangs here:
[2021-03-03 08:55:40,021] DEBUG urllib3.connectionpool - connectionpool:_new_conn:227 - Starting new HTTP connection (1): localhost:8000
If we disable ASGI mode, everything works smoothly. Then logs look like:
[2021-03-03 08:55:40,021] DEBUG urllib3.connectionpool - connectionpool:_new_conn:227 - Starting new HTTP connection (1): localhost:8000
[2021-03-03 08:55:40,023] DEBUG oauthlib.oauth2.rfc6749.endpoints.token - token:create_token_response:116 - Dispatching grant_type authorization_code request to <oauthlib.oauth2.rfc6749.grant_types.authorization_code.AuthorizationCodeGrant object at 0x1144408b0>.
[2021-03-03 08:55:40,400] DEBUG oauthlib.oauth2.rfc6749.grant_types.authorization_code - authorization_code:validate_token_request:533 - Using provided redirect_uri http://myapp1.localhost:8000/social/complete/custom/?redirect_state=3EbZCgPGPP5WPYpYb7xFXpKQXV9Gx94j
[2021-03-03 08:55:40,441] DEBUG oauthlib.oauth2.rfc6749.grant_types.authorization_code - authorization_code:create_token_response:302 - Token request validation ok for <oauthlib.Request SANITIZED>.
[2021-03-03 08:55:41,157] DEBUG urllib3.connectionpool - connectionpool:_make_request:452 - http://localhost:8000 "POST /o/token/ HTTP/1.1" 200 169
To Reproduce
To reproduce:
- Add a middleware which adjust
request.urlconfbased upon incoming host. Forlocalhosturlconf is similar to that of a provider. Formyapp1.localhosturlconf simply offers OAuth login/complete/disconnect URLs (using django python social) - Initiating OAuth flow by visiting
myapp1.localhost:8000/social/login/custom/will result in above error.
Disable ASGI and everything will start to work fine. Let me know if a working project is necessary to debug and reproduce this.
Expected behavior
Behavior for ASGI and WSGI should be same. Currently workflow hangs under ASGI mode.