
    F                         S r SSKrSSKrSSKrSSKJr  SrSrSr\R                  " \
5      r " S S\5      rS r SS	KJr  \R                   r\R"                  r\R$                  r\(       a  \r\rO SS
KJr  \R.                  r\R0                  rSS jrS rS rS rSS jr g! \ a	    SrSr\r NMf = f! \ a!    SSKJr  \R4                  r\R6                  r NLf = f)z)Crypto-related routines for oauth2client.    N)_helpersi,  iQ c                       \ rS rSrSrSrg)AppIdentityError    z!Error to indicate crypto failure. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       %lib/third_party/oauth2client/crypt.pyr   r       s    +r   r   c                      [        S5      e)Nz#pkcs12_key_as_pem requires OpenSSL.)NotImplementedError)argskwargss     r   _bad_pkcs12_key_as_pemr   $   s    
C
DDr   )_openssl_crypt)_pycrypto_crypt)_pure_python_cryptc                    SSS.nUb  X#S'   [         R                  " [         R                  " U5      5      [         R                  " [         R                  " U5      5      /nSR                  U5      nU R	                  U5      nUR                  [         R                  " U5      5        [        R                  [        U5      5        SR                  U5      $ )a2  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.
JWTRS256)typalgkid   .)	r   _urlsafe_b64encode_json_encodejoinsignappendloggerdebugstr)signerpayloadkey_idheadersegmentssigning_input	signatures          r   make_signed_jwtr.   A   s     7+Fu 	##H$9$9&$AB##H$9$9'$BCH IIh'MM*IOOH//	:;
LLX99Xr   c                     U H/  n[         R                  USS9nUR                  X5      (       d  M/    g   [        S5      e)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)is_x509_certNzInvalid token signature)Verifierfrom_stringverifyr   )messager-   certspemverifiers        r   _verify_signaturer8   `   sC     ''$'???7..  4
55r   c                     Uc  gU R                  S5      nUc  [        SR                  U 5      5      eX!:w  a  [        SR                  X!U 5      5      eg)a  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``.
NaudzNo aud field in token: {0}z Wrong recipient, {0} != {1}: {2})getr   format)payload_dictaudienceaudience_in_payloads      r   _check_audiencer@   u   sj      &**51"(//=? 	?&AHH< 9 : 	: 'r   c                    [        [        R                  " 5       5      nU R                  S5      nUc  [        SR	                  U 5      5      eU R                  S5      nUc  [        SR	                  U 5      5      eX1[
        -   :  a  [        SR	                  U 5      5      eU[        -
  nX:  a  [        SR	                  XU 5      5      eU[        -   nX:  a  [        SR	                  XU 5      5      eg)	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).
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=   now	issued_at
expirationearliestlatests         r   _verify_time_rangerM      s
   0 diik
C   'I(//=? 	?!!%(J(//=? 	? 222.55lCE 	E ?*H
~ELL< ) * 	* /)F
|DKK ' ( 	( r   c                    [         R                  " U 5      n U R                  S5      S:w  a  [        SR	                  U 5      5      eU R                  S5      u  p4nUS-   U-   n[         R                  " U5      n[         R                  " U5      n [        R                  " [         R                  " U5      5      n[        XeUR                  5       5        [        U5        [        X5        U$ !   [        SR	                  U5      5      e= f)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<   split_urlsafe_b64decodejsonloads_from_bytesr8   valuesrM   r@   )	jwtr5   r>   r*   r(   r-   message_to_signpayload_bytesr=   s	            r   verify_signed_jwt_with_certsr[      s    " 

S
!C
yy!4;;C@B 	B "%4FYtmg-O++I6I //8MPzz("6"6}"EF
 o%,,.A |$ L+P8??NOOs   *C+ +D)N)!r   rT   loggingrE   oauth2clientr   rG   AUTH_TOKEN_LIFETIME_SECSrF   	getLoggerr   r$   	Exceptionr   r   r   OpenSSLSignerOpenSSLVerifierpkcs12_key_as_pemImportErrorSignerr1   r   PyCryptoSignerPyCryptoVerifierr   	RsaSignerRsaVerifierr.   r8   r@   rM   r[   r   r   r   <module>rj      s    0    !   			8	$,y ,E/+"00M$44O&88 FH20 //"33>6*:82(j+s  /OM./  23#--%112s#   *B% 3B7 %B43B47$CC