
    n                         S r SSKrSSKrSSKrSSKrSSKrSSKrSSKJr  SSKJr  SSKJ	r	  SSKJ
r
  SrSrS	r " S
 S\R                  5      rS r " S S\5      rg)z/oauth2client Service account credentials class.    N)_helpers)client)crypt)	transport
notasecret_private_key_pkcs12a
  
This library only implements PKCS#12 support via the pyOpenSSL library.
Either install pyOpenSSL, or please convert the .p12 file
to .pem format:
    $ cat key.p12 | \
    >   openssl pkcs12 -nodes -nocerts -passin pass:notasecret | \
    >   openssl rsa > key.pem
c                   .  ^  \ rS rSrSrSr \" S/5      \R                  R                  -  r	 Sr
SrSrSSSS\R                  \R                  4U 4S jjrSU 4S jjr\ SS	 j5       r\  SS
 j5       r\  SS j5       r\SS\R                  \R                  4S j5       r\SS\R                  \R                  4S j5       r\SS\R                  \R                  4S j5       rS rS r\S 5       r\S 5       r\S 5       rS rS r S r!S r"Sr#U =r$$ )ServiceAccountCredentials+   aI  Service Account credential for OAuth 2.0 signed JWT grants.

Supports

* JSON keyfile (typically contains a PKCS8 key stored as
  PEM text)
* ``.p12`` key (stores PKCS12 key and certificate)

Makes an assertion to server using a signed JWT assertion in exchange
for an access token.

This credential does not require a flow to instantiate because it
represents a two legged flow, and therefore has all of the required
information to generate and refresh its own access tokens.

Args:
    service_account_email: string, The email associated with the
                           service account.
    signer: ``crypt.Signer``, A signer which can be used to sign content.
    scopes: List or string, (Optional) Scopes to use when acquiring
            an access token.
    private_key_id: string, (Optional) Private key identifier. Typically
                    only used with a JSON keyfile. Can be sent in the
                    header of a JWT token assertion.
    client_id: string, (Optional) Client ID for the project that owns the
               service account.
    user_agent: string, (Optional) User agent to use when sending
                request.
    token_uri: string, URI for token endpoint. For convenience defaults
               to Google's endpoints but any OAuth 2.0 provider can be
               used.
    revoke_uri: string, URI for revoke endpoint.  For convenience defaults
               to Google's endpoints but any OAuth 2.0 provider can be
               used.
    kwargs: dict, Extra key-value pairs (both strings) to send in the
            payload body when making an assertion.
  _signerN c	                    > [         [        U ]  S XgUS9  Xl        X l        [
        R                  " U5      U l        X@l        XPl	        X`l
        Xl        g )N)
user_agent	token_uri
revoke_uri)superr
   __init___service_account_emailr   r   scopes_to_string_scopes_private_key_id	client_id_user_agent_kwargs)selfservice_account_emailsignerscopesprivate_key_idr   r   r   r   kwargs	__class__s             ;platform/bq/third_party/oauth2client_4_0/service_account.pyr   "ServiceAccountCredentials.__init__`   sW     	'7Z! 	8 	# '<#008-"%    c                    > Uc   [         R                   " U R                  5      nUR                  [        5      nUb  [        R
                  " U5      U[        '   [        [        U ]#  XS9$ )a  Utility function that creates JSON repr. of a credentials object.

Over-ride is needed since PKCS#12 keys will not in general be JSON
serializable.

Args:
    strip: array, An array of names of members to exclude from the
           JSON.
    to_serialize: dict, (Optional) The properties for this object
                  that will be serialized. This allows callers to
                  modify before serializing.

Returns:
    string, a JSON representation of this instance, suitable to pass to
    from_json().
)to_serialize)	copy__dict__get_PKCS12_KEYbase64	b64encoder   r
   _to_json)r   stripr'   
pkcs12_valr"   s       r#   r.   "ServiceAccountCredentials._to_jsonw   se    " 99T]]3L!%%k2
!(.(8(8(DL%.> ? . 	.r%   c           
         UR                  S5      nU[        R                  :w  a  [        SUS[        R                  5      eUS   nUS   nUS   nUS   n	U(       d   UR                  S[        R
                  5      nU(       d   UR                  S	[        R                  5      n[        R                  R                  U5      n
U " XjUUXUS
9nX{l
        U$ )aq  Helper for factory constructors from JSON keyfile.

Args:
    keyfile_dict: dict-like object, The parsed dictionary-like object
                  containing the contents of the JSON keyfile.
    scopes: List or string, Scopes to use when acquiring an
            access token.
    token_uri: string, URI for OAuth 2.0 provider token endpoint.
               If unset and not present in keyfile_dict, defaults
               to Google's endpoints.
    revoke_uri: string, URI for OAuth 2.0 provider revoke endpoint.
               If unset and not present in keyfile_dict, defaults
               to Google's endpoints.

Returns:
    ServiceAccountCredentials, a credentials object created from
    the keyfile contents.

Raises:
    ValueError, if the credential type is not :data:`SERVICE_ACCOUNT`.
    KeyError, if one of the expected keys is not present in
        the keyfile.
typezUnexpected credentials typeExpectedclient_emailprivate_keyr    r   r   r   )r   r    r   r   r   )r*   r   SERVICE_ACCOUNT
ValueErroroauth2client_4_0GOOGLE_TOKEN_URIGOOGLE_REVOKE_URIr   Signerfrom_string_private_key_pkcs8_pem)clskeyfile_dictr   r   r   
creds_typer   private_key_pkcs8_pemr    r   r   credentialss               r#   _from_parsed_json_keyfile3ServiceAccountCredentials._from_parsed_json_keyfile   s    4 "%%f-
///:J')?)?A A !-^ < ,] ;%&67 -	$(()9)J)JLI%)),*:*L*LNJ ))*?@/)7$-%/1 .C*r%   c                     [        US5       n[        R                  " U5      nSSS5        U R                  WUUUS9$ ! , (       d  f       N = f)a  Factory constructor from JSON keyfile by name.

Args:
    filename: string, The location of the keyfile.
    scopes: List or string, (Optional) Scopes to use when acquiring an
            access token.
    token_uri: string, URI for OAuth 2.0 provider token endpoint.
               If unset and not present in the key file, defaults
               to Google's endpoints.
    revoke_uri: string, URI for OAuth 2.0 provider revoke endpoint.
               If unset and not present in the key file, defaults
               to Google's endpoints.

Returns:
    ServiceAccountCredentials, a credentials object created from
    the keyfile.

Raises:
    ValueError, if the credential type is not :data:`SERVICE_ACCOUNT`.
    KeyError, if one of the expected keys is not present in
        the keyfile.
rNr   r   )openjsonloadrD   )r?   filenamer   r   r   file_objclient_credentialss          r#   from_json_keyfile_name0ServiceAccountCredentials.from_json_keyfile_name   sR    4 (C H!%8!4 !,,-?7@8B - D 	D ! s	   >
Ac                 $    U R                  XUUS9$ )an  Factory constructor from parsed JSON keyfile.

Args:
    keyfile_dict: dict-like object, The parsed dictionary-like object
                  containing the contents of the JSON keyfile.
    scopes: List or string, (Optional) Scopes to use when acquiring an
            access token.
    token_uri: string, URI for OAuth 2.0 provider token endpoint.
               If unset and not present in keyfile_dict, defaults
               to Google's endpoints.
    revoke_uri: string, URI for OAuth 2.0 provider revoke endpoint.
               If unset and not present in keyfile_dict, defaults
               to Google's endpoints.

Returns:
    ServiceAccountCredentials, a credentials object created from
    the keyfile.

Raises:
    ValueError, if the credential type is not :data:`SERVICE_ACCOUNT`.
    KeyError, if one of the expected keys is not present in
        the keyfile.
rH   )rD   )r?   r@   r   r   r   s        r#   from_json_keyfile_dict0ServiceAccountCredentials.from_json_keyfile_dict   s&    4 ,,\7@8B - D 	Dr%   c                     Uc  [         n[        R                  [        R                  La  [	        [
        5      e[        R                  R                  UU5      nU " XUXVS9nX(l        X8l        U$ )a  Factory constructor from JSON keyfile.

Args:
    service_account_email: string, The email associated with the
                           service account.
    private_key_pkcs12: string, The contents of a PKCS#12 keyfile.
    private_key_password: string, (Optional) Password for PKCS#12
                          private key. Defaults to ``notasecret``.
    scopes: List or string, (Optional) Scopes to use when acquiring an
            access token.
    token_uri: string, URI for token endpoint. For convenience defaults
               to Google's endpoints but any OAuth 2.0 provider can be
               used.
    revoke_uri: string, URI for revoke endpoint. For convenience
                defaults to Google's endpoints but any OAuth 2.0
                provider can be used.

Returns:
    ServiceAccountCredentials, a credentials object created from
    the keyfile.

Raises:
    NotImplementedError if pyOpenSSL is not installed / not the
    active crypto library.
)r   r   r   )	_PASSWORD_DEFAULTr   r<   OpenSSLSignerNotImplementedError_PKCS12_ERRORr=   r   _private_key_password)	r?   r   private_key_pkcs12private_key_passwordr   r   r   r   rC   s	            r#   _from_p12_keyfile_contents4ServiceAccountCredentials._from_p12_keyfile_contents   sm    >  '#4 <<u222%m44))*<*>@/$-F*<',@)r%   c           	          [        US5       nUR                  5       nSSS5        U R                  UWX4XVS9$ ! , (       d  f       N = f)a  Factory constructor from JSON keyfile.

Args:
    service_account_email: string, The email associated with the
                           service account.
    filename: string, The location of the PKCS#12 keyfile.
    private_key_password: string, (Optional) Password for PKCS#12
                          private key. Defaults to ``notasecret``.
    scopes: List or string, (Optional) Scopes to use when acquiring an
            access token.
    token_uri: string, URI for token endpoint. For convenience defaults
               to Google's endpoints but any OAuth 2.0 provider can be
               used.
    revoke_uri: string, URI for revoke endpoint. For convenience
                defaults to Google's endpoints but any OAuth 2.0
                provider can be used.

Returns:
    ServiceAccountCredentials, a credentials object created from
    the keyfile.

Raises:
    NotImplementedError if pyOpenSSL is not installed / not the
    active crypto library.
rbNr[   r   r   r   )rI   readr\   )	r?   r   rL   r[   r   r   r   rM   rZ   s	            r#   from_p12_keyfile*ServiceAccountCredentials.from_p12_keyfile+  sN    > (D!X!) "--!#5!5 . 8 	8 "!s	   8
Ac           	      D    UR                  5       nU R                  XX4XVS9$ )a  Factory constructor from JSON keyfile.

Args:
    service_account_email: string, The email associated with the
                           service account.
    file_buffer: stream, A buffer that implements ``read()``
                 and contains the PKCS#12 key contents.
    private_key_password: string, (Optional) Password for PKCS#12
                          private key. Defaults to ``notasecret``.
    scopes: List or string, (Optional) Scopes to use when acquiring an
            access token.
    token_uri: string, URI for token endpoint. For convenience defaults
               to Google's endpoints but any OAuth 2.0 provider can be
               used.
    revoke_uri: string, URI for revoke endpoint. For convenience
                defaults to Google's endpoints but any OAuth 2.0
                provider can be used.

Returns:
    ServiceAccountCredentials, a credentials object created from
    the keyfile.

Raises:
    NotImplementedError if pyOpenSSL is not installed / not the
    active crypto library.
r`   )ra   r\   )r?   r   file_bufferr[   r   r   r   rZ   s           r#   from_p12_keyfile_buffer1ServiceAccountCredentials.from_p12_keyfile_bufferQ  s4    > )--/--!!5 . 8 	8r%   c                 4   [        [        R                  " 5       5      n[        R                  U R                  UXR
                  -   U R                  S.nUR                  U R                  5        [        R                  " U R                  UU R                  S9$ )z8Generate the assertion that will be used in the request.)audscopeiatexpisskey_id)inttimer9   r:   r   MAX_TOKEN_LIFETIME_SECSr   updater   r   make_signed_jwtr   r   )r   nowpayloads      r#   _generate_assertion-ServiceAccountCredentials._generate_assertionv  sy    $))+#44\\555..
 	t||$$$T\\7,0,@,@B 	Br%   c                 P    U R                   U R                  R                  U5      4$ )a  Cryptographically sign a blob (of bytes).

Implements abstract method
:meth:`oauth2client_4_0.client.AssertionCredentials.sign_blob`.

Args:
    blob: bytes, Message to be signed.

Returns:
    tuple, A pair of the private key ID used to sign the blob and
    the signed contents.
)r   r   sign)r   blobs     r#   	sign_blob#ServiceAccountCredentials.sign_blob  s$     ##T\\%6%6t%<<<r%   c                     U R                   $ )ztGet the email for the current service account.

Returns:
    string, The email associated with the service account.
)r   r   s    r#   r   /ServiceAccountCredentials.service_account_email  s     ***r%   c                 b    SU R                   U R                  U R                  U R                  S.$ )Nservice_account)r3   r5   r    r6   r   )r   r   r>   r   r   s    r#   serialization_data,ServiceAccountCredentials.serialization_data  s3     & 77"2266
 	
r%   c                    [        U[        5      (       d*  [        R                  " [        R
                  " U5      5      nSnUR                  [        5      nSnUc%  US   n[        R                  R                  U5      nO:[        R                  " U5      nUS   n[        R                  R                  X45      nU " US   U4US   US   US   US   S	.US
   D6nUb  X&l        Ub  X6l        Ub  XFl        US   Ul        US   Ul        US   Ul        US   Ul        UR                  SS5      nUb3  [(        R(                  R+                  U[,        R.                  5      Ul        U$ )a  Deserialize a JSON-serialized instance.

Inverse to :meth:`to_json`.

Args:
    json_data: dict or string, Serialized JSON (as a string or an
               already parsed dictionary) representing a credential.

Returns:
    ServiceAccountCredentials from the serialized data.
Nr>   rY   r   r   r   r   r   r   r    r   r   r   invalidaccess_tokenr   r   token_expiry)
isinstancedictrJ   loadsr   _from_bytesr*   r+   r   r<   r=   r,   	b64decoder>   r   rY   r   r   r   r   datetimestrptimer   EXPIRY_FORMATr   )r?   	json_datarB   r0   passwordr   rC   r   s           r#   	from_json#ServiceAccountCredentials.from_json  s    )T**

8#7#7	#BCI $]];/
$-.F$G!\\--.CDF
  ))*5J !89H\\--jCF./
 Y'$%67, /
 	"
 !,1F.!.8+08-'	2#,^#<  )+ 6!*<!8 }}^T:#'/'8'8'A'Af22(4K$r%   c                 $    U R                   (       + $ N)r   r   s    r#   create_scoped_required0ServiceAccountCredentials.create_scoped_required  s    <<r%   c                 Z   U R                   " U R                  U R                  4UU R                  U R                  U R
                  S.U R                  D6nU R                  Ul        U R                  Ul        U R                  Ul	        U R                  Ul
        U R                  Ul        U$ )Nr   )r"   r   r   r   r   r   r   r   r   r>   r   rY   )r   r   results      r#   create_scoped'ServiceAccountCredentials.create_scoped  s     ; ; $0'-/3/C/C*...+/+;+;0 #',,0  >> OO(,(C(C%%)%=%="'+'A'A$r%   c                    [        U R                  5      nUR                  U5        U R                  " U R                  U R
                  4U R                  U R                  U R                  U R                  S.UD6nU R                  Ul
        U R                  Ul        U R                  Ul        U R                  Ul        U R                  Ul        U$ )a  Create credentials that specify additional claims.

Args:
    claims: dict, key-value pairs for claims.

Returns:
    ServiceAccountCredentials, a copy of the current service account
    credentials with updated claims to use when obtaining access
    tokens.
r   )r   r   rs   r"   r   r   r   r   r   r   r   r   r>   r   rY   )r   claims
new_kwargsr   s       r#   create_with_claims,ServiceAccountCredentials.create_with_claims  s     $,,'
&! ; ; $.'+||/3/C/C*...+/+;+;. #-.  >> OO(,(C(C%%)%=%="'+'A'A$r%   c                 (    U R                  SU05      $ )a  Create credentials that act as domain-wide delegation of authority.

Use the ``sub`` parameter as the subject to delegate on behalf of
that user.

For example::

  >>> account_sub = 'foo@email.com'
  >>> delegate_creds = creds.create_delegated(account_sub)

Args:
    sub: string, An email address that this service account will
         act on behalf of (via domain-wide delegation).

Returns:
    ServiceAccountCredentials, a copy of the current service account
    updated to act on behalf of ``sub``.
sub)r   )r   r   s     r#   create_delegated*ServiceAccountCredentials.create_delegated  s    & &&s|44r%   )r   r   r   r   r   r   r   r   NN)r   NN)%__name__
__module____qualname____firstlineno____doc__rr   	frozensetr   AssertionCredentialsNON_SERIALIZED_MEMBERSr>   r   rY   r9   r:   r;   r   r.   classmethodrD   rO   rR   r\   rb   rf   rw   r|   propertyr   r   r   r   r   r   r   __static_attributes____classcell__r"   s   @r#   r
   r
   +   s   $L #; 	9+##::	;  K " 
  $ +<<,>>..2 =A/ /b 57:>D D> 9;:>D D:  9=R-=-N-N.>.P.P	( (T .22#3#D#D$4$F$F#8 #8J 59"*:*K*K+;+M+M"8 "8HB= + + 
 
 4 4l 65 5r%   r
   c                 r    [         R                   " SSS5      nX-
  nUR                  S-  UR                  -   $ )Ni     iQ )r   daysseconds)utc_timeepoch
time_deltas      r#   _datetime_to_secsr   !  s:     dAq)E!J??U"Z%7%777r%   c                      ^  \ rS rSrSrSr SSSS\R                  \R                  S4U 4S jjr	S r
SS jrS rS	 r\R                  \R                  4S
 jrS rS rSS jrSrU =r$ )_JWTAccessCredentialsi)  zSelf signed JWT credentials.

Makes an assertion to server using a self signed JWT from service account
credentials.  These credentials do NOT use OAuth 2.0 and instead
authenticate directly.
r   Nc
           	      H   > U	c  0 n	[         [        U ]
  " UU4UUUUUS.U	D6  g )N)r    r   r   r   r   )r   r   r   )r   r   r   r   r    r   r   r   r   additional_claimsr"   s             r#   r   _JWTAccessCredentials.__init__3  sG     $ "#T3!	! *!!	!  	!r%   c                 2    [         R                  " X5        U$ )ai  Authorize an httplib2.Http instance with a JWT assertion.

Unless specified, the 'aud' of the assertion will be the base
uri of the request.

Args:
    http: An instance of ``httplib2.Http`` or something that acts
          like it.
Returns:
    A modified instance of http that was passed in.
Example::
    h = httplib2.Http()
    h = credentials.authorize(h)
)r   wrap_http_for_jwt_accessr   https     r#   	authorize_JWTAccessCredentials.authorizeI  s     	**46r%   c                 "   Uc\  U R                   b  U R                  (       a  U R                  S5        [        R                  " U R                   U R                  5       S9$ U R                  U5      u  p4[        R                  " X0R                  S9$ )zCreate a signed jwt.

Args:
    http: unused
    additional_claims: dict, additional claims to add to
        the payload of the JWT.
Returns:
    An AccessTokenInfo with the signed jwt
N)r   
expires_in)r   access_token_expiredrefreshr   AccessTokenInfo_expires_in_create_token_MAX_TOKEN_LIFETIME_SECS)r   r   r   tokenunused_expirys        r#   get_access_token&_JWTAccessCredentials.get_access_token[  s     $  (D,E,ET")),,9I9I9KM M $(#5#56G#H E)) -J-JL Lr%   c                     g)z*Cannot revoke JWTAccessCredentials tokens.N r   s     r#   revoke_JWTAccessCredentials.revokep  s    r%   c                     g)NTr   r   s    r#   r   ,_JWTAccessCredentials.create_scoped_requiredt  s    r%   c           
      Z   [        U R                  U R                  4UU R                  U R                  U R
                  UUS.U R                  D6nU R                  b  U R                  Ul        U R                  b  U R                  Ul        U R                  b  U R                  Ul	        U$ )N)r   r    r   r   r   r   )
r
   r   r   r   r   r   r   r>   r   rY   )r   r   r   r   r   s        r#   r   #_JWTAccessCredentials.create_scopedx  s     +4+F+F+/<<;28:>:N:N59^^6:6F6F5>6@; .2\\; &&2,0,G,GF)##/)-)A)AF&%%1+/+E+EF(r%   c                 &    U R                  S5        g)zRefreshes the access_token.

The HTTP object is unused since no request needs to be made to
get a new token, it can just be generated locally.

Args:
    http: unused HTTP object
N)_refreshr   s     r#   r   _JWTAccessCredentials.refresh  s     	dr%   c                 >    U R                  5       u  U l        U l        g)z@Refreshes the access_token.

Args:
    http: unused HTTP object
N)r   r   r   r   s     r#   r   _JWTAccessCredentials._refresh  s     04/A/A/C,4,r%   c                    [         R                  " 5       n[        R                  " U R                  S9nX#-   n[        U5      [        U5      U R                  U R                  S.nUR                  U R                  5        Ub  UR                  U5        [        R                  " U R                  UU R                  S9nUR                  S5      U4$ )N)r   )rk   rl   rm   r   rn   ascii)r   _UTCNOWr   	timedeltar   r   r   rs   r   r   rt   r   r   decode)r   r   ru   lifetimeexpiryrv   jwts          r#   r   #_JWTAccessCredentials._create_token  s    nn%%d.K.KL$S)$V,....	
 	t||$(NN,-##DLL'+/+?+?Azz'"F**r%   )r   r   r   r   )r   r   r   r   r   r   r9   r:   r;   r   r   r   r   r   r   r   r   r   r   r   r   s   @r#   r   r   )  s~      $;
  $ +<<,>>#'!,$L* /?.O.O!1!C!C(	D+ +r%   r   )r   r,   r(   r   rJ   rq   r9   r   r   r   r   rU   r+   rX   r   r
   r   r   r   r%   r#   <module>r      se     6       % # " & ! #s5 ; ; s5l8E+5 E+r%   