npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bugle

v2.12.0

Published

Hapi plugin for Google Drive Login with refresh token storage policy and drive API extensions from npm: decorated-google-drive

Downloads

25

Readme

bugle free image of a bugle

A Hapi plugin for Google Drive Login with:

  • refresh token storage policy
  • hooked to create .drive on requests where available
  • drive API extensions from npm: decorated-google-drive

Usage

Compatibility

  • bugle@1 and bugle@2 work with hapi 16 but not hapi 17
  • bugle >= 2.12 includes [email protected] for request.drive
  • bugle >= 2.12 uses npm:axios for uploads, earlier versions use npm:request

Installation

npm i bugle -S

You also need grant and probably yar.

npm i grant -S
npm i yar -S

Configuration

./path/to/hapi/config/bugle.json

If you want to store encrypted refresh tokens for each user in their own Drive appDataFolder, where they can't change them and no one else will likely see them, add the "refreshTokenStash" setting below. Otherwise that can be omitted.

{
  "drive": {
      "refreshTokenStash": {
	      "location": "appDataFolder",
          "file": "r",
          "key": "makeUpSomthingBetterThanThisForYourIronEncryptionKey"
      }
   },
   "myRedirect": "/a/me", // where to send users who have logged in with Google Drive -- defaults to /a/me
   "useMeLevel": 2, // level from 0-4 to determine what to return from /a/me -- 0 is 404 (default), 1 is name+pic, 2 includes drive profile, 3 headers, 4 server info
   "loginHTMLFile": "/path/to/your/login.html", // optional HTML file to show at URL /a/login -- default provided
   "retryHTMLFile": "/path/to/your/retry.html"  // also optional
}

./path/to/hapi/config/grant.json

**Note: bugle reads the grant config via server.registrations.grant **

Configure grant like this for an app that wants to:

  • read/write its own files from the user's Google Drive
  • read/write the secret appDataFolder on the user's Google Drive
{
    "server": {
        "protocol": "https",   // you do use https, dont you?
        "host": "example.com",
        "callback": "/a/googledrive",
        "transport": "session",
        "state": true
     },
     "google": {
        "key": "your-api-console-drive-client-id-goes-here",
        "secret": "your-api-console-drive-secret-goes-here"
        "scope": [
             "https://www.googleapis.com/auth/drive.file",
             "https://www.googleapis.com/auth/drive.appdata"
         ],
        "callback": "/a/googledrive",
        "custom_params": {
             "access_type": "offline"
        }
     }
}

Note: Adding "prompt": "consent" to custom_params forces the Google app consent dialog every login, yielding new refresh tokens each time. In documentation, though, it is suggested to reuse the old refresh tokens until they expire (6 mo?) by storing them somewhere safe.

./path/to/hapi/config/yar.json

isSameSite:false turns off some cookie same site protection that was problematic with yar + chrome browser - use at own risk or omit

{
   "cookieOptions": {
        "password": "changeThisPasswordToSomethingHardToGuessBecauseItEncryptsTheCookiesWithTheDriveTokens",
        "isSecure": true,  // use true if you are using https
        "isSameSite": false  
     	}
}

Example Hapi Server

./path/to/hapi/index.js

Note: this requires some set up in Google API Console and the configuration files before it will work


// Minimal Example: Hapi server using yar-grant-bugle

// jshint esversion:6, strict:global, node:true

"use strict";

const Hapi = require('hapi');
const yar = require('yar');
const Grant = require('grant-hapi');
const grant = new Grant();
const server = new Hapi.Server();
const Boom = require('boom');
const bugle = require('bugle');

server.connection({port:8888});

server.register([
    // REQUIRED: (any session store - see ./examples/hapi-session)
    {
	register: yar,
	options: require('./config/yar.json')
    },
    // REQUIRED: grant, configured for Google Drive login
    {
	register: grant,
	options: require('./config/grant.json')
    },
    // REQUIRED: bugle, grabs google config from grant, and own config from bugle.json
    {
	register: bugle,
	options: require('./config/bugle.json')
    }
], function(err){
    if (err) throw new Error(err);

    server.start((err)=>{
	if (err) throw new Error(err);
	console.log("Server started at:", server.info.uri);
    });
});

Features

  • provides several routes under /a to deal with /a/login, /a/logout, /a/googledriveretry retries, /a/me Google Drive User's profile
  • inherits route /connect/google as alternate login point from grant
  • request decorated with .drive from decorated-google-drive at onPreAuth
  • req.drive and req.drive.x are built from and on top of npm:googleapis
  • req.drive without the .x is a vanilla npm:googleapis.drive client
  • req.drive.x extensions know about
    • the appDataFolder via req.drive.x.appDataFolder.method
    • paths req.drive.x.findPath('/some/nested/path/in/users/drive')
    • how to make paths like mkdir -p does
    • creating upload URLs for later uploads, and doing resumable upload now for bigger files
    • more (see decorated-google-drive)
  • request.drive will use googleapis to try to auto-update its Google Drive OAuth2 access_token from the OAuth2 refresh_token
  • token/cookie management
    • updated Google Drive tokens are pushed out into the session cookie automatically at onPostHandler
    • new Google Drive OAuth2 refresh tokens can be stashed in the user's Drive appDataFolder in an encrypted file for safe keeping
    • subsequent access_token only logins will retrieve the stashed refresh_token as part of drive initialization

Copyright

Copyright 2017 Paul Brewer, Economic and Financial Technology Consulting LLC [email protected]

License: MIT

No relation to Google, Inc.

This software is 3rd party software.

It is not a product of Google, Inc.

Google Drive[tm] is a trademark of Google, Inc.

npm: googleapis is a Google-provided nodejs client for Google APIs such as Google Drive, linked to/referenced from our code. License: Apache 2.0 A copy is not included herein, but running npm install may install one.