iMerica / dj-rest-auth

Authentication for Django Rest Framework
https://dj-rest-auth.readthedocs.io/en/latest/index.html
MIT License
1.64k stars 306 forks source link

Google Provider Error, response["id_token"] #481

Open 5no0p opened 1 year ago

5no0p commented 1 year ago

I get this error when using google provider

from dj_rest_auth.registration.views import SocialLoginView
from allauth.socialaccount.providers.google.views import GoogleOAuth2Adapter
from allauth.socialaccount.providers.oauth2.client import OAuth2Client

class GoogleLoginView(SocialLoginView):
    authentication_classes = (
        []
    )  # disable authentication, make sure to override `allowed origins` in settings.py in production!
    adapter_class = GoogleOAuth2Adapter
    callback_url = "http://localhost:8000/accounts/google/login/callback/" 
    client_class = OAuth2Client
2023-02-05 21:58:32,311 ERROR Internal Server Error: /users/social/google/
Traceback (most recent call last):
  File "C:\Users\mmoaa\.virtualenvs\Django-Celery-sT3nqBRY\lib\site-packages\asgiref\sync.py", line 486, in thread_handler
    raise exc_info[1]
  File "C:\Users\mmoaa\.virtualenvs\Django-Celery-sT3nqBRY\lib\site-packages\django\core\handlers\exception.py", line 38, in inner
    response = await get_response(request)
  File "C:\Users\mmoaa\.virtualenvs\Django-Celery-sT3nqBRY\lib\site-packages\django\core\handlers\base.py", line 233, in _get_response_async
    response = await wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\mmoaa\.virtualenvs\Django-Celery-sT3nqBRY\lib\site-packages\asgiref\sync.py", line 448, in __call__
    ret = await asyncio.wait_for(future, timeout=None)
  File "c:\users\mmoaa\appdata\local\programs\python\python39\lib\asyncio\tasks.py", line 442, in wait_for
    return await fut
  File "C:\Users\mmoaa\.virtualenvs\Django-Celery-sT3nqBRY\lib\site-packages\asgiref\current_thread_executor.py", line 22, in run
    result = self.fn(*self.args, **self.kwargs)
  File "C:\Users\mmoaa\.virtualenvs\Django-Celery-sT3nqBRY\lib\site-packages\asgiref\sync.py", line 490, in thread_handler
    return func(*args, **kwargs)
  File "C:\Users\mmoaa\.virtualenvs\Django-Celery-sT3nqBRY\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view    
    return view_func(*args, **kwargs)
  File "C:\Users\mmoaa\.virtualenvs\Django-Celery-sT3nqBRY\lib\site-packages\django\views\generic\base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\mmoaa\.virtualenvs\Django-Celery-sT3nqBRY\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "C:\Users\mmoaa\.virtualenvs\Django-Celery-sT3nqBRY\lib\site-packages\django\views\decorators\debug.py", line 89, in sensitive_post_parameters_wrapper
    return view(request, *args, **kwargs)
  File "C:\Users\mmoaa\.virtualenvs\Django-Celery-sT3nqBRY\lib\site-packages\dj_rest_auth\views.py", line 53, in dispatch
    return super().dispatch(*args, **kwargs)
  File "C:\Users\mmoaa\.virtualenvs\Django-Celery-sT3nqBRY\lib\site-packages\rest_framework\views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "C:\Users\mmoaa\.virtualenvs\Django-Celery-sT3nqBRY\lib\site-packages\rest_framework\views.py", line 469, in handle_exception       
    self.raise_uncaught_exception(exc)
  File "C:\Users\mmoaa\.virtualenvs\Django-Celery-sT3nqBRY\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
    raise exc
  File "C:\Users\mmoaa\.virtualenvs\Django-Celery-sT3nqBRY\lib\site-packages\rest_framework\views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "C:\Users\mmoaa\.virtualenvs\Django-Celery-sT3nqBRY\lib\site-packages\dj_rest_auth\views.py", line 130, in post
    self.serializer.is_valid(raise_exception=True)
  File "C:\Users\mmoaa\.virtualenvs\Django-Celery-sT3nqBRY\lib\site-packages\rest_framework\serializers.py", line 227, in is_valid
    self._validated_data = self.run_validation(self.initial_data)
  File "C:\Users\mmoaa\.virtualenvs\Django-Celery-sT3nqBRY\lib\site-packages\rest_framework\serializers.py", line 429, in run_validation   
    value = self.validate(value)
  File "C:\Users\mmoaa\.virtualenvs\Django-Celery-sT3nqBRY\lib\site-packages\dj_rest_auth\registration\serializers.py", line 150, in validate
    login = self.get_social_login(adapter, app, social_token, token)
  File "C:\Users\mmoaa\.virtualenvs\Django-Celery-sT3nqBRY\lib\site-packages\dj_rest_auth\registration\serializers.py", line 60, in get_social_login
    social_login = adapter.complete_login(request, app, token, response=response)
  File "C:\Users\mmoaa\.virtualenvs\Django-Celery-sT3nqBRY\lib\site-packages\allauth\socialaccount\providers\google\views.py", line 22, in 
complete_login
    response["id_token"],
TypeError: string indices must be integers
sp-luciano-chinke commented 1 year ago

Opened PR yesterday that fixes this small issue: https://github.com/iMerica/dj-rest-auth/pull/482

a-ruban commented 1 year ago

@sp-luciano-chinke have you verified that the fix actually working? I'm getting 'Invalid id_token' with this approach, but it could be something on my side.

a-ruban commented 1 year ago

In my case the issue was in allauth 0.52 version, they updated GoogleOauth2 provider (I believe to use id_token instead of access_token). So if you face with problem mentioned in issue - try to return to allauth 0.51

c-w commented 1 year ago

If you want to upgrade to allauth >=0.52 but want to stick with the access token instead of the id token, consider modifying your client code and switching to the auth-code flow instead of the implicit flow.

Note that there's currently a bug in dj-rest-auth >=2.2.8 and later using the auth-code flow, but there's a fix for this in https://github.com/iMerica/dj-rest-auth/pull/488.

Housain-maina commented 1 year ago

Answered in https://github.com/iMerica/dj-rest-auth/issues/516#issuecomment-1630741716