pub struct Builder { /* private fields */ }Expand description
A builder for constructing Credentials instances.
This builder loads credentials according to the standard Application Default Credentials (ADC) strategy. ADC is the recommended approach for most applications and conforms to AIP-4110. If you need to load credentials from a non-standard location or source, you can use Builders on the specific credential types.
Common use cases where using ADC would is useful include:
- Your application is deployed to a Google Cloud environment such as Google Compute Engine (GCE), Google Kubernetes Engine (GKE), or Cloud Run. Each of these deployment environments provides a default service account to the application, and offers mechanisms to change this default service account without any code changes to your application.
- You are testing or developing the application on a workstation (physical or virtual). These credentials will use your preferences as set with gcloud auth application-default. These preferences can be your own Google Cloud user credentials, or some service account.
- Regardless of where your application is running, you can use the
GOOGLE_APPLICATION_CREDENTIALSenvironment variable to override the defaults. This environment variable should point to a file containing a service account key file, or a JSON object describing your user credentials.
The headers returned by these credentials should be used in the Authorization HTTP header.
The Google Cloud client libraries for Rust will typically find and use these credentials automatically if a credentials file exists in the standard ADC search paths. You might instantiate these credentials if you need to:
- Override the OAuth 2.0 scopes being requested for the access token.
- Override the quota project ID for billing and quota management.
§Example: fetching headers using ADC
let credentials = Builder::default()
.with_quota_project_id("my-project")
.build()?;
let headers = credentials.headers(Extensions::new()).await?;
println!("Headers: {headers:?}");Implementations§
Source§impl Builder
impl Builder
Sourcepub fn with_quota_project_id<S: Into<String>>(self, quota_project_id: S) -> Self
pub fn with_quota_project_id<S: Into<String>>(self, quota_project_id: S) -> Self
Sets the quota project for these credentials.
In some services, you can use an account in one project for authentication
and authorization, and charge the usage to a different project. This requires
that the user has serviceusage.services.use permissions on the quota project.
§Important: Precedence
If the GOOGLE_CLOUD_QUOTA_PROJECT environment variable is set,
its value will be used instead of the value provided to this method.
§Example
let credentials = Builder::default()
.with_quota_project_id("my-project")
.build();Sourcepub fn with_scopes<I, S>(self, scopes: I) -> Self
pub fn with_scopes<I, S>(self, scopes: I) -> Self
Sets the scopes for these credentials.
scopes act as an additional restriction in addition to the IAM permissions
granted to the principal (user or service account) that creates the token.
scopes define the permissions being requested for this specific access token
when interacting with a service. For example,
https://www.googleapis.com/auth/devstorage.read_write.
IAM permissions, on the other hand, define the underlying capabilities
the principal possesses within a system. For example, storage.buckets.delete.
The credentials certify that a particular token was created by a certain principal.
When a token generated with specific scopes is used, the request must be permitted by both the the principals’s underlying IAM permissions and the scopes requested for the token.
Sourcepub fn build(self) -> Result<Credentials, Error>
pub fn build(self) -> Result<Credentials, Error>
Returns a Credentials instance with the configured settings.
§Errors
Returns a CredentialsError if an unsupported credential type is provided or if the JSON value is either malformed or missing required fields.
For more information, on how to generate the JSON for a credential, consult the relevant section in the application-default credentials guide.
Sourcepub fn build_access_token_credentials(
self,
) -> Result<AccessTokenCredentials, Error>
pub fn build_access_token_credentials( self, ) -> Result<AccessTokenCredentials, Error>
Returns an AccessTokenCredentials instance with the configured settings.
§Example
// This will search for Application Default Credentials and build AccessTokenCredentials.
let credentials: AccessTokenCredentials = Builder::default()
.build_access_token_credentials()?;
let access_token = credentials.access_token().await?;
println!("Token: {}", access_token.token);§Errors
Returns a CredentialsError if an unsupported credential type is provided or if the JSON value is either malformed or missing required fields.
For more information, on how to generate the JSON for a credential, consult the relevant section in the application-default credentials guide.