;i^UdZddlZddlmZddlZddlmZddlmZm Z ddlm Z ddlm Z ddl m Z dd lmZdd lmZd ZgZeeed <d ZGdde ZGddeZGddeZGddeZGddeZGddeZGddejZGddeZd$dZ Gd d!ejZ!Gd"d#eZ"dS)%zInterfaces for credentials.N)Enum)List)_helpersenvironment_vars) exceptions)metrics)_BaseCredentials)_LOGGER)RefreshThreadManagerzgoogleapis.comNO_OP_TRUST_BOUNDARY_LOCATIONS0x0ceZdZdZfdZedZedZedZedZ edZ dZ e j d Zd Zdd Zd ZdZdZdZxZS) CredentialsaQBase class for all credentials. All credentials have a :attr:`token` that is used for authentication and may also optionally set an :attr:`expiry` to indicate when the token will no longer be valid. Most credentials will be :attr:`invalid` until :meth:`refresh` is called. Credentials can do this automatically before the first HTTP request in :meth:`before_request`. Although the token and expiration will change as the credentials are :meth:`refreshed ` and used, credentials should be considered immutable. Various credentials will accept configuration such as private keys, scopes, and other options. These options are not changeable after construction. Some classes will provide mechanisms to copy the credentials with modifications such as :meth:`ScopedCredentials.with_scopes`. ctt|d|_ d|_ d|_ t |_ d|_t|_ dSNF) superr__init__expiry_quota_project_id_trust_boundaryDEFAULT_UNIVERSE_DOMAIN_universe_domain_use_non_blocking_refreshr _refresh_workerself __class__s KC:\PYTHON\MyICR_Workspace\venv\Lib\site-packages\google/auth/credentials.pyrzCredentials.__init__6sq k4  ))+++  B!%K# !8 */&355cj|jsdS|jtjz }tj|kS)a(Checks if the credentials are expired. Note that credentials can be invalid but not expired because Credentials with :attr:`expiry` set to None is considered to never expire. .. deprecated:: v2.24.0 Prefer checking :attr:`token_state` instead. F)rrREFRESH_THRESHOLDutcnow)r skewed_expirys rexpiredzCredentials.expiredIs7{ 5 h&@@   M11rc$|jduo|j S)zChecks the validity of the credentials. This is True if the credentials have a :attr:`token` and the token is not :attr:`expired`. .. deprecated:: v2.24.0 Prefer checking :attr:`token_state` instead. N)tokenr$rs rvalidzCredentials.valid[sz%:dl*::rc(|j tjS|j tjSt j|jk}|r tjSt j|jt jz k}|r tjStjS)z( See `:obj:`TokenState` ) r& TokenStateINVALIDrFRESHrr"r!STALE)rr$is_stales r token_statezCredentials.token_stategs : % % ; # #/##t{2  &% %?$$x7Q)QR  $# #rc|jS)z.Project to use for quota and billing purposes.)rr's rquota_project_idzCredentials.quota_project_id}s %%rc|jS)zThe universe domain value.)rr's runiverse_domainzCredentials.universe_domains $$rcdS)zThe credential information JSON. The credential information will be added to auth related error messages by client library. Returns: Mapping[str, str]: The credential information JSON. Nr's r get_cred_infozCredentials.get_cred_infos trc td)Refreshes the access token. Args: request (google.auth.transport.Request): The object used to make HTTP requests. Raises: google.auth.exceptions.RefreshError: If the credentials could not be refreshed. zRefresh must be implementedNotImplementedErrorrrequests rrefreshzCredentials.refreshs""?@@@rcdS)a The x-goog-api-client header for token usage metric. This header will be added to the API service requests in before_request method. For example, "cred-type/sa-jwt" means service account self signed jwt access token is used in the API service request authorization header. Children credentials classes need to override this method to provide the header value, if the token usage metric is needed. Returns: str: The x-goog-api-client header value. Nr5r's r_metric_header_for_usagez$Credentials._metric_header_for_usages trNcX||||jr |j|d<dSdS)zApply the token to the authentication header. Args: headers (Mapping): The HTTP request headers. token (Optional[str]): If specified, overrides the current access token. zx-goog-user-projectN)_applyr1rheadersr&s rapplyzCredentials.applysD GU###   C-1-BG) * * * C CrcB|js||dSdSN)r(r=r;s r_blocking_refreshzCredentials._blocking_refreshs.z " LL ! ! ! ! ! " "rcd}|jtjkr|j|| }|jtjks|r0|||jdSdSr)r/r*r-r start_refreshr+r= clear_error)rr<use_blocking_refresh_fallbacks r_non_blocking_refreshz!Credentials._non_blocking_refreshs(-%  z/ / /040D0R0Rg11- )  z1 1 15R 1 LL ! ! !  , , . . . . . 2 1rc|jr||n||tj||||dS)aPerforms credential-specific before request logic. Refreshes the credentials if necessary, then calls :meth:`apply` to apply the token to the authentication header. Args: request (google.auth.transport.Request): The object used to make HTTP requests. method (str): The request's HTTP method or the RPC method being invoked. url (str): The request's URI or the RPC service's URI. headers (Mapping): The request's headers. N)rrLrGradd_metric_headerr?rDrr<methodurlrCs rbefore_requestzCredentials.before_requestsq"  ) ,  & &w / / / /  " "7 + + +!'4+H+H+J+JKKK 7rcd|_dS)NT)rr's rwith_non_blocking_refreshz%Credentials.with_non_blocking_refreshs)-&&&rrF)__name__ __module__ __qualname____doc__rpropertyr$r(r/r1r3r6abcabstractmethodr=r?rDrGrLrRrT __classcell__rs@rrr#s]$66666&22X2" ; ;X ;  X *&&X&%%X%     A A A    C C C C""" / / /2.......rrceZdZdZdZdZdS)CredentialsWithQuotaProjectzGAbstract base for credentials supporting ``with_quota_project`` factoryc td)aReturns a copy of these credentials with a modified quota project. Args: quota_project_id (str): The project to use for quota and billing purposes Returns: google.auth.credentials.Credentials: A new credentials instance. z/This credential does not support quota project.r9)rr1s rwith_quota_projectz.CredentialsWithQuotaProject.with_quota_projects""STTTrctjtj}|r||S|SrF)osenvirongetrGOOGLE_CLOUD_QUOTA_PROJECTra)rquota_from_envs r#with_quota_project_from_environmentz?CredentialsWithQuotaProject.with_quota_project_from_environments:(8(STT  ;**>:: : rN)rUrVrWrXrarhr5rrr_r_s;QQ U U Urr_ceZdZdZdZdS)CredentialsWithTokenUrizCAbstract base for credentials supporting ``with_token_uri`` factoryc td)aReturns a copy of these credentials with a modified token uri. Args: token_uri (str): The uri to use for fetching/exchanging tokens Returns: google.auth.credentials.Credentials: A new credentials instance. z'This credential does not use token uri.r9)r token_uris rwith_token_uriz&CredentialsWithTokenUri.with_token_uris""KLLLrN)rUrVrWrXrmr5rrrjrjs.MM M M M M MrrjceZdZdZdZdS)CredentialsWithUniverseDomainzIAbstract base for credentials supporting ``with_universe_domain`` factoryc td)zReturns a copy of these credentials with a modified universe domain. Args: universe_domain (str): The universe domain to use Returns: google.auth.credentials.Credentials: A new credentials instance. z6This credential does not support with_universe_domain.r9)rr3s rwith_universe_domainz2CredentialsWithUniverseDomain.with_universe_domains" D   rN)rUrVrWrXrqr5rrroros)SS      rroceZdZdZejdZdZdZdZ d fd Z dZ d Z d Z ejd Zd ZxZS)CredentialsWithTrustBoundaryzHAbstract base for credentials supporting ``with_trust_boundary`` factoryc td)r8z"_refresh_token must be implementedr9r;s r_refresh_tokenz+CredentialsWithTrustBoundary._refresh_token&s""FGGGrc td)aReturns a copy of these credentials with a modified trust boundary. Args: trust_boundary Mapping[str, str]: The trust boundary to use for the credential. This should be a map with a "locations" key that maps to a list of GCP regions, and a "encodedLocations" key that maps to a hex string. Returns: google.auth.credentials.Credentials: A new credentials instance. z2This credential does not support trust boundaries.r9)rtrust_boundarys rwith_trust_boundaryz0CredentialsWithTrustBoundary.with_trust_boundary4s""VWWWrctjtjdsdS|jt krdS| S)aLChecks if a trust boundary lookup is required. A lookup is required if the feature is enabled via an environment variable, the universe domain is supported, and a no-op boundary is not already cached. Returns: bool: True if a trust boundary lookup is required, False otherwise. F)default)rget_bool_from_envr"GOOGLE_AUTH_TRUST_BOUNDARY_ENABLEDr3r_has_no_op_trust_boundaryr's r"_is_trust_boundary_lookup_requiredz?CredentialsWithTrustBoundary._is_trust_boundary_lookup_requiredBsZ)  ?    5  #: : :5113333rcb|j'|rddiSd|jdiSiS)Nzx-allowed-locationsencodedLocations)rr}r's r_get_trust_boundary_headerz7CredentialsWithTrustBoundary._get_trust_boundary_headerYsD   +--// Y-r22-t/CDV/WXX rNct||||dS)z-Apply the token to the authentication header.N)rrDupdater)rrCr&rs rrDz"CredentialsWithTrustBoundary.applybs?  gu%%%t668899999rcZ||||dS)zRefreshes the access token and the trust boundary. This method calls the subclass's token refresh logic and then refreshes the trust boundary if applicable. N)ru_refresh_trust_boundaryr;s rr=z$CredentialsWithTrustBoundary.refreshgs2 G$$$ $$W-----rc|sdS |||_dS#tj$rB}|j|t jtrtjd|Yd}~dSd}~wwxYw)aqTriggers a refresh of the trust boundary and updates the cache if necessary. Args: request (google.auth.transport.Request): The object used to make HTTP requests. Raises: google.auth.exceptions.RefreshError: If the trust boundary could not be refreshed and no cached value is available. Nz4Using cached trust boundary due to refresh error: %s) r~_lookup_trust_boundaryrr RefreshErrorris_logging_enabledr debug)rr<errors rrz4CredentialsWithTrustBoundary._refresh_trust_boundaryps6688  F #'#>#>w#G#GD &   #+ *733  JE FFFFF s4B7BBcddlm}|}|stjdi}||||||||S)aCalls the trust boundary lookup API to refresh the trust boundary cache. Args: request (google.auth.transport.Request): The object used to make HTTP requests. Returns: trust_boundary (dict): The trust boundary object returned by the lookup API. Raises: google.auth.exceptions.RefreshError: If the trust boundary could not be retrieved. r)_clientz*Failed to build trust boundary lookup URL.)rC) google.oauth2r _build_trust_boundary_lookup_urlr InvalidValuerArrr)rr<rrQrCs rrz3CredentialsWithTrustBoundary._lookup_trust_boundarys *)))))3355 X)*VWW W Gt6688999--gsG-LLLrc td)a Builds and returns the URL for the trust boundary lookup API. This method should be implemented by subclasses to provide the specific URL based on the credential type and its properties. Returns: str: The URL for the trust boundary lookup endpoint, or None if lookup should be skipped (e.g., for non-applicable universe domains). z4_build_trust_boundary_lookup_url must be implementedr9r's rrz=CredentialsWithTrustBoundary._build_trust_boundary_lookup_urls" B   rcZ|jdS|jdtkS)NFr)rre&NO_OP_TRUST_BOUNDARY_ENCODED_LOCATIONSr's rr}z6CredentialsWithTrustBoundary._has_no_op_trust_boundarys4   '5  $ $%7 8 85 6 rrF)rUrVrWrXrZr[rurxr~rrDr=rrrr}r\r]s@rrsrs#sRR H H H X X X444.:::::: ...4MMM2             rrscReZdZdZedZedZdZddZdZ dS) AnonymousCredentialszCredentials that do not provide any authentication information. These are useful in the case of services that support anonymous access or local service emulators that do not use credentials. cdS)z4Returns `False`, anonymous credentials never expire.Fr5r's rr$zAnonymousCredentials.expired urcdS)z7Returns `True`, anonymous credentials are always valid.Tr5r's rr(zAnonymousCredentials.valids trc*tjd)zVRaises :class:``InvalidOperation``, anonymous credentials cannot be refreshed.z*Anonymous credentials cannot be refreshed.)rInvalidOperationr;s rr=zAnonymousCredentials.refreshs)*VWWWrNc2|tjddS)zAnonymous credentials do nothing to the request. The optional ``token`` argument is not supported. Raises: google.auth.exceptions.InvalidValue: If a token was specified. Nz+Anonymous credentials don't support tokens.)rrrBs rrDzAnonymousCredentials.applys$  )*WXX X  rcdS)z0Anonymous credentials do nothing to the request.Nr5rOs rrRz#AnonymousCredentials.before_requestsrrF) rUrVrWrXrYr$r(r=rDrRr5rrrrs XXXXX Y Y Y Y?????rrcteZdZdZfdZedZedZej dZ dZ xZ S)ReadOnlyScopeda+Interface for credentials whose scopes can be queried. OAuth 2.0-based credentials allow limiting access using scopes as described in `RFC6749 Section 3.3`_. If a credential class implements this interface then the credentials either use scopes in their implementation. Some credentials require scopes in order to obtain a token. You can check if scoping is necessary with :attr:`requires_scopes`:: if credentials.requires_scopes: # Scoping is required. credentials = credentials.with_scopes(scopes=['one', 'two']) Credentials that require scopes must either be constructed with scopes:: credentials = SomeScopedCredentials(scopes=['one', 'two']) Or must copy an existing instance using :meth:`with_scopes`:: scoped_credentials = credentials.with_scopes(scopes=['one', 'two']) Some credentials have scopes but do not allow or require scopes to be set, these credentials can be used as-is. .. _RFC6749 Section 3.3: https://tools.ietf.org/html/rfc6749#section-3.3 crtt|d|_d|_dSrF)rrr_scopes_default_scopesrs rrzReadOnlyScoped.__init__s4 nd##,,... #rc|jS)z6Sequence[str]: the credentials' current set of scopes.)rr's rscopeszReadOnlyScoped.scopess |rc|jS)z>Sequence[str]: the credentials' current set of default scopes.)rr's rdefault_scopeszReadOnlyScoped.default_scopes s ##rcdS)zCTrue if these credentials require scopes to obtain an access token.Fr5r's rrequires_scopeszReadOnlyScoped.requires_scopesrrc|j|jn|j}t|t|pgS)a_Checks if the credentials have the given scopes. .. warning: This method is not guaranteed to be accurate if the credentials are :attr:`~Credentials.invalid`. Args: scopes (Sequence[str]): The list of scopes to check. Returns: bool: True if the credentials have the given scopes. )rrsetissubset)rrcredential_scopess r has_scopeszReadOnlyScoped.has_scopessF!L4DLL$:N 6{{##C(9(?R$@$@AAAr) rUrVrWrXrrYrrrZabstractpropertyrrr\r]s@rrrs8$$$$$ X$$X$ BBBBBBBrr) metaclassc4eZdZdZejddZdS)Scopeda5Interface for credentials whose scopes can be replaced while copying. OAuth 2.0-based credentials allow limiting access using scopes as described in `RFC6749 Section 3.3`_. If a credential class implements this interface then the credentials either use scopes in their implementation. Some credentials require scopes in order to obtain a token. You can check if scoping is necessary with :attr:`requires_scopes`:: if credentials.requires_scopes: # Scoping is required. credentials = credentials.create_scoped(['one', 'two']) Credentials that require scopes must either be constructed with scopes:: credentials = SomeScopedCredentials(scopes=['one', 'two']) Or must copy an existing instance using :meth:`with_scopes`:: scoped_credentials = credentials.with_scopes(scopes=['one', 'two']) Some credentials have scopes but do not allow or require scopes to be set, these credentials can be used as-is. .. _RFC6749 Section 3.3: https://tools.ietf.org/html/rfc6749#section-3.3 Nc td)aCreate a copy of these credentials with the specified scopes. Args: scopes (Sequence[str]): The list of scopes to attach to the current credentials. Raises: NotImplementedError: If the credentials' scopes can not be changed. This can be avoided by checking :attr:`requires_scopes` before calling this method. z$This class does not require scoping.r9)rrrs r with_scopeszScoped.with_scopesCs""HIIIrrF)rUrVrWrXrZr[rr5rrrr&sH8  J J J J J Jrrclt|tr|jr|||S|S)aCreates a copy of the credentials with scopes if scoping is required. This helper function is useful when you do not know (or care to know) the specific type of credentials you are using (such as when you use :func:`google.auth.default`). This function will call :meth:`Scoped.with_scopes` if the credentials are scoped credentials and if the credentials require scoping. Otherwise, it will return the credentials as-is. Args: credentials (google.auth.credentials.Credentials): The credentials to scope if necessary. scopes (Sequence[str]): The list of scopes to use. default_scopes (Sequence[str]): Default scopes passed by a Google client library. Use 'scopes' for user-defined scopes. Returns: google.auth.credentials.Credentials: Either a new set of scoped credentials, or the passed in credentials instance if no scoping was required. )r) isinstancerrr) credentialsrrs rwith_scopes_if_requiredrSs?,+v&&;+F&&vn&MMMrcreZdZdZejdZejdZejdZ dS)SigningzCInterface for credentials that can cryptographically sign messages.c td)zSigns the given message. Args: message (bytes): The message to sign. Returns: bytes: The message's cryptographic signature. zSign bytes must be implemented.r9)rmessages r sign_byteszSigning.sign_bytesrs""CDDDrc td)z;Optional[str]: An email address that identifies the signer.z!Signer email must be implemented.r9r's r signer_emailzSigning.signer_emails ""EFFFrc td)z8google.auth.crypt.Signer: The signer used to sign bytes.zSigner must be implemented.r9r's rsignerzSigning.signers ""?@@@rN) rUrVrWrXrZr[rrrrr5rrrrosMM E E E GGG  AAAAArrceZdZdZdZdZdZdS)r*aJ Tracks the state of a token. FRESH: The token is valid. It is not expired or close to expired, or the token has no expiry. STALE: The token is close to expired, and should be refreshed. The token can be used normally. INVALID: The token is expired or invalid. The token cannot be used for a normal operation. N)rUrVrWrXr,r-r+r5rrr*r*s) E EGGGrr*rF)#rXrZenumrrctypingr google.authrrrrgoogle.auth._credentials_baser google.auth._defaultr google.auth._refresh_workerr rr str__annotations__rrr_rjrorsrABCMetarrrrr*r5rrrs "!!  22222222""""""::::::((((((<<<<<<*,.S ...).&G.G.G.G.G."G.G.G.T+, M M M M Mk M M M     K   "X X X X X ;X X X v"?"?"?"?"?;"?"?"?J@B@B@B@B@Bs{@B@B@B@BF*J*J*J*J*J^*J*J*JZ8AAAAA AAAA@          r