a
    ù!fÿ   ã                   @   s>  d Z ddlZddlZddlZddlmZ ddlmZ ejZejZdZ	dZ
dZe e¡ZG dd„ deƒZd	d
„ Zz"ddlmZ ejZejZejZW n ey®   dZdZeZY n0 zddlmZ ejZejZW n eyæ   dZdZY n0 eröeZeZnereZeZneZeZddd„Zdd„ Zdd„ Zdd„ Zddd„Z dS )z)Crypto-related routines for oauth2client.é    N)Ú_helpers)Ú_pure_python_crypti,  i€Q c                   @   s   e Zd ZdZdS )ÚAppIdentityErrorz!Error to indicate crypto failure.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r	   r	   úS/var/www/html/python-backend/venv/lib/python3.9/site-packages/oauth2client/crypt.pyr   $   s   r   c                  O   s   t dƒ‚d S )Nz#pkcs12_key_as_pem requires OpenSSL.)ÚNotImplementedError)ÚargsÚkwargsr	   r	   r
   Ú_bad_pkcs12_key_as_pem(   s    r   )Ú_openssl_crypt)Ú_pycrypto_cryptc                 C   sv   dddœ}|dur||d< t  t  |¡¡t  t  |¡¡g}d |¡}|  |¡}| t  |¡¡ t t|ƒ¡ d |¡S )aR  Make a signed JWT.

    See http://self-issued.info/docs/draft-jones-json-web-token.html.

    Args:
        signer: crypt.Signer, Cryptographic signer.
        payload: dict, Dictionary of data to convert to JSON and then sign.
        key_id: string, (Optional) Key ID header.

    Returns:
        string, The JWT for the payload.
    ZJWTZRS256)ÚtypÚalgNÚkidó   .)	r   Z_urlsafe_b64encodeZ_json_encodeÚjoinÚsignÚappendÚloggerÚdebugÚstr)ZsignerÚpayloadZkey_idÚheaderÚsegmentsZsigning_inputÚ	signaturer	   r	   r
   Úmake_signed_jwtJ   s    
þ

r   c                 C   s6   |D ]$}t j|dd}| | |¡r dS qtdƒ‚dS )a€  Verifies signed content using a list of certificates.

    Args:
        message: string or bytes, The message to verify.
        signature: string or bytes, The signature on the message.
        certs: iterable, certificates in PEM format.

    Raises:
        AppIdentityError: If none of the certificates can verify the message
                          against the signature.
    T)Zis_x509_certNzInvalid token signature)ÚVerifierZfrom_stringÚverifyr   )Úmessager   ÚcertsÚpemZverifierr	   r	   r
   Ú_verify_signaturei   s
    r%   c                 C   sJ   |du rdS |   d¡}|du r,td | ¡ƒ‚||krFtd ||| ¡ƒ‚dS )aA  Checks audience field from a JWT payload.

    Does nothing if the passed in ``audience`` is null.

    Args:
        payload_dict: dict, A dictionary containing a JWT payload.
        audience: string or NoneType, an audience to check for in
                  the JWT payload.

    Raises:
        AppIdentityError: If there is no ``'aud'`` field in the payload
                          dictionary but there is an ``audience`` to check.
        AppIdentityError: If the ``'aud'`` field in the payload dictionary
                          does not match the ``audience``.
    NZaudzNo aud field in token: {0}z Wrong recipient, {0} != {1}: {2})Úgetr   Úformat)Úpayload_dictÚaudienceZaudience_in_payloadr	   r	   r
   Ú_check_audience~   s    
ÿÿr*   c                 C   s®   t t ¡ ƒ}|  d¡}|du r,td | ¡ƒ‚|  d¡}|du rLtd | ¡ƒ‚||t krftd | ¡ƒ‚|t }||k rˆtd ||| ¡ƒ‚|t }||krªtd ||| ¡ƒ‚dS )	aÜ  Verifies the issued at and expiration from a JWT payload.

    Makes sure the current time (in UTC) falls between the issued at and
    expiration for the JWT (with some skew allowed for via
    ``CLOCK_SKEW_SECS``).

    Args:
        payload_dict: dict, A dictionary containing a JWT payload.

    Raises:
        AppIdentityError: If there is no ``'iat'`` field in the payload
                          dictionary.
        AppIdentityError: If there is no ``'exp'`` field in the payload
                          dictionary.
        AppIdentityError: If the JWT expiration is too far in the future (i.e.
                          if the expiration would imply a token lifetime
                          longer than what is allowed.)
        AppIdentityError: If the token appears to have been issued in the
                          future (up to clock skew).
        AppIdentityError: If the token appears to have expired in the past
                          (up to clock skew).
    ZiatNzNo iat field in token: {0}ÚexpzNo exp field in token: {0}z exp field too far in future: {0}z$Token used too early, {0} < {1}: {2}z#Token used too late, {0} > {1}: {2})ÚintÚtimer&   r   r'   ÚMAX_TOKEN_LIFETIME_SECSÚCLOCK_SKEW_SECS)r(   ÚnowZ	issued_atZ
expirationZearliestZlatestr	   r	   r
   Ú_verify_time_rangeš   s2    
ÿ
ÿÿÿÿr1   c           	      C   s¬   t  | ¡} |  d¡dkr&td | ¡ƒ‚|  d¡\}}}|d | }t  |¡}t  |¡}zt t  	|¡¡}W n   td |¡ƒ‚Y n0 t
||| ¡ ƒ t|ƒ t||ƒ |S )aþ  Verify a JWT against public certs.

    See http://self-issued.info/docs/draft-jones-json-web-token.html.

    Args:
        jwt: string, A JWT.
        certs: dict, Dictionary where values of public keys in PEM format.
        audience: string, The audience, 'aud', that this JWT should contain. If
                  None then the JWT's 'aud' parameter is not verified.

    Returns:
        dict, The deserialized JSON payload in the JWT.

    Raises:
        AppIdentityError: if any checks are failed.
    r   é   z&Wrong number of segments in token: {0}zCan't parse token: {0})r   Ú	_to_bytesÚcountr   r'   ÚsplitZ_urlsafe_b64decodeÚjsonÚloadsZ_from_bytesr%   Úvaluesr1   r*   )	Zjwtr#   r)   r   r   r   Zmessage_to_signZpayload_bytesr(   r	   r	   r
   Úverify_signed_jwt_with_certsÏ   s"    
ÿ


r9   )N)N)!r   r6   Úloggingr-   Zoauth2clientr   r   Z	RsaSignerZRsaVerifierr/   ZAUTH_TOKEN_LIFETIME_SECSr.   Ú	getLoggerr   r   Ú	Exceptionr   r   r   ZOpenSSLSignerZOpenSSLVerifierZpkcs12_key_as_pemÚImportErrorr   ZPyCryptoSignerZPyCryptoVerifierZSignerr    r   r%   r*   r1   r9   r	   r	   r	   r
   Ú<module>   sT   





5