New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@dgcode/auth

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dgcode/auth

authentication helper for google apis

  • 0.1.42
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
6
increased by200%
Maintainers
1
Weekly downloads
 
Created
Source

@dgcode/auth

authentication helper for google apis

Install

$ npm install @dgcode/auth

Usage

While Google documents its Node.JS setup for API calls fairly well, the whole process can quickly become annoying when the same setup code needs to be replicated across multiple side-projects. This library provides a one-liner to achieve the same results.

Basic authorization

import { authorize } from '@dgcode/auth';

authorize({
  credentials: 'my-keys-folder/google-secret.json',
  scopes: [ 'admin.directory' ]
}).then(auth => {

  // `auth` is a Google OAuth2Client instance

});

Authentication

The following impersonates a user with help of a service account:

import { authenticate } from '@dgcode/auth';

authenticate({
  email: 'someuser@gmail.com',
  key: 'my-keys-folder/service-account.json',
  scopes: [ 'gmail.settings' ]
}).then(auth => {

  // `auth` is a Google JWT instance

});

Post-auth usage

Once an auth has been determined (using one of the above setups), you can pass it to send authorized requests via the googleapis module (conveniently re-exported by this library):

import { authenticate, google } from '@dgcode/auth';

authorize({
  credentials: 'my-keys-folder/google-secret.json',
  scopes: [ 'admin.directory.user' ]
}).then(auth => {

  const userKey = 'someuser@gmail.com';
  return google.admin('directory_v1').users.get({ auth, userKey });

}).then(res => {
  const user = res.data;
  // `user` is a User resource object provided by Google APIs
});

With a Client manager

All processes above can be even more conveniently executed with help of a centralized Client instance:

import { Client } from '@dgcode/auth';

async function getGoogleUser(email) {

  const client = new Client();

  await client.authorize({
    credentials: 'my-keys-folder/google-secret.json',
    scopes: [ 'admin.directory.user' ]
  });

  const userKey = email; 
  const { data: user } = await client.service('admin(directory_v1)').users.get({
    userKey
  });

  return user;

}

License

MIT

FAQs

Package last updated on 09 Jan 2019

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc