Class GoogleAuth (8.2.0)

Package

google-auth-library

Constructors

(constructor)(opts)

constructor(opts?: GoogleAuthOptions<T>);

Constructs a new instance of the GoogleAuth class

Parameter
NameDescription
opts GoogleAuthOptions<T>

Properties

cachedCredential

cachedCredential: JSONClient | Impersonated | Compute | T | null;

defaultScopes

defaultScopes?: string | string[];

Scopes populated by the client library by default. We differentiate between these and user defined scopes when deciding whether to use a self-signed JWT.

defaultServicePath

defaultServicePath?: string;

DefaultTransporter

static DefaultTransporter: typeof DefaultTransporter;

Export DefaultTransporter as a static property of the class.

isGCE

get isGCE(): boolean | undefined;

jsonContent

jsonContent: JWTInput | ExternalAccountClientOptions | null;

transporter

transporter?: Transporter;

useJWTAccessWithScope

useJWTAccessWithScope?: boolean;

Methods

_checkIsGCE()

_checkIsGCE(): Promise<boolean>;

Determines whether the auth layer is running on Google Compute Engine.

Returns
TypeDescription
Promise<boolean>

A promise that resolves with the boolean. private

_getApplicationCredentialsFromFilePath(filePath, options)

_getApplicationCredentialsFromFilePath(filePath: string, options?: RefreshOptions): Promise<JSONClient>;

Attempts to load default credentials from a file at the given path..

Parameters
NameDescription
filePath string

The path to the file to read.

options RefreshOptions
Returns
TypeDescription
Promise<JSONClient>

Promise that resolves with the OAuth2Client private

_tryGetApplicationCredentialsFromEnvironmentVariable(options)

_tryGetApplicationCredentialsFromEnvironmentVariable(options?: RefreshOptions): Promise<JSONClient | null>;

Attempts to load default credentials from the environment variable path..

Parameter
NameDescription
options RefreshOptions
Returns
TypeDescription
Promise<JSONClient | null>

Promise that resolves with the OAuth2Client or null. private

_tryGetApplicationCredentialsFromWellKnownFile(options)

_tryGetApplicationCredentialsFromWellKnownFile(options?: RefreshOptions): Promise<JSONClient | null>;

Attempts to load default credentials from a well-known file location Promise that resolves with the OAuth2Client or null. private

Parameter
NameDescription
options RefreshOptions
Returns
TypeDescription
Promise<JSONClient | null>

authorizeRequest(opts)

authorizeRequest(opts: {
        url?: string;
        uri?: string;
        headers?: Headers;
    }): Promise<{
        url?: string | undefined;
        uri?: string | undefined;
        headers?: Headers | undefined;
    }>;

Obtain credentials for a request, then attach the appropriate headers to the request options.

Parameter
NameDescription
opts { url?: string; uri?: string; headers?: Headers; }

Axios or Request options on which to attach the headers

Returns
TypeDescription
Promise<{ url?: string | undefined; uri?: string | undefined; headers?: Headers | undefined; }>

fromAPIKey(apiKey, options)

fromAPIKey(apiKey: string, options?: RefreshOptions): JWT;

Create a credentials instance using the given API key string.

Parameters
NameDescription
apiKey string

The API key string

options RefreshOptions

An optional options object.

Returns
TypeDescription
JWT

A JWT loaded from the key

fromImpersonatedJSON(json)

fromImpersonatedJSON(json: ImpersonatedJWTInput): Impersonated;

Create a credentials instance using a given impersonated input options.

Parameter
NameDescription
json ImpersonatedJWTInput

The impersonated input object.

Returns
TypeDescription
Impersonated

JWT or UserRefresh Client with data

fromJSON(json, options)

fromJSON(json: JWTInput | ImpersonatedJWTInput, options?: RefreshOptions): JSONClient;

Create a credentials instance using the given input options.

Parameters
NameDescription
json JWTInput | ImpersonatedJWTInput

The input object.

options RefreshOptions

The JWT or UserRefresh options for the client

Returns
TypeDescription
JSONClient

JWT or UserRefresh Client with data

fromStream(inputStream)

fromStream(inputStream: stream.Readable): Promise<JSONClient>;

Create a credentials instance using the given input stream.

Parameter
NameDescription
inputStream stream.Readable

The input stream.

Returns
TypeDescription
Promise<JSONClient>

fromStream(inputStream, callback)

fromStream(inputStream: stream.Readable, callback: CredentialCallback): void;
Parameters
NameDescription
inputStream stream.Readable
callback CredentialCallback
Returns
TypeDescription
void

fromStream(inputStream, options)

fromStream(inputStream: stream.Readable, options: RefreshOptions): Promise<JSONClient>;
Parameters
NameDescription
inputStream stream.Readable
options RefreshOptions
Returns
TypeDescription
Promise<JSONClient>

fromStream(inputStream, options, callback)

fromStream(inputStream: stream.Readable, options: RefreshOptions, callback: CredentialCallback): void;
Parameters
NameDescription
inputStream stream.Readable
options RefreshOptions
callback CredentialCallback
Returns
TypeDescription
void

getAccessToken()

getAccessToken(): Promise<string | null | undefined>;

Automatically obtain application default credentials, and return an access token for making requests.

Returns
TypeDescription
Promise<string | null | undefined>

getApplicationDefault()

getApplicationDefault(): Promise<ADCResponse>;

Obtains the default service-level credentials for the application.

Returns
TypeDescription
Promise<ADCResponse>

Promise that resolves with the ADCResponse (if no callback was passed).

getApplicationDefault(callback)

getApplicationDefault(callback: ADCCallback): void;
Parameter
NameDescription
callback ADCCallback
Returns
TypeDescription
void

getApplicationDefault(options)

getApplicationDefault(options: RefreshOptions): Promise<ADCResponse>;
Parameter
NameDescription
options RefreshOptions
Returns
TypeDescription
Promise<ADCResponse>

getApplicationDefault(options, callback)

getApplicationDefault(options: RefreshOptions, callback: ADCCallback): void;
Parameters
NameDescription
options RefreshOptions
callback ADCCallback
Returns
TypeDescription
void

getClient()

getClient(): Promise<Compute | JSONClient | NonNullable<T>>;

Automatically obtain a client based on the provided configuration. If no options were passed, use Application Default Credentials.

Returns
TypeDescription
Promise<Compute | JSONClient | NonNullable<T>>

getCredentials()

getCredentials(): Promise<CredentialBody>;

The callback function handles a credential object that contains the client_email and private_key (if exists). getCredentials first checks if the client is using an external account and uses the service account email in place of client_email. If that doesn't exist, it checks for these values from the user JSON. If the user JSON doesn't exist, and the environment is on GCE, it gets the client_email from the cloud metadata server.

Returns
TypeDescription
Promise<CredentialBody>

getCredentials(callback)

getCredentials(callback: (err: Error | null, credentials?: CredentialBody) => void): void;
Parameter
NameDescription
callback (err: Error | null, credentials?: CredentialBody) => void
Returns
TypeDescription
void

getEnv()

getEnv(): Promise<GCPEnv>;

Determine the compute environment in which the code is running.

Returns
TypeDescription
Promise<GCPEnv>

getIdTokenClient(targetAudience)

getIdTokenClient(targetAudience: string): Promise<IdTokenClient>;

Creates a client which will fetch an ID token for authorization.

Parameter
NameDescription
targetAudience string

the audience for the fetched ID token.

Returns
TypeDescription
Promise<IdTokenClient>

IdTokenClient for making HTTP calls authenticated with ID tokens.

getProjectId()

getProjectId(): Promise<string>;

Obtains the default project ID for the application.

Returns
TypeDescription
Promise<string>

Promise that resolves with project Id (if used without callback)

getProjectId(callback)

getProjectId(callback: ProjectIdCallback): void;
Parameter
NameDescription
callback ProjectIdCallback
Returns
TypeDescription
void

getRequestHeaders(url)

getRequestHeaders(url?: string): Promise<Headers>;

Obtain the HTTP headers that will provide authorization for a given request.

Parameter
NameDescription
url string
Returns
TypeDescription
Promise<Headers>

request(opts)

request<T = any>(opts: GaxiosOptions): Promise<GaxiosResponse<T>>;

Automatically obtain application default credentials, and make an HTTP request using the given options.

Parameter
NameDescription
opts GaxiosOptions

Axios request options for the HTTP request.

Returns
TypeDescription
Promise<GaxiosResponse<T>>
Type Parameter
NameDescription
T

setGapicJWTValues(client)

setGapicJWTValues(client: JWT): void;
Parameter
NameDescription
client JWT
Returns
TypeDescription
void

sign(data)

sign(data: string): Promise<string>;

Sign the given data with the current private key, or go out to the IAM API to sign it.

Parameter
NameDescription
data string

The data to be signed.

Returns
TypeDescription
Promise<string>