a
    ñ»j   ã                   @   s`   d dl Z d dlZd dlZd dlZd dlmZmZmZ ddlm	Z	 ddl
mZ G dd„ deƒZdS )é    N)ÚAnyÚOptionalÚUnioné   )Úutils)ÚOTPc                	       s¸   e Zd ZdZdeeeee ee eddœ‡ fdd„Zde	ee
j
f eed	œd
d„Zedœdd„Zdeee
j
 eedœdd„Zdee ee ee edœdd„Ze
j
edœdd„Z‡  ZS )ÚTOTPz.
    Handler for time-based OTP counters.
    é   Né   )ÚsÚdigitsÚdigestÚnameÚissuerÚintervalÚreturnc                    s.   |du rt j}|| _tƒ j|||||d� dS )aˆ  
        :param s: secret in base32 format
        :param interval: the time interval in seconds for OTP. This defaults to 30.
        :param digits: number of integers in the OTP. Some apps expect this to be 6 digits, others support more.
        :param digest: digest function to use in the HMAC (expected to be SHA1)
        :param name: account name
        :param issuer: issuer
        N)r   r   r   r   r   )ÚhashlibÚsha1r   ÚsuperÚ__init__)Úselfr   r   r   r   r   r   ©Ú	__class__© ú0E:\soccer\ml_env\Lib\site-packages\pyotp/totp.pyr      s    zTOTP.__init__r   )Úfor_timeÚcounter_offsetr   c                 C   s0   t |tjƒstj t|ƒ¡}|  |  |¡| ¡S )a  
        Accepts either a Unix timestamp integer or a datetime object.

        To get the time until the next timecode change (seconds until the current OTP expires), use this instead:

        .. code:: python

            totp = pyotp.TOTP(...)
            time_remaining = totp.interval - datetime.datetime.now().timestamp() % totp.interval

        :param for_time: the time to generate an OTP for
        :param counter_offset: the amount of ticks to add to the time counter
        :returns: OTP value
        )Ú
isinstanceÚdatetimeÚfromtimestampÚintÚgenerate_otpÚtimecode)r   r   r   r   r   r   Úat'   s    zTOTP.at)r   c                 C   s   |   |  tj ¡ ¡¡S )zL
        Generate the current time OTP

        :returns: OTP value
        )r!   r"   r   Únow)r   r   r   r   r$   :   s    zTOTP.now)Úotpr   Úvalid_windowr   c              	   C   sl   |du rt j  ¡ }|rRt| |d ƒD ]&}t t|ƒt|  ||¡ƒ¡r& dS q&dS t t|ƒt|  |¡ƒ¡S )ah  
        Verifies the OTP passed in against the current time OTP.

        :param otp: the OTP to check against
        :param for_time: Time to check OTP at (defaults to now)
        :param valid_window: extends the validity to this many counter ticks before and after the current one
        :returns: True if verification succeeded, False otherwise
        Nr   TF)r   r$   Úranger   Zstrings_equalÚstrr#   )r   r%   r   r&   Úir   r   r   ÚverifyB   s    	
zTOTP.verify)r   Úissuer_nameÚimager   c              	   C   s8   t j| j|r|n| j|r|n| j|  ¡ j| j| j|d�S )a  
        Returns the provisioning URI for the OTP.  This can then be
        encoded in a QR Code and used to provision an OTP app like
        Google Authenticator.

        See also:
            https://github.com/google/google-authenticator/wiki/Key-Uri-Format

        )r   Ú	algorithmr   Zperiodr,   )r   Z	build_uriÚsecretr   r   r   r   r   )r   r   r+   r,   r   r   r   Úprovisioning_uriV   s    ùzTOTP.provisioning_uri)r   r   c                 C   s:   |j rtt | ¡ ¡| j ƒS tt | ¡ ¡| j ƒS dS )z¿
        Accepts either a timezone naive (`for_time.tzinfo is None`) or
        a timezone aware datetime as argument and returns the
        corresponding counter value (timecode).

        N)	Útzinfor    ÚcalendarÚtimegmÚutctimetupler   ÚtimeÚmktimeÚ	timetuple)r   r   r   r   r   r"   m   s    zTOTP.timecode)r	   NNNr
   )r   )Nr   )NNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r(   r    r   r   r   r   r   r#   r$   Úboolr*   r/   r"   Ú__classcell__r   r   r   r   r      s0        ùø ÿþr   )r1   r   r   r4   Útypingr   r   r   Ú r   r%   r   r   r   r   r   r   Ú<module>   s   