o
    ãË·e  ã                   @   s€   g d ¢Z ddlmZ ddlmZmZmZmZ ddlmZ ddl	Z	e	j
e	j ZdZdZedƒZd	d
„ Zedfdd„Zddd„ZdS ))ÚsplitÚmkNonceÚcheckTimestampé    )Ú	cryptutil)ÚstrptimeÚstrftimeÚgmtimeÚtime)ÚtimegmNiPF  z%Y-%m-%dT%H:%M:%SZz0000-00-00T00:00:00Zc                 C   sV   | dt … }z	tt|tƒƒ}W n ty   d}Y nw |dk r#tdƒ‚|| t d… fS )aZ  Extract a timestamp from the given nonce string

    @param nonce_string: the nonce from which to extract the timestamp
    @type nonce_string: str

    @returns: A pair of a Unix timestamp and the salt characters
    @returntype: (int, str)

    @raises ValueError: if the nonce does not start with a correctly
        formatted time string
    Néÿÿÿÿr   ztime out of range)Útime_str_lenr
   r   Útime_fmtÚAssertionErrorÚ
ValueError)Únonce_stringÚtimestamp_strÚ	timestamp© r   úI/var/www/ideatree/venv/lib/python3.10/site-packages/openid/store/nonce.pyr      s   ÿr   c                 C   s\   zt | ƒ\}}W n
 ty   Y dS w |du rtƒ }|| }|| }||  ko+|kS   S )a8  Is the timestamp that is part of the specified nonce string
    within the allowed clock-skew of the current time?

    @param nonce_string: The nonce that is being checked
    @type nonce_string: str

    @param allowed_skew: How many seconds should be allowed for
        completing the request, allowing for clock skew.
    @type allowed_skew: int

    @param now: The current time, as a Unix timestamp
    @type now: int

    @returntype: bool
    @returns: Whether the timestamp is correctly formatted and within
        the allowed skew of the current time.
    FN)r   r   r	   )r   Úallowed_skewÚnowÚstampÚ_ÚpastÚfuturer   r   r   r   -   s   ÿr   c                 C   s6   t  dt¡}| du rtƒ }nt| ƒ}tt|ƒ}|| S )a  Generate a nonce with the current timestamp

    @param when: Unix timestamp representing the issue time of the
        nonce. Defaults to the current time.
    @type when: int

    @returntype: str
    @returns: A string that should be usable as a one-way nonce

    @see: time
    é   N)r   ÚrandomStringÚNONCE_CHARSr   r   r   )ÚwhenÚsaltÚtÚtime_strr   r   r   r   R   s   
r   )N)Ú__all__Úopenidr   r	   r   r   r   Úcalendarr
   ÚstringÚascii_lettersÚdigitsr   ÚSKEWr   Úlenr   r   r   r   r   r   r   r   Ú<module>   s    %