Pyjo.GCP.Credentials.Provider - Base class of the providers of credentials¶
from Pyjo.GCP.Credentials import Credentials
from Pyjo.GCP.Credentials.Provider import Provider
class Static(Provider):
async def fetch(self):
return Credentials(token='ya29.static')
credentials = Static().credentials()
# Or import the module, as in Google Python Style Guide
import Pyjo.GCP.Credentials
import Pyjo.GCP.Credentials.Provider
class Service(Pyjo.GCP.Credentials.Provider.object):
async def fetch(self):
tx = await self.start(self.ua.build_tx('GET', 'http://127.0.0.1:8080/token'))
data = Pyjo.GCP.Credentials.Provider.parse_token(tx, 'the service')
return Pyjo.GCP.Credentials.new(
token=data['access_token'], expiration=Pyjo.GCP.Credentials.expiration_in(data['expires_in'])
)
Service.new().credentials_p().then(lambda credentials: print(credentials.expiration)).wait()
Pyjo.GCP.Credentials.Provider is the base class of the providers of
Pyjo.GCP.Credentials. A provider gets its credentials in
fetch(), keeps them until they are about
to expire, and gets them again then.
fetch() is a coroutine, which awaits
what it needs to wait for, so that the same code runs blocking, in
credentials(), and non-blocking, in
credentials_p():
a transaction of
start(), whichuastarts, gives the transaction back with its response, or raises thePyjo.Exceptionof an error of the connection;a program of
run()gives its exit code, its output and its error, or raises thePyjo.Exceptionof a program that cannot be run;the
steps()of another provider give its credentials.
Pyjo.Steps runs the coroutine, without an event loop, so it
awaits nothing else, not a Pyjo.Promise and not a future of asyncio,
which is a TypeError. fetch() may
be a generator too, which yields the transactions and the programs, and takes the
credentials of another provider with yield from of its
steps():
class Service(Provider):
def fetch(self):
tx = yield self.start(self.ua.build_tx('GET', 'http://127.0.0.1:8080/token'))
...
What the coroutine returns is the credentials, or None when the
provider is not configured, as a file of credentials that does not exist, so that
Pyjo.GCP.Credentials.Chain asks the next one. An exception is an error
of a provider that is configured, which ends the chain.
See also¶
Pyjo.GCP.Credentials, Pyjo.GCP.Credentials.Chain, Pyjo.Steps.
Classes¶
- class Pyjo.GCP.Credentials.Provider.Pyjo_GCP_Credentials_Provider(*, ua: Pyjo_UserAgent | None = None, refresh: float = 225)[source]¶
Pyjo.GCP.Credentials.Providerinherits all attributes and methods fromPyjo.Baseand implements the following new ones.- __init__(*, ua: Pyjo_UserAgent | None = None, refresh: float = 225) None[source]¶
provider = Provider.new() provider = Provider.new(ua=Pyjo.UserAgent.new(request_timeout=5), refresh=600)
Construct a new provider.
- __weakref__¶
list of weak references to the object
- cached: Pyjo_GCP_Credentials | None¶
credentials = provider.cached provider.cached = None
The credentials that the provider got last, which it gives until they are about to expire, or
None, which makes it get them again.
- credentials() Pyjo_GCP_Credentials | None[source]¶
credentials = provider.credentials()
The credentials,
cachedones or new ones, blocking, orNonewhen the provider is not configured.
- credentials_p() Pyjo_Promise[source]¶
promise = provider.credentials_p()
Same as
credentials(), but non-blocking, and returns aPyjo.Promiseobject. The calls while the provider gets the credentials get the same promise, so that it gets them once.
- async fetch() Pyjo_GCP_Credentials | None[source]¶
credentials = await provider.fetch()
Get new credentials, a coroutine that awaits what it waits for, as the description of the class says, and returns the credentials or
None. A subclass implements it; this one has no credentials.
- is_fresh() bool[source]¶
boolean = provider.is_fresh()
Whether the provider has
cachedcredentials that are not about to expire.
- refresh¶
seconds = provider.refresh provider.refresh = 600
The number of seconds before their expiration when the credentials are got again, 225 by default, 3 minutes and 45 seconds, as google-auth has it.
- run(args: Sequence[str], input: bytes = b'', *, env: Mapping[str, str] | None = None, timeout: float | None = None) Command[source]¶
code, out, err = await provider.run(['gcloud', 'auth', 'print-access-token']) code, out, err = await provider.run(['/opt/bin/credentials'], input=b'admin', timeout=30)
A program for
fetch()to await, with the environment and the timeout ofCommand, which gives its exit code, its standard output and its standard error.
- start(tx: Pyjo_Transaction_HTTP) Request[source]¶
tx = await provider.start(provider.ua.build_tx('POST', url, form=body))
A transaction for
fetch()to await, whichuastarts, and which it gives back with its response.
- steps() Pyjo_Steps[Pyjo_GCP_Credentials | None][source]¶
credentials = await provider.steps() credentials = yield from provider.steps()
The
cachedcredentials, or the ones offetch(), which it keeps, for a provider that gets its credentials with the ones of another, as an impersonated service account with the credentials of its source.
- ua¶
ua = provider.ua provider.ua = Pyjo.UserAgent.new()
The user agent of the requests of the provider, a new
Pyjo.UserAgentobject by default.
- Pyjo.GCP.Credentials.Provider.invalid(name: str, missing: Sequence[str]) Pyjo_Exception[source]¶
raise Pyjo.GCP.Credentials.Provider.invalid('an authorized user', ['refresh_token'])
The
Pyjo.Exceptionof the credentials of the name without the fields that they need.
- Pyjo.GCP.Credentials.Provider.parse_token(tx: Pyjo_Transaction_HTTP, name: str) dict[str, Any][source]¶
data = Pyjo.GCP.Credentials.Provider.parse_token(tx, 'an authorized user')
The JSON object of the response of a token endpoint of OAuth 2.0, with
access_token, or aPyjo.Exceptionof the credentials of the name, witherroranderror_descriptionof OAuth 2.0, as google-auth has them, or the status of the response.