Service Accounts: JSON Web Token (JWT) Profile for OAuth 2.0
This module implements the JWT Profile for OAuth 2.0 Authorization Grants as defined by RFC 7523 with particular support for how this RFC is implemented in Google’s infrastructure. Google refers to these credentials as Service Accounts.
Service accounts are used for server-to-server communication, such as interactions between a web application server and a Google service. The service account belongs to your application instead of to an individual end user. In contrast to other OAuth 2.0 profiles, no users are involved and your application “acts” as the service account.
Typically an application uses a service account when the application uses Google APIs to work with its own data rather than a user’s data. For example, an application that uses Google Cloud Datastore for data persistence would use a service account to authenticate its calls to the Google Cloud Datastore API. However, an application that needs to access a user’s Drive documents would use the normal OAuth 2.0 profile.
Additionally, Google Apps domain administrators can grant service accounts domain-wide delegation authority to access user data on behalf of users in the domain.
This profile uses a JWT to acquire an OAuth 2.0 access token. The JWT is used in place of the usual authorization token returned during the standard OAuth 2.0 Authorization Code grant. The JWT is only used for this purpose, as the acquired access token is used as the bearer token when making requests using these credentials.
This profile differs from normal OAuth 2.0 profile because no user consent step is required. The use of the private key allows this profile to assert identity directly.
This profile also differs from the google.auth.jwt
authentication because the JWT credentials use the JWT directly as the bearer token. This profile instead only uses the JWT to obtain an OAuth 2.0 access token. The obtained OAuth 2.0 access token is used as the bearer token.
Domain-wide delegation allows a service account to access user data on behalf of any user in a Google Apps domain without consent from the user. For example, an application that uses the Google Calendar API to add events to the calendars of all users in a Google Apps domain would use a service account to access the Google Calendar API on behalf of users.
The Google Apps administrator must explicitly authorize the service account to do this. This authorization step is referred to as “delegating domain-wide authority” to a service account.
You can use domain-wise delegation by creating a set of credentials with a specific subject using with_subject()
.
Bases: google.auth.credentials.Signing
, google.auth.credentials.Scoped
, google.auth.credentials.CredentialsWithQuotaProject
, google.auth.credentials.CredentialsWithTokenUri
Service account credentials
Usually, you’ll create these credentials with one of the helper constructors. To create credentials using a Google service account private key JSON file:
credentials = service_account.Credentials.from_service_account_file( 'service-account.json')
Or if you already have the service account file loaded:
service_account_info = json.load(open('service_account.json')) credentials = service_account.Credentials.from_service_account_info( service_account_info)
Both helper methods pass on arguments to the constructor, so you can specify additional scopes and a subject if necessary:
credentials = service_account.Credentials.from_service_account_file( 'service-account.json', scopes=['email'], subject='user@example.com')
The credentials are considered immutable. If you want to modify the scopes or the subject used for delegation, use with_scopes()
or with_subject()
:
scoped_credentials = credentials.with_scopes(['email']) delegated_credentials = credentials.with_subject(subject)
To add a quota project, use with_quota_project()
:
credentials = credentials.with_quota_project('myproject-123')
signer (google.auth.crypt.Signer) – The signer used to sign JWTs.
service_account_email (str) – The service account’s email.
scopes (Sequence
str
) – User-defined scopes to request during the authorization grant.
default_scopes (Sequence
str
) – Default scopes passed by a Google client library. Use ‘scopes’ for user-defined scopes.
token_uri (str) – The OAuth 2.0 Token URI.
subject (str) – For domain-wide delegation, the email address of the user to for which to request delegated access.
project_id (str) – Project ID associated with the service account credential.
quota_project_id (Optional
str
) – The project ID used for quota and billing.
additional_claims (Mapping
str
, str
) – Any
additional claims for the JWT assertion used in the authorization grant.
always_use_jwt_access (Optional
bool
) – Whether self signed JWT should be always used.
universe_domain (str) – The universe domain. The default universe domain is googleapis.com. For default value self signed jwt is used for token refresh.
trust_boundary (str) – String representation of trust boundary meta.
Creates a Credentials instance from parsed service account info.
credentials.
google.auth.service_account.Credentials
ValueError – If the info is not in the expected format.
Creates a Credentials instance from a service account json file.
filename (str) – The path to the service account json file.
kwargs – Additional arguments to pass to the constructor.
credentials.
google.auth.service_account.Credentials
The service account email.
Project ID associated with this credential.
Checks if the credentials requires scopes.
True if there are no scopes set otherwise False.
Create a copy of these credentials with the specified scopes.
scopes (Sequence
str
) – The list of scopes to attach to the current credentials.
NotImplementedError – If the credentials’ scopes can not be changed. This can be avoided by checking requires_scopes
before calling this method.
Create a copy of these credentials with the specified always_use_jwt_access value.
always_use_jwt_access (bool) – Whether always use self signed JWT or not.
instance.
google.auth.service_account.Credentials
google.auth.exceptions.InvalidValue – If the universe domain is not default and always_use_jwt_access is False.
Returns a copy of these credentials with a modified universe domain.
universe_domain (str) – The universe domain to use
A new credentials instance.
Create a copy of these credentials with the specified subject.
subject (str) – The subject claim.
instance.
google.auth.service_account.Credentials
Returns a copy of these credentials with modified claims.
Returns a copy of these credentials with a modified quota project.
quota_project_id (str) – The project to use for quota and billing purposes
A new credentials instance.
Returns a copy of these credentials with a modified token uri.
token_uri (str) – The uri to use for fetching/exchanging tokens
A new credentials instance.
Refreshes the access token.
request (google.auth.transport.Request) – The object used to make HTTP requests.
google.auth.exceptions.RefreshError – If the credentials could not be refreshed.
Signs the given message.
The signer used to sign bytes.
The credential information JSON.
The credential information will be added to auth related error messages by client library.
Apply the token to the authentication header.
Performs credential-specific before request logic.
Refreshes the credentials if necessary, then calls apply()
to apply the token to the authentication header.
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.
Checks if the credentials are expired.
Note that credentials can be invalid but not expired because Credentials with expiry
set to None is considered to never expire.
Deprecated since version v2.24.0: Prefer checking token_state
instead.
Checks if the credentials have the given scopes.
Project to use for quota and billing purposes.
See :obj:`TokenState
The universe domain value.
Checks the validity of the credentials.
This is True if the credentials have a token
and the token is not expired
.
Deprecated since version v2.24.0: Prefer checking token_state
instead.
Bases: google.auth.credentials.Signing
, google.auth.credentials.CredentialsWithQuotaProject
, google.auth.credentials.CredentialsWithTokenUri
Open ID Connect ID Token-based service account credentials.
These credentials are largely similar to Credentials
, but instead of using an OAuth 2.0 Access Token as the bearer token, they use an Open ID Connect ID Token as the bearer token. These credentials are useful when communicating to services that require ID Tokens and can not accept access tokens.
Usually, you’ll create these credentials with one of the helper constructors. To create credentials using a Google service account private key JSON file:
credentials = ( service_account.IDTokenCredentials.from_service_account_file( 'service-account.json'))
Or if you already have the service account file loaded:
service_account_info = json.load(open('service_account.json')) credentials = ( service_account.IDTokenCredentials.from_service_account_info( service_account_info))
Both helper methods pass on arguments to the constructor, so you can specify additional scopes and a subject if necessary:
credentials = ( service_account.IDTokenCredentials.from_service_account_file( 'service-account.json', scopes=['email'], subject='user@example.com'))
The credentials are considered immutable. If you want to modify the scopes or the subject used for delegation, use with_scopes()
or with_subject()
:
scoped_credentials = credentials.with_scopes(['email']) delegated_credentials = credentials.with_subject(subject)
signer (google.auth.crypt.Signer) – The signer used to sign JWTs.
service_account_email (str) – The service account’s email.
token_uri (str) – The OAuth 2.0 Token URI.
target_audience (str) – The intended audience for these credentials, used when requesting the ID Token. The ID Token’s aud
claim will be set to this string.
additional_claims (Mapping
str
, str
) – Any
additional claims for the JWT assertion used in the authorization grant.
quota_project_id (Optional
str
) – The project ID used for quota and billing.
universe_domain (str) – The universe domain. The default universe domain is googleapis.com. For default value IAM ID token endponint is used for token refresh. Note that iam.serviceAccountTokenCreator role is required to use the IAM endpoint.
Creates a credentials instance from parsed service account info.
credentials.
google.auth.service_account.IDTokenCredentials
ValueError – If the info is not in the expected format.
Creates a credentials instance from a service account json file.
filename (str) – The path to the service account json file.
kwargs – Additional arguments to pass to the constructor.
credentials.
google.auth.service_account.IDTokenCredentials
Create a copy of these credentials with the specified target audience.
target_audience (str) – The intended audience for these credentials,
Token. (used when requesting the ID) –
instance.
google.auth.service_account.IDTokenCredentials
Returns a copy of these credentials with a modified quota project.
quota_project_id (str) – The project to use for quota and billing purposes
A new credentials instance.
Returns a copy of these credentials with a modified token uri.
token_uri (str) – The uri to use for fetching/exchanging tokens
A new credentials instance.
Apply the token to the authentication header.
Performs credential-specific before request logic.
Refreshes the credentials if necessary, then calls apply()
to apply the token to the authentication header.
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.
Checks if the credentials are expired.
Note that credentials can be invalid but not expired because Credentials with expiry
set to None is considered to never expire.
Deprecated since version v2.24.0: Prefer checking token_state
instead.
The credential information JSON.
The credential information will be added to auth related error messages by client library.
Project to use for quota and billing purposes.
Refreshes the access token.
request (google.auth.transport.Request) – The object used to make HTTP requests.
google.auth.exceptions.RefreshError – If the credentials could not be refreshed.
See :obj:`TokenState
The universe domain value.
Checks the validity of the credentials.
This is True if the credentials have a token
and the token is not expired
.
Deprecated since version v2.24.0: Prefer checking token_state
instead.
When the token expires and is no longer valid. If this is None, the token is assumed to never expire.
The service account email.
Signs the given message.
The signer used to sign bytes.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4