Coder Social home page Coder Social logo

carloslema / node-gtoken Goto Github PK

View Code? Open in Web Editor NEW

This project forked from googleapis/node-gtoken

0.0 1.0 0.0 232 KB

:key: Google Auth Service Account Tokens for Node.js

License: MIT License

JavaScript 9.19% Shell 17.61% Batchfile 2.75% TypeScript 69.69% Python 0.77%

node-gtoken's Introduction

node-gtoken

NPM Version CircleCI 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: '[email protected]',
  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: '[email protected]',
  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'

Changelog

1.2.2 -> 2.0.0

New features:

  • API now supports callback and promise based workflows

Breaking changes:

  • GoogleToken is now a class type, and must be instantiated.
  • GoogleToken.expires_at renamed to GoogleToken.expiresAt
  • GoogleToken.raw_token renamed to GoogleToken.rawToken
  • GoogleToken.token_expires renamed to GoogleToken.tokenExpires

License

MIT

node-gtoken's People

Contributors

justinbeckwith avatar ryanseys avatar ofrobots avatar renovate[bot] avatar greenkeeper[bot] avatar dpebot avatar jkwlui avatar allonsy avatar stephenplusplus avatar jinwoo avatar mhart avatar nhooey avatar theacodes avatar yoshi-automation avatar

Watchers

Carlos Lema avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.