Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Telegram views CallbackView 'bool' object has no attribute 'pop' #3764

Closed
I-SMAF opened this issue Apr 25, 2024 · 2 comments
Closed

Telegram views CallbackView 'bool' object has no attribute 'pop' #3764

I-SMAF opened this issue Apr 25, 2024 · 2 comments

Comments

@I-SMAF
Copy link

I-SMAF commented Apr 25, 2024

error

"GET /accounts/telegram/login/callback/?state=WINdxvbAJ9zvOFaL HTTP/1.1"

Internal Server Error: /accounts/telegram/login/callback/
Traceback (most recent call last):
  File "Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "Lib\site-packages\django\views\generic\base.py", line 104, in view
    return self.dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "Lib\site-packages\django\utils\decorators.py", line 48, in _wrapper
    return bound_method(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "Lib\site-packages\django\views\decorators\csrf.py", line 65, in _view_wrapper
    return view_func(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "Lib\site-packages\django\views\generic\base.py", line 143, in dispatch
    return handler(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "Lib\site-packages\allauth\socialaccount\providers\telegram\views.py", line 47, in post
    hash = data.pop("hash")
           ^^^^^^^^
AttributeError: 'bool' object has no attribute 'pop'
@method_decorator(csrf_exempt, name="dispatch")
class CallbackView(View):
    def get(self, request):
        return render(request, "telegram/callback.html")

    def post(self, request):
        adapter = get_adapter()
        provider = adapter.get_provider(request, TelegramProvider.id)

        state_id = request.GET.get("state")
        if not state_id:
            return render_authentication_error(
                request,
                provider=provider,
            )

        result = request.POST.get("tgAuthResult")
        padding = "=" * (4 - (len(result) % 4))
        data = json.loads(base64.b64decode(result + padding))
        hash = data.pop("hash")
        payload = "\n".join(sorted(["{}={}".format(k, v) for k, v in data.items()]))
        token = provider.app.secret
        token_sha256 = hashlib.sha256(token.encode()).digest()
        expected_hash = hmac.new(
            token_sha256, payload.encode(), hashlib.sha256
        ).hexdigest()
        auth_date = int(data.pop("auth_date"))
        auth_date_validity = provider.get_auth_date_validity()
        if hash != expected_hash or time.time() - auth_date > auth_date_validity:
            return render_authentication_error(
                request,
                provider=provider,
                extra_context={"response": data, "state_id": state_id},
            )
        login = provider.sociallogin_from_response(request, data)
        login.state = provider.unstash_redirect_state(request, state_id)
        return complete_social_login(request, login)
result = request.POST.get("tgAuthResult")
padding = "=" * (4 - (len(result) % 4))
data = json.loads(base64.b64decode(result + padding))

-----> data: False

@I-SMAF I-SMAF changed the title Telegram views CallbackView'bool' object has no attribute 'pop' Telegram views CallbackView 'bool' object has no attribute 'pop' Apr 25, 2024
@pennersr
Copy link
Owner

Something is wrong/unexpected in your tgAuthResult:

data = json.loads(base64.b64decode(result + padding))

In your case, that results in a boolean, which clearly has no pop()... but under normal circumstances that should not occur. I suspect something is wrong with your setup/config.

@pennersr
Copy link
Owner

See aa1a78d -- this fixes crashing on invalid tgAuthResult. It does not solve your issue though, but that is beyond control.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants