# google-auto-auth

> Making it as easy as possible to authenticate a Google API request

Latest version **0.10.1** (published 2018-04-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install google-auto-auth
pnpm add google-auto-auth
yarn add google-auto-auth
bun add google-auto-auth
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.10.1 |
| Published | 2018-04-23 |
| First published | 2015-08-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=4.0.0 |
| Dependencies | 4 |
| Unpacked size | 18.5 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 34 |
| Author | Stephen Sawchuk |
| Maintainers | borenet, callmehiphop, dogben, google-admin, google-node-team, jcgregorio, jdobry, jgeewax, kjlubick, rmistry, saltmueller, stephenplusplus |
| Keywords | google, authentication, jwt, service, account, googleapis, gcloud, cloud, gce, compute, engine, auth, access, token |

## Links

- npm: https://www.npmjs.com/package/google-auto-auth
- Repository: https://github.com/stephenplusplus/google-auto-auth
- Homepage: https://github.com/stephenplusplus/google-auto-auth#readme
- Issues: https://github.com/stephenplusplus/google-auto-auth/issues
- npm.io page: https://npm.io/package/google-auto-auth

## Dependencies (4)

- [async](https://npm.io/package/async.md) ^2.3.0
- [request](https://npm.io/package/request.md) ^2.79.0
- [gcp-metadata](https://npm.io/package/gcp-metadata.md) ^0.6.1
- [google-auth-library](https://npm.io/package/google-auth-library.md) ^1.3.1

## Alternatives

- [@clerk/clerk-expo](https://npm.io/package/@clerk/clerk-expo.md) — 133.6K weekly downloads
- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads

## Recent versions

- 0.10.1 (latest) — 2018-04-23
- 0.10.0 — 2018-03-19
- 0.9.7 — 2018-03-07
- 0.9.6 — 2018-03-06
- 0.9.5 — 2018-03-01
- 0.9.4 — 2018-02-21
- 0.9.3 — 2018-01-31
- 0.9.2 — 2018-01-31
- 0.9.1 — 2018-01-11
- 0.9.0 — 2017-12-21
- 0.8.2 — 2017-12-20
- 0.8.1 — 2017-11-30
- 0.8.0 — 2017-11-20
- 0.7.2 — 2017-08-21
- 0.7.1 — 2017-06-21
- … 17 more at https://npm.io/package/google-auto-auth/versions

## README

# google-auto-auth
> Making it as easy as possible to authenticate a Google API request

```sh
$ npm install --save google-auto-auth
```
```js
var googleAuth = require('google-auto-auth');

// Create a client
var auth = googleAuth();

auth.authorizeRequest({
  method: 'get',
  uri: 'https://www.googleapis.com/something'
}, function (err, authorizedReqOpts) {
/*
  authorizedReqOpts = {
    method: 'get',
    uri: 'https://www.googleapis.com/something',
    headers: {
      Authorization: 'Bearer {{token}}'
    }
  }
*/
});
```

Or, just get an access token.
```js
auth.getToken(function (err, token) {
/*
  token = 'access token'
*/
});
```

<a name="automatic-if"></a>
This works automatically **if**:

  - your app runs on Google Cloud Platform
  - you are authenticated with the `gcloud` sdk
  - you have the path to a JSON key file as an environment variable named `GOOGLE_APPLICATION_CREDENTIALS`

If you do not meet those, you must provide a `keyFilename` or `credentials` object.

```js
var googleAuth = require('google-auto-auth');

var authConfig = {};

// path to a key:
authConfig.keyFilename = '/path/to/keyfile.json';

// or a credentials object:
authConfig.credentials = {
  client_email: '...',
  private_key: '...'
};

// Create a client
var auth = googleAuth(authConfig);

auth.authorizeRequest({/*...*/}, function (err, authorizedReqOpts) {});
auth.getToken(function (err, token) {});
```

### API

#### googleAuth = require('google-auto-auth')

#### auth = googleAuth([authConfig])

##### authConfig

- Type: `Object`

See the above section on Authentication. This object is necessary if automatic authentication is not available in your environment.

At a glance, the supported properties for this method are:

- `credentials` - Object containing `client_email` and `private_key` properties
- `keyFilename` - Path to a .json, .pem, or .p12 key file
- `projectId` - Your project ID
- `scopes` - Required scopes for the desired API request
- `token` - An access token. If provided, we'll use this instead of fetching a new one

#### auth.authorizeRequest(reqOpts, callback)

Extend an HTTP request object with an authorized header.

##### callback(err, authorizedReqOpts)

###### callback.err

- Type: `Error`

An API error or an error if scopes are required for the request you're trying to make (check for err.code = `MISSING_SCOPE`). If you receive the missing scope error, provide the `authConfig.scopes` array with the necessary scope URLs for your request. There are examples of scopes that are required for some of the Google Cloud Platform services in the [gcloud-node Authentication Guide](https://googlecloudplatform.github.io/gcloud-node/#/authentication).

###### callback.authorizedReqOpts

- Type: `Object`

The reqOpts object provided has been extended with a valid access token attached to the `headers.Authorization` value. E.g.: `headers.Authorization = 'Bearer y.2343...'`.

#### auth.getAuthClient(callback)

Get the auth client instance from [google-auth-library](http://gitnpm.com/googleauth).

##### callback(err, authClient)

###### callback.err

- Type: `Error`

An error that occurred while trying to get an authorization client.

###### callback.authClient

- Type: [`google-auth-library`](http://gitnpm.com/googleauth)

The client instance from [google-auth-library](http://gitnpm.com/googleauth). This is the underlying object this library uses.


#### auth.getCredentials(callback)

Get the `client_email` and `private_key` properties from an authorized client.

##### callback(err, credentials)

###### callback.err

- Type: `Error`

An error that occurred while trying to get an authorization client.

###### callback.credentials

- Type: `Object`

An object containing `client_email` and `private_key`.


#### auth.getEnvironment(callback)

Determine if the environment the app is running in is a Google Compute Engine instance.

##### callback(err, environmentObject)

###### callback.err

- Type: `Null`

We won't return an error, but it's here for convention-sake.

###### callback.environmentObject

- Type: `Object`

```js
{
  IS_APP_ENGINE: Boolean,
  IS_CLOUD_FUNCTION: Boolean,
  IS_COMPUTE_ENGINE: Boolean,
  IS_CONTAINER_ENGINE: Boolean
}
```

If you've already run this function, the object will persist as `auth.environment`.


#### auth.getProjectId(callback)

Get the project ID if it was auto-detected or parsed from the provided keyfile.

##### callback(err, projectId)

###### callback.err

- Type: `Error`

An error that occurred while trying to get an authorization client.

###### callback.projectId

- Type: `string`

The project ID that was parsed from the provided key file or auto-detected from the environment.


#### auth.getToken(callback)

Get an access token. The token will always be current. If necessary, background refreshes are handled automatically.

##### callback(err, token)

###### callback.err

- Type: `Error`

An API error or an error if scopes are required for the request you're trying to make (check for err.code = `MISSING_SCOPE`). If you receive the missing scope error, provide the `authConfig.scopes` array with the necessary scope URLs for your request.

###### callback.token

- Type: `String`

A current access token to be used during an API request. If you provided `authConfig.token`, this method simply returns the value you passed.


#### auth.isAppEngine(callback)

Determine if the environment the app is running in is a Google App Engine instance.

##### callback(err, isAppEngine)

###### callback.err

- Type: `Null`

We won't return an error, but it's here for convention-sake.

###### callback.isAppEngine

- Type: `Boolean`

Whether the app is in App Engine or not.


#### auth.isCloudFunction(callback)

Determine if the environment the app is running in is a Google Cloud Function.

##### callback(err, isCloudFunction)

###### callback.err

- Type: `Null`

We won't return an error, but it's here for convention-sake.

###### callback.isCloudFunction

- Type: `Boolean`

Whether the app is in a Cloud Function or not.


#### auth.isComputeEngine(callback)

Determine if the environment the app is running in is a Google Compute Engine instance.

##### callback(err, isComputeEngine)

###### callback.err

- Type: `Null`

We won't return an error, but it's here for convention-sake.

###### callback.isComputeEngine

- Type: `Boolean`

Whether the app is in a Compute Engine instance or not.


#### auth.isContainerEngine(callback)

Determine if the environment the app is running in is a Google Container Engine instance.

##### callback(err, isContainerEngine)

###### callback.err

- Type: `Null`

We won't return an error, but it's here for convention-sake.

###### callback.isContainerEngine

- Type: `Boolean`

Whether the app is in a Container Engine instance or not.

---
_Source: https://npm.io/package/google-auto-auth · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
