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

google-oauth-server

v1.0.40

Published

OAuth Server for authenticating users to access google apis

Downloads

4

Readme

GoogleOAuthServer

Known Vulnerabilities

OAuth server to allow authentication with google

This node server in this project provides the following:

  • builds a url that will open the Google auth page that a user logs into Google with
  • exchanges the code provided from the step above for a valid access_token
  • stores a refresh_token in a database so that it is available for the account no matter what device the user is on.
  • refreshes the access_token when it expires using the refresh_token stored in the database.

The client provides the following:

  • navigates to the Google Authorisation page using the url provided by the server
  • exchanges the code for a valid access_token
  • uses the access_token to make authorised requests to a Google api
  • automatically refreshes tokens as part of a request if the token has expired
  • revokes tokens to support log out

The client does NOT store the tokens between sessions. It is up to the developer to do this - it is suggested to use local storage and not cookies.

Usage

As standalone server

You can check this project out and run it as a standalone server that will provide authenticated keys for a client application.

Prerequisites

To run this project you will need:

  • Credentials from the Google Developers Console (client_id and client_secret)
  • a mongoDB instance - used for storing refresh tokens

Installation

Clone the github repo

git clone https://github.com/Roaders/GoogleOAuthServer.git

Install dependencies. This will also compile the typescript files.

npm install

Set environment variables or update devDependencies.env (this will set environment variables).

You will need to enter your CLIENT_ID and CLIENT_SECRET from the Google Developers Console. Enter any required authorisation SCOPES that your application will use and you will need to enter the MONGODB_URI of your mongoDB.

Other environmental variables are:

  • TOKEN_COLLECTION_NAME to specify the name of the collection to store the tokens in in the mongoDB
  • permittedOrigin optionally specify this to allow applications on another domain to access the server

Start the server

npm start

In non-prod environments (anything where the environment variable NODE_ENV is not production) there will be a test harness app available at http://localhost:8080 (assuming you are still running on port 8080). For this test app to work you must add the scope https://www.googleapis.com/auth/youtube.readonly to the SCOPES environment variable. This app will load a list of YouTube videos for a channel that belongs to the account that you authenticate.

Installing via npm

The project can be installed as a dependency in another project. An external project may wish to use the client code to make authorised requests or it may use the server code to embed the authorisation server in an existing express app.

Install the project as a dependency using npm:

npm install google-oauth-server --save

Server

Import the package:

import googleOAuth = require("google-oauth-server");

Instantiate server:

var dbConnection = new googleOAuth.DataBaseConnection();
var authServer = new googleOAuth.GoogleOAuthServer(dbConnection);

Set up express routes to handle authorisation requests:

var app = express();

app.get( "/auth/*", (req: express.Request, res: express.Response) => {

	authServer.handleExpressRequest(req)
		.subscribe(
			result => {
				res.send(result);
			},
			error => {
				console.log(`Error: ${error}`);
				res.status(500).send(`{"error": "${error}"}`);
			}
		);
});

Client

Import client:

import {GoogleOAuthClient, IAuthToken} from "google-oauth-server/dist/browser";

Store tokens when they are created or refreshed:

var tokens: IAuthToken;
var authClient = new GoogleOAuthClient("http://urlOfMyServer.com/routeToServer");

authClient.createTokensStream()
	.do( result => tokens = result)
	.subscribe();

Make a request:

authClient.makeRequest("channels?part=id&mine=true", tokens);