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.59k stars 461 forks source link

getting no client id from vk.com oauth #652

Open swats-the-floran opened 6 months ago

swats-the-floran commented 6 months ago

here is my settings and code if it is not a bug:

settings

AUTHLIB_OAUTH_CLIENTS = {
    'vkontakte': {
        'client_id': os.getenv('VKONTAKTE_CLIENT_ID'),
        'client_secret': os.getenv('VKONTAKTE_CLIENT_SECRET'),
        'access_token_url': 'https://oauth.vk.com/access_token',
        'authorize_url': 'https://oauth.vk.com/authorize',
        'userinfo_endpoint': 'https://api.vk.com/method/',
        'client_kwargs': {
            'scope': 'email',
            # 'response_type': 'token',
        },
    }
}

django view

def vkontakte_login(request: WSGIRequest) -> HttpResponseRedirect:
    redirect_uri = request.build_absolute_uri(reverse('vkontakte_auth'))
    return oauth.vkontakte.authorize_redirect(request, redirect_uri)

def vkontakte_auth(request: WSGIRequest) -> HttpResponseRedirect:
    vkontakte_token_info = oauth.vkontakte.authorize_access_token(request)
    resp = oauth.vkontakte.get('info', token=vkontakte_token_info)
    user_info = resp.json()
    user = find_or_create_user(
        email=user_info['default_email'],
        is_active=True,
        username=user_info['login'],
        first_name=user_info['first_name'],
        last_name=user_info['last_name'],
    )

    social_account = find_or_create_social_account(user.id, 'vkontakte')
    social_account.token_type = vkontakte_token_info['token_type'],
    social_account.access_token = vkontakte_token_info['access_token']
    social_account.refresh_token = vkontakte_token_info['refresh_token']
    social_account.access_token_expiration = datetime.fromtimestamp(vkontakte_token_info['expires_at'])
    social_account.save()

    djoser_token = djoser_utils.login_user(request, user)

    response = HttpResponseRedirect(stn.SOCIAL_REDIRECT)
    set_protected_cookie(response, 'token', str(djoser_token))

    return response

here is what comes in response from vk oauth service:

request_headers:
{
    'Host': 'domain.su',
    'X-Real-Ip': '178.208.75.177',
    'X-Forwarded-For': '178.208.75.177',
    'X-Forwarded-Proto': 'https',
    'X-Request-Id': '2448bdf6d949cf599d7c7d816a5a7d09',
    'Connection': 'close', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
    'Accept-Language': 'en-US,en;q=0.5',
    'Accept-Encoding': 'gzip, deflate, br', 'Referer': 'https://id.vk.com/',
    'Cookie': 'csrftoken=kDTHlCrNOglwyOvKseZbZdsZUAfEWKgK; sessionid=vb6kemx5x23hya5rdyr2gg2xxu2qsshq; token=ee7dd8b8f2fede92cfe9f65375bc26db3c8b93b7', 'Upgrade-Insecure-Requests': '1', 'Sec-Fetch-Dest': 'document', 'Sec-Fetch-Mode': 'navigate', 'Sec-Fetch-Site': 'cross-site', 'Sec-Fetch-User': '?1'
}

params:
{
    'code': 'e9d313142c0f893541',
     'state': 'I0wbcAvsI7IOwnJyiIgazozqDAnx8q'
}

claims_options: None

state_data:
{
    'redirect_uri': 'https://domain.su/user/social/vkontakte_auth/',
    'url': 'https://oauth.vk.com/authorize?response_type=code&client_id=51927258&redirect_uri=https%3A%2F%2Fdomain.su%2Fuser%2Fsocial%2Fvkontakte_auth%2F&scope=email&state=I0wbcAvsI7IOwnJyiIgazozqDAnx8q'
}

after getting response i get a "client_id is undefined" error:

exception:

Traceback (most recent call last):
  File "/root/user-service/venv/lib/python3.12/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/root/user-service/venv/lib/python3.12/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/user-service/backend/user/views_social.py", line 103, in vkontakte_auth
    vkontakte_token_info = oauth.vkontakte.authorize_access_token(request)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/user-service/venv/lib/python3.12/site-packages/authlib/integrations/django_client/apps.py", line 90, in authorize_access_token
    token = self.fetch_access_token(**params, **kwargs)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/user-service/venv/lib/python3.12/site-packages/authlib/integrations/base_client/sync_app.py", line 342, in fetch_access_token
    token = client.fetch_token(token_endpoint, **params)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/user-service/venv/lib/python3.12/site-packages/authlib/oauth2/client.py", line 211, in fetch_token
    return self._fetch_token(
           ^^^^^^^^^^^^^^^^^^
  File "/root/user-service/venv/lib/python3.12/site-packages/authlib/oauth2/client.py", line 368, in _fetch_token
    return self.parse_response_token(resp)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/user-service/venv/lib/python3.12/site-packages/authlib/oauth2/client.py", line 344, in parse_response_token
    raise self.oauth_error_class(
authlib.integrations.base_client.errors.OAuthError: invalid_client: client_id is undefined
swats-the-floran commented 6 months ago

vk.com needs additional parameters so the fix is in one string:

    vkontakte_token_info = oauth.vkontakte.authorize_access_token(
        request=request,
        client_id=settings.client_id,
        client_secret=settings.client_secret,
    )