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

passport-oauth2-password-grant

v0.0.4

Published

OAuth 2.0 password grant authentication strategy for Passport.

Downloads

3,337

Readme

passport-oauth2-password-grant

Build Quality

This module allows authentication through OAuth2 on servers which permit the 'password' grant type. It makes use of the Passport authentication framework to allow easy use by any Express-based application. Like the module on which it is based, passport-oauth2, its functionality is designed to be generic enough for use with any OAuth2-compliant implementation which accepts password grants and can be subclassed for provider-specific functionality, including user profile.

Installation

$ npm install passport-oauth2-password-grant

Usage

Generic Configuration

Like passport-oauth2, the token grant endpoint and OAuth2 client ID are passed as options to the strategy constructor. It also requires a verify() callback, which is called when authentication has succeeded and must call the done() callback when it has finished. verify() accepts one of the following prototypes:

function(accessToken, refreshToken, profile, done);
function(accessToken, refreshToken, requestParams, profile, done);

The following demonstrates how to construct and use a PasswordGrantStrategy object:

var PasswordGrantStrategy = require('passport-oauth2-password-grant');

passport.use(new PasswordGrantStrategy({
	tokenURL: 'https://www.example.com/oauth2/token',
	clientID: EXAMPLE_CLIENT_ID
},
function(accessToken, refreshToken, profile, done) {
	done(null, profile);
});

Additionally, the passReqToCallback and skipUserProfile options may be used, which function identically to the same options for passport-oauth2.

Authentication

This is accomplished through the use of passport.authenticate() with the password-grant strategy. The username and password to be used for authentication are to be passed to passport.authenticate() as the username and password options, respectively. This may be done as in the following example:

function authenticate() {
	return function(req, res, next) {
		var username = req.body.username;
		var password = req.body.password;

		passport.authenticate('password-grant', {
			username: username,
			password: password
		})(req, res, next);
	};
}

app.get('/auth/handler', authenticate(), function(req, res) {
	res.redirect('/');
});

User Profile Retrieval

In order to retrieve profile information for the authenticating user, a subclass of PasswordGrantStrategy must be provided which overrides the PasswordGrantStrategy.userProfile() function with prototype userProfile(accessToken, done). done() should be called as done(err, profile), and profile is then passed to the verify() callback provided during configuration.

Related Modules

Testing

$ npm install
$ npm test

Credits

  • Sean Burke — Module author
  • Jared Hanson – Author of passport-oauth2, from which this module was adapted and upon which this module relies

License

The MIT License