Socket
Socket
Sign inDemoInstall

gtoken

Package Overview
Dependencies
26
Maintainers
6
Versions
47
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    gtoken

Node.js Google Authentication Service Account Tokens


Version published
Weekly downloads
11M
increased by10.79%
Maintainers
6
Install size
2.73 MB
Created
Weekly downloads
 

Package description

What is gtoken?

The gtoken npm package is designed to handle the acquisition and management of Google OAuth2 tokens. It simplifies the process of authenticating with Google APIs by managing the token generation, refresh, and validation processes.

What are gtoken's main functionalities?

Generate Access Token

This feature allows users to generate an access token by providing a key file and the required scopes. The token can then be used to authenticate requests to Google APIs.

const { GoogleToken } = require('gtoken');
const gtoken = new GoogleToken({
  keyFile: 'path/to/keyfile.json',
  scope: ['https://www.googleapis.com/auth/drive']
});
gtoken.getToken().then(token => {
  console.log(token);
}).catch(err => {
  console.error(err);
});

Refresh Token

This feature allows users to refresh their existing token when it expires. The refreshed token ensures continued access without needing to re-authenticate.

const { GoogleToken } = require('gtoken');
const gtoken = new GoogleToken({
  keyFile: 'path/to/keyfile.json',
  scope: ['https://www.googleapis.com/auth/drive'],
  token: existingToken
});
gtoken.refreshToken().then(newToken => {
  console.log(newToken);
}).catch(err => {
  console.error(err);
});

Other packages similar to gtoken

Changelog

Source

v2.3.3

03-13-2019 14:54 PDT

Bug Fixes

  • fix: propagate error message (#173)

Documentation

  • docs: update links in contrib guide (#171)
  • docs: move CONTRIBUTING.md to root (#166)
  • docs: add lint/fix example to contributing guide (#164)

Internal / Testing Changes

  • build: Add docuploader credentials to node publish jobs (#176)
  • build: use node10 to run samples-test, system-test etc (#175)
  • build: update release configuration
  • chore(deps): update dependency mocha to v6
  • build: use linkinator for docs test (#170)
  • build: create docs test npm scripts (#169)
  • build: test using @grpc/grpc-js in CI (#168)
  • build: ignore googleapis.com in doc link check (#162)
  • build: check for 404s on all docs

Readme

Source

node-gtoken

NPM Version Dependency Status devDependency Status Known Vulnerabilities codecov style badge

Node.js Google Authentication Service Account Tokens

Installation

npm install gtoken

Usage

Use with a .pem or .p12 key file:

const { GoogleToken } = require('gtoken');
const gtoken = new GoogleToken({
  keyFile: 'path/to/key.pem', // or path to .p12 key file
  email: 'my_service_account_email@developer.gserviceaccount.com',
  scope: ['https://scope1', 'https://scope2'] // or space-delimited string of scopes
});

gtoken.getToken(function(err, token) {
  if (err) {
    console.log(err);
    return;
  }
  console.log(token);
});

You can also use the async/await style API:

const token = await gtoken.getToken()
console.log(token);

Or use promises:

gtoken.getToken()
  .then(token => {
    console.log(`Token: ${token}`)
  })
  .catch(e => console.error);

Use with a service account .json key file:

const { GoogleToken } = require('gtoken');
const gtoken = new GoogleToken({
  keyFile: 'path/to/key.json',
  scope: ['https://scope1', 'https://scope2'] // or space-delimited string of scopes
});

gtoken.getToken(function(err, token) {
  if (err) {
    console.log(err);
    return;
  }
  console.log(token);
});

Pass the private key as a string directly:

const key = '-----BEGIN RSA PRIVATE KEY-----\nXXXXXXXXXXX...';
const { GoogleToken } = require('gtoken');
const gtoken = new GoogleToken({
  email: 'my_service_account_email@developer.gserviceaccount.com',
  scope: ['https://scope1', 'https://scope2'], // or space-delimited string of scopes
  key: key
});

Options

Various options that can be set when creating initializing the gtoken object.

  • options.email or options.iss: The service account email address.
  • options.scope: An array of scope strings or space-delimited string of scopes.
  • options.sub: The email address of the user requesting delegated access.
  • options.keyFile: The filename of .json key, .pem key or .p12 key.
  • options.key: The raw RSA private key value, in place of using options.keyFile.

.getToken(callback)

Returns the cached token or requests a new one and returns it.

gtoken.getToken(function(err, token) {
  console.log(err || token);
  // gtoken.token value is also set
});

.getCredentials('path/to/key.json')

Given a keyfile, returns the key and (if available) the client email.

const creds = await gtoken.getCredentials('path/to/key.json');

Properties

Various properties set on the gtoken object after call to .getToken().

  • gtoken.token: The access token.
  • gtoken.expiresAt: The expiry date as milliseconds since 1970/01/01
  • gtoken.key: The raw key value.
  • gtoken.rawToken: Most recent raw token data received from Google.

.hasExpired()

Returns true if the token has expired, or token does not exist.

gtoken.getToken(function(err, token) {
  if(token) {
    gtoken.hasExpired(); // false
  }
});

.revokeToken()

Revoke the token if set.

gtoken.revokeToken(function(err) {
  if (err) {
    console.log(err);
    return;
  }
  console.log('Token revoked!');
});

Downloading your private .p12 key from Google

  1. Open the Google Developer Console.
  2. Open your project and under "APIs & auth", click Credentials.
  3. Generate a new .p12 key and download it into your project.

Converting your .p12 key to a .pem key

You can just specify your .p12 file (with .p12 extension) as the keyFile and it will automatically be converted to a .pem on the fly, however this results in a slight performance hit. If you'd like to convert to a .pem for use later, use OpenSSL if you have it installed.

$ openssl pkcs12 -in key.p12 -nodes -nocerts > key.pem

Don't forget, the passphrase when converting these files is the string 'notasecret'

License

MIT

Keywords

FAQs

Last updated on 13 Mar 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc