U
    VQhm                     @   sF   d dl Z d dlZd dlZddlmZmZ ddlmZ G dd dZdS )    N   )types	functions   )eventsc                   @   sh   e Zd ZdZdd Zdd ZeedddZee	dd	d
Z
eejdddZdedddZdS )QRLoginz
    QR login information.

    Most of the time, you will present the `url` as a QR code to the user,
    and while it's being shown, call `wait`.
    c                 C   s*   || _ tj| j j| j j|| _d | _d S N)_clientr   authZExportLoginTokenRequestZapi_idZapi_hash_request_resp)selfclientZignored_ids r   >/tmp/pip-build-6059463i/telethon/telethon/tl/custom/qrlogin.py__init__   s      zQRLogin.__init__c                    s   |  | jI dH | _dS )z
        Generates a new token and URL for a new QR code, useful if the code
        has expired before it was imported.
        N)r	   r   r   r   r   r   r   recreate   s    zQRLogin.recreate)returnc                 C   s   | j jS )z
        The binary data representing the token.

        It can be used by a previously-authorized client in a call to
        :tl:`auth.importLoginToken` to log the client that originally
        requested the QR login.
        )r   tokenr   r   r   r   r      s    	zQRLogin.tokenc                 C   s    d t| jjddS )a}  
        The ``tg://login`` URI with the token. When opened by a Telegram
        application where the user is logged in, it will import the login
        token.

        If you want to display a QR code to the user, this is the URL that
        should be launched when the QR code is scanned (the URL that should
        be contained in the QR code image you generate).

        Whether you generate the QR code image or not is up to you, and the
        library can't do this for you due to the vast ways of generating and
        displaying the QR code that exist.

        The URL simply consists of `token` base64-encoded.
        ztg://login?token={}zutf-8=)formatbase64urlsafe_b64encoder   r   decoderstripr   r   r   r   url(   s    zQRLogin.urlc                 C   s   | j jS )z
        The `datetime` at which the QR code will expire.

        If you want to try again, you will need to call `recreate`.
        )r   expiresr   r   r   r   r   ;   s    zQRLogin.expiresNtimeoutc              	      s  |dkr&| j jtjjtjjd  }t   fdd}| j	
|ttj ztj  |dI dH  W 5 | j	| X | 	| jI dH }t|tjjr| j	|jI dH  | 	tj|jI dH }t|tjjr|jj}| j	|I dH  |S td |dS )a  
        Waits for the token to be imported by a previously-authorized client,
        either by scanning the QR, launching the URL directly, or calling the
        import method.

        This method **must** be called before the QR code is scanned, and
        must be executing while the QR code is being scanned. Otherwise, the
        login will not complete.

        Will raise `asyncio.TimeoutError` if the login doesn't complete on
        time.

        Arguments
            timeout (float):
                The timeout, in seconds, to wait before giving up. By default
                the library will wait until the token expires, which is often
                what you want.

        Returns
            On success, an instance of :tl:`User`. On failure it will raise.
        N)tzc                    s       d S r   )set)_updateeventr   r   handler_   s    zQRLogin.wait.<locals>.handlerr   z'Login token response was unexpected: {})!r   r   datetimenowtimezoneutctotal_secondsasyncioEventr	   Zadd_event_handlerr   ZRawr   ZUpdateLoginTokenZremove_event_handlerwait_forwaitr   
isinstancer
   ZLoginTokenMigrateToZ
_switch_dcZdc_idr   ZImportLoginTokenRequestr   ZLoginTokenSuccessauthorizationuserZ	_on_login	TypeErrorr   )r   r   r%   respr1   r   r#   r   r.   D   s"    zQRLogin.wait)N)__name__
__module____qualname____doc__r   r   propertybytesr   strr   r&   r   floatr.   r   r   r   r   r   	   s   
r   )r+   r   r&    r   r   r   r   r   r   r   r   <module>   s
   