;iQadZddlmZddlZddlZddlZddlZddlmZddlmZddlm Z ddlm Z ddlm Z dd l m Z ejeZd Zd ZGd d e je jZGdde jZdS)aOAuth 2.0 Credentials. This module provides credentials based on OAuth 2.0 access and refresh tokens. These credentials usually access resources on behalf of a user (resource owner). Specifically, this is intended to use access tokens acquired using the `Authorization Code grant`_ and can refresh those tokens using a optional `refresh token`_. Obtaining the initial access and refresh token is outside of the scope of this module. Consult `rfc6749 section 4.1`_ for complete details on the Authorization Code grant flow. .. _Authorization Code grant: https://tools.ietf.org/html/rfc6749#section-1.3.1 .. _refresh token: https://tools.ietf.org/html/rfc6749#section-6 .. _rfc6749 section 4.1: https://tools.ietf.org/html/rfc6749#section-4.1 )datetimeN) _cloud_sdk)_helpers) credentials) exceptions)metrics)reauthz#https://oauth2.googleapis.com/tokenz'https://oauth2.googleapis.com/tokeninfoceZdZdZddddddddddddddejdffd ZdZdZe dZ e dZ e d Z e d Z e d Ze d Ze d Ze dZe dZe dZejdZe dZdZejejdZejejdZejejdZdZejej dZ!dZ"ejejdZ#e$ddZ%e$ddZ&ddZ'xZ(S) CredentialsaCredentials using OAuth 2.0 access and refresh tokens. The credentials are considered immutable except the tokens and the token expiry, which are updated after refresh. If you want to modify the quota project, use :meth:`with_quota_project` or :: credentials = credentials.with_quota_project('myproject-123') Reauth is disabled by default. To enable reauth, set the `enable_reauth_refresh` parameter to True in the constructor. Note that reauth feature is intended for gcloud to use only. If reauth is enabled, `pyu2f` dependency has to be installed in order to use security key reauth feature. Dependency can be installed via `pip install pyu2f` or `pip install google-auth[reauth]`. NFcntt|||_| |_||_||_||_||_||_ ||_ ||_ ||_ | |_ | |_| |_| |_||_|p t$j|_|pd|_d|_dS)a Args: token (Optional(str)): The OAuth 2.0 access token. Can be None if refresh information is provided. refresh_token (str): The OAuth 2.0 refresh token. If specified, credentials can be refreshed. id_token (str): The Open ID Connect ID Token. token_uri (str): The OAuth 2.0 authorization server's token endpoint URI. Must be specified for refresh, can be left as None if the token can not be refreshed. client_id (str): The OAuth 2.0 client ID. Must be specified for refresh, can be left as None if the token can not be refreshed. client_secret(str): The OAuth 2.0 client secret. Must be specified for refresh, can be left as None if the token can not be refreshed. scopes (Sequence[str]): The scopes used to obtain authorization. This parameter is used by :meth:`has_scopes`. OAuth 2.0 credentials can not request additional scopes after authorization. The scopes must be derivable from the refresh token if refresh information is provided (e.g. The refresh token scopes are a superset of this or contain a wild card scope like 'https://www.googleapis.com/auth/any-api'). default_scopes (Sequence[str]): Default scopes passed by a Google client library. Use 'scopes' for user-defined scopes. quota_project_id (Optional[str]): The project ID used for quota and billing. This project may be different from the project used to create the credentials. rapt_token (Optional[str]): The reauth Proof Token. refresh_handler (Optional[Callable[[google.auth.transport.Request, Sequence[str]], [str, datetime]]]): A callable which takes in the HTTP request callable and the list of OAuth scopes and when called returns an access token string for the requested scopes and its expiry datetime. This is useful when no refresh tokens are provided and tokens are obtained by calling some external process on demand. It is particularly useful for retrieving downscoped tokens from a token broker. enable_reauth_refresh (Optional[bool]): Whether reauth refresh flow should be used. This flag is for gcloud to use only. granted_scopes (Optional[Sequence[str]]): The scopes that were consented/granted by the user. This could be different from the requested scopes and it could be empty if granted and requested scopes were same. trust_boundary (str): String representation of trust boundary meta. universe_domain (Optional[str]): The universe domain. The default universe domain is googleapis.com. account (Optional[str]): The account associated with the credential. N)superr __init__tokenexpiry_refresh_token _id_token_scopes_default_scopes_granted_scopes _token_uri _client_id_client_secret_quota_project_id _rapt_tokenrefresh_handler_enable_reauth_refresh_trust_boundaryrDEFAULT_UNIVERSE_DOMAIN_universe_domain_account_cred_file_path)selfr refresh_tokenid_token token_uri client_id client_secretscopesdefault_scopesquota_project_idr rapt_tokenrenable_reauth_refreshgranted_scopestrust_boundaryuniverse_domainaccount __class__s MC:\PYTHON\MyICR_Workspace\venv\Lib\site-packages\google/oauth2/credentials.pyrzCredentials.__init__JsB k4  ))+++  +! --##+!1%.&;#- / V;3V 2 #cT|j}d|vr|d=d|vr|d=|S)zA __getstate__ method must exist for the __setstate__ to be called This is identical to the default implementation. See https://docs.python.org/3.7/library/pickle.html#object.__setstate__ _refresh_handler_refresh_worker)__dict__copy)r# state_dicts r3 __getstate__zCredentials.__getstate__sG ]''))  + +-.  * *,-r4c|d|_|d|_|d|_|d|_|d|_|d|_|d|_|d|_|d |_ |d |_ |d |_ |d |_ |d |_ |d|_|dp tj|_|d|_d|_d|_|dd|_|dd|_dS)z\Credentials pickled with older versions of the class do not have all the attributes.rrrrrrrrrrrrrrr r"N_use_non_blocking_refreshFr!r )getrrrrrrrrrrrrrrrrr r"r6r7r=r!)r#ds r3 __setstate__zCredentials.__setstate__sUU7^^ eeHoo ee$455{++uuY''  uu%677 uu%677%% --%% --ee$455!"':!;!;55//&'ee,D&E&E# uu%677 EE$ % % L)L  !uu%677 $#)*/JE)R)R&j"-- r4c|jS)z+Optional[str]: The OAuth 2.0 refresh token.)rr#s r3r$zCredentials.refresh_token ""r4c|jS)z/Optional[str]: The OAuth 2.0 permission scopes.)rrBs r3r)zCredentials.scopess |r4c|jS)zWOptional[Sequence[str]]: The OAuth 2.0 permission scopes that were granted by the user.)rrBs r3r.zCredentials.granted_scopess ##r4c|jS)zOOptional[str]: The OAuth 2.0 authorization server's token endpoint URI.)rrBs r3r&zCredentials.token_uris r4c|jS)adOptional[str]: The Open ID Connect ID Token. Depending on the authorization server and the scopes requested, this may be populated when credentials are obtained and updated when :meth:`refresh` is called. This token is a JWT. It can be verified and decoded using :func:`google.oauth2.id_token.verify_oauth2_token`. )rrBs r3r%zCredentials.id_tokens ~r4c|jS)z'Optional[str]: The OAuth 2.0 client ID.)rrBs r3r'zCredentials.client_ids r4c|jS)z+Optional[str]: The OAuth 2.0 client secret.)rrBs r3r(zCredentials.client_secretrCr4cdS)zvFalse: OAuth 2.0 credentials have their scopes set when the initial token is requested and can not be changed.FrBs r3requires_scopeszCredentials.requires_scopess ur4c|jS)z&Optional[str]: The reauth Proof Token.)rrBs r3r,zCredentials.rapt_tokens r4c|jS)zReturns the refresh handler if available. Returns: Optional[Callable[[google.auth.transport.Request, Sequence[str]], [str, datetime]]]: The current refresh handler. )r6rBs r3rzCredentials.refresh_handlers $$r4cTt|s|td||_dS)a/Updates the current refresh handler. Args: value (Optional[Callable[[google.auth.transport.Request, Sequence[str]], [str, datetime]]]): The updated value of the refresh handler. Raises: TypeError: If the value is not a callable or None. Nz7The provided refresh_handler is not a callable or None.)callable TypeErrorr6)r#values r3rzCredentials.refresh_handler s5 W5#4UVV V %r4c|jS)zlstr: The user account associated with the credential. If the account is unknown an empty string is returned.)r!rBs r3r1zCredentials.accounts }r4c||j|j|j|j|j|j|j|j|j |j |j |j |j |j|j}|j|_|S)N)r$r%r&r'r(r)r*r.r+r,r-r/r0r1)r2rr$r%r&r'r(r)r*r.r+r,rrr r!r")r#creds r3 _make_copyzCredentials._make_copys~~ J,]nn,;..!2"&"=/ 1M  " $3 r4cN|jr|jdd}|jr |j|d<|SdS)Nzuser credentials)credential_sourcecredential_type principal)r"r1)r# cred_infos r3 get_cred_infozCredentials.get_cred_info3sE   %)%9#5I| 6)- +& tr4c<|}||_|SN)rVr)r#r+rUs r3with_quota_projectzCredentials.with_quota_project?s  !1 r4c<|}||_|Sr^)rVr)r#r&rUs r3with_token_urizCredentials.with_token_uriEs  # r4c<|}||_|S)zReturns a copy of these credentials with a modified account. Args: account (str): The account to set Returns: google.oauth2.credentials.Credentials: A new credentials instance. )rVr!)r#r1rUs r3 with_accountzCredentials.with_accountKs    r4c<|}||_|Sr^)rVr )r#r0rUs r3with_universe_domainz Credentials.with_universe_domainXs   / r4ctjSr^)rCRED_TYPE_USERrBs r3_metric_header_for_usagez$Credentials._metric_header_for_usage^s %%r4c |jtjkr,tjd|j|j|jn|j}|j|j r| ||\}}t|tstjdt|tstjdtj|tjz krtjd||_||_dS|j|j|j|jtjdt+j||j|j|j|j||j|j\}}}}}||_||_||_|d|_||_|rd |vrt7|} |d |_t7|j} | | z } | rFt<d d | dSdSdSdS) NaUser credential refresh is only supported in the default googleapis.com universe domain, but the current universe domain is {}. If you created the credential with an access token, it's likely that the provided token is expired now, please update your code with a valid token.)r)z3The refresh_handler returned token is not a string.z=The refresh_handler returned expiry is not a datetime object.zDThe credentials returned by the refresh_handler are already expired.zThe credentials do not contain the necessary fields need to refresh the access token. You must specify refresh_token, token_uri, client_id, and client_secret.)r)r,r-r%scopezUNot all requested scopes were granted by the authorization server, missing scopes {}., )!r rrr RefreshErrorformatrrrr isinstancestrrrutcnowREFRESH_THRESHOLDrrrrrr refresh_grantrrr>r frozensetsplitr_LOGGERwarningjoin) r#requestr)rr access_tokenr$grant_responser,requested_scopesr. scopes_requested_but_not_granteds r3refreshzCredentials.refreshas  K$G G G)>?Ef)??  "&!9t?S   &4+? & 000HHME6eS))  -Ifh//  -S  FX-G$GGG -'DJ DK F   '&&"*);    O   O  '"&"=      "  +'++J77%  g//(00 #1'#:#@#@#B#BD &t';<rstrptimerstriprtrrprqrnro_GOOGLE_OAUTH2_TOKEN_ENDPOINT)clsinfor) keys_neededmissingrs r3from_authorized_user_infoz%Credentials.from_authorized_user_infos"IJJ ((55  #VDIIg$6$677  (##  D& c""((--a02EFF_&&)CCF >h$..XXh''F&#&& +c**s((7##((?333hh{++((?33!XX&899xx --88$455 HH%677HHY++    r4ctj|dd5}tj|}|||cdddS#1swxYwYdS)aCreates a Credentials instance from an authorized user json file. Args: filename (str): The path to the authorized user json file. scopes (Sequence[str]): Optional list of scopes to include in the credentials. Returns: google.oauth2.credentials.Credentials: The constructed credentials. Raises: ValueError: If the file is not in the expected format. rzutf-8)encodingN)ioopenjsonloadr)rfilenamer) json_filedatas r3from_authorized_user_filez%Credentials.from_authorized_user_files WXsW 5 5 5 ?9Y''D00v>> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?s*AAAc j|j|j|j|j|j|j|j|j|jd }|j r|j dz|d<d| D} fd| D}tj |S)aUtility function that creates a JSON representation of a Credentials object. Args: strip (Sequence[str]): Optional list of members to exclude from the generated JSON. Returns: str: A JSON representation of this instance. When converted into a dictionary, it can be passed to from_authorized_user_info() to create a new credential instance. ) rr$r&r'r(r)r,r0r1rrci|] \}}||| Sr^rK).0kvs r3 z'Credentials.to_json..#s???A1r4Nc$i|] \}}|v || SrKrK)rrrstrips r3rz'Credentials.to_json..'s$DDDTQQe^^Aq^^^r4)rr$r&r'r(r)r,r r!r isoformatitemsrdumps)r#rpreps ` r3to_jsonzCredentials.to_jsonsZ!/!/k/#4}    ; ;![2244s:DN@????  DDDDTZZ\\DDDDz$r4r^))__name__ __module__ __qualname____doc__rrrr;r@propertyr$r)r.r&r%r'r(rLr,rsetterr1rVrcopy_docstringr r\CredentialsWithQuotaProjectr_CredentialsWithTokenUrirarcCredentialsWithUniverseDomainrerhr} classmethodrrr __classcell__r2s@r3r r 9sN&##;%S$S$S$S$S$S$j"...6##X#X$$X$X XX##X#X   X %%X% & & &X*X[455  65 X[DEEFE X[@AABA   X[FGGHG &&&X[455WW65Wr5 5 5 [5 n???[?&" " " " " " " " r4r ceZdZdZdfd ZdZejej dZ dZ ejej dZ xZS) UserAccessTokenCredentialsaAccess token credentials for user account. Obtain the access token for a given user account or the current active user account with the ``gcloud auth print-access-token`` command. Args: account (Optional[str]): Account to get the access token for. If not specified, the current active account will be used. quota_project_id (Optional[str]): The project ID used for quota and billing. Nctjdtt|||_||_dS)NzUserAccessTokenCredentials is deprecated, please use google.oauth2.credentials.Credentials instead. To use that credential type, simply run `gcloud auth application-default login` and let the client libraries pick up the application default credentials.)warningswarnrrrr!r)r#r1r+r2s r3rz#UserAccessTokenCredentials.__init__9sQ  L    ($//88::: !1r4c:|||jS)aCreate a new instance with the given account. Args: account (str): Account to get the access token for. Returns: google.oauth2.credentials.UserAccessTokenCredentials: The created credentials with the given account. r1r+)r2r)r#r1s r3rcz'UserAccessTokenCredentials.with_accountEs~~g@V~WWWr4c:||j|S)Nr)r2r!)r#r+s r3r_z-UserAccessTokenCredentials.with_quota_projectQs~~dmFV~WWWr4cBtj|j|_dS)a{Refreshes the access token. Args: request (google.auth.transport.Request): This argument is required by the base class interface but not used in this implementation, so just set it to `None`. Raises: google.auth.exceptions.UserAccessTokenError: If the access token refresh failed. N)rget_auth_access_tokenr!r)r#rxs r3r}z"UserAccessTokenCredentials.refreshUs 5dmDD r4cZ||||dSr^)r}apply)r#rxmethodurlheaderss r3before_requestz)UserAccessTokenCredentials.before_requestcs, W 7r4)NN)rrrrrrcrrrrr_r}r rrrs@r3rr,s   2 2 2 2 2 2 X X XX[DEEXXFEX E E EX[45565r4r)rrrrloggingr google.authrrrrr google.oauth2r getLoggerrrur"_GOOGLE_OAUTH2_TOKEN_INFO_ENDPOINTReadOnlyScopedrr rrKr4r3rsI& """""" ######"""""" ' H % %!F&O"p p p p p +,k.Up p p f:::::!H:::::r4