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

QR code аuthorization #3081

Closed
3 tasks done
IvanNazaruk opened this issue Jun 20, 2021 · 1 comment · Fixed by #3082 or sakibguy/Telethon#4
Closed
3 tasks done

QR code аuthorization #3081

IvanNazaruk opened this issue Jun 20, 2021 · 1 comment · Fixed by #3082 or sakibguy/Telethon#4

Comments

@IvanNazaruk
Copy link
Contributor

Checklist

  • The error is in the library's code, and not in my own.
  • I have searched for this issue before posting it and there isn't a duplicate.
  • I ran pip install -U https://github.com/LonamiWebs/Telethon/archive/master.zip and triggered the bug in the latest version.

Code that causes the issue

from telethon import TelegramClient
import asyncio

api_id = ""
api_hash = ""


def display_url_as_qr(url):
    print(url)
    pass  # do whatever to show url as a qr to the user


async def qr_login():
    client = TelegramClient('test', api_id, api_hash)
    await client.connect()

    qr_login = await client.qr_login()
    display_url_as_qr(qr_login.url)

    # Important! You need to wait for the login to complete!
    await qr_login.wait()
    print(await client.get_me())


loop = asyncio.get_event_loop()
loop.run_until_complete(qr_login())

Problem
When you scan the generated qr code, the application on the phone crashes: (click to open gif)
windows

phone.mp4

But not always: (click to open gif)
windows_2

phone_2.mp4

What's the problem?
The problem is that "qrlogin.py" uses base64 -> base64.b64encode(), as I understand it should be base64url -> urlsafe_b64encode().
I found url64 library, it has this code:

from base64 import urlsafe_b64encode, urlsafe_b64decode

__all__ = ['encode', 'decode']


def pad(s):
    return s + '=' * (4 - len(s) & 3)


def encode(s):
    if isinstance(s, str):
        s = s.encode()
    return urlsafe_b64encode(s).decode().rstrip("=")


def decode(s):
    return urlsafe_b64decode(pad(s)).decode()

and it does just fine:

import url64
return 'tg://login?token={}'.format(url64.encode(self._resp.token))

(I'm not suggesting that add this code to the library, it's just an example of how I fixed it)

What has changed?
Old:

tg://login?token=AQK/M89gA0PawCUIZe1cs5Y657FATBKcKfJ/C5NaQETbFQ==
tg://login?token=AQLcM89genyIgiSpZB6wmXJ2X3EKOma4x9MuAME/8ygXKA==
tg://login?token=AQL4M89gHX+0YcgqisVOiGSYEKlyz++1DbtOk9OfvDTxbg==

New:

tg://login?token=AQK_M89gA0PawCUIZe1cs5Y657FATBKcKfJ_C5NaQETbFQ
tg://login?token=AQLcM89genyIgiSpZB6wmXJ2X3EKOma4x9MuAME_8ygXKA
tg://login?token=AQL4M89gHX-0YcgqisVOiGSYEKlyz--1DbtOk9OfvDTxbg
@Lonami
Copy link
Member

Lonami commented Jun 20, 2021

Thank you for the detailed report. Would you like to send a pull request? As I understand it, the changes needed are using base64.urlsafe_b64encode and adding .rstrip('=').

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

Successfully merging a pull request may close this issue.

2 participants