Skip to content
 
 

Repository files navigation

@passport-next/passport-strategy

Build Status Coverage Status Maintainability Dependencies

An abstract class implementing Passport's strategy API.

Install

npm install @passport-next/passport-strategy

Usage

This module exports an abstract Strategy class that is intended to be subclassed when implementing concrete authentication strategies. Once implemented, such strategies can be used by applications that utilize Passport middleware for authentication.

Subclass Strategy

Create a new CustomStrategy constructor which inherits from Strategy:

import { Strategy } from '@passport-next/passport-strategy';

/**
 *
 */
class CustomStrategy extends Strategy {
  /**
   *
   */
  constructor(/* ... */) {
    super();
    doSomething();
  }
}

Implement Authentication

Implement authenticate(), performing the necessary operations required by the authentication scheme or protocol being implemented.

import { Strategy } from '@passport-next/passport-strategy';

/**
 *
 */
class CustomStrategy extends Strategy {
  /**
   *
   * @param {object} req
   * @param {object} options
   */
  #authenticateRequest(req, options) {
    try {
      // TODO: authenticate request
      return findUser(req, options); // Returns user object or false if not found
    } catch {
      this.error(new Error('Database error during authentication'));
      return false;
    }
  }

  // ...
  /**
   * @param req
   * @param options
   */
  authenticate(req, options) {
    const user = this.#authenticateRequest(req, options);
    if (user) {
      this.success({
        username: ''
      });
    } else {
      this.fail();
    }
  }
}

See "API" below for additional expected properties and methods.

API

Instance properties

Passport will identify mounted strategies by the instance's name attribute, so be sure to set one in the constructor:

import { Strategy } from '@passport-next/passport-strategy';

class CustomStrategy extends Strategy {
  constructor() {
    super();
    this.name = 'custom'; // set instance name
  }
}

Later, when a user calls passport.authenticate to acquire the authentication middleware that employs this strategy, the value of this name attribute is what must be passed in as the first argument (as a string or an array of strings):

const authMiddleware = passport.authenticate('custom');

Augmented Methods

The Strategy.authenticate method is called on an instance of this Strategy which is augmented with the following action functions.

These action functions are bound via closure the the request/response pair.

The end goal of the strategy is to invoke one of these action methods, in order to indicate successful or failed authentication, redirect to a third-party identity provider, etc.

strategy.success(user, info)

Authenticate user, with optional info.

Strategies should call this method to successfully authenticate a user. user should be an object supplied by the application after it has been given an opportunity to verify credentials. info is an optional argument containing additional user information. This is useful for third-party authentication strategies to pass profile details.

Kind: instance method of Strategy API: public

Param Type
user object
info object

strategy.fail(challenge, status)

Fail authentication, with optional challenge and status, defaulting to 401.

Strategies should call this function to fail an authentication attempt.

Kind: instance method of Strategy API: public

Param Type
challenge string|{type?: string, message: string}
status number

strategy.redirect(url, status)

Redirect to url with optional status, defaulting to 302.

Strategies should call this function to redirect the user (via their user agent) to a third-party website for authentication.

Kind: instance method of Strategy API: public

Param Type
url string
status number

strategy.pass()

Pass without making a success or fail decision.

Under most circumstances, Strategies should not need to call this function. It exists primarily to allow previous authentication state to be restored, for example from an HTTP session.

Kind: instance method of Strategy API: public

strategy.error(err)

Internal error while performing authentication.

Strategies should call this function when an internal error occurs during the process of performing authentication; for example, if the user directory is not available.

Kind: instance method of Strategy API: public

Param Type
err Error

Related Modules

Tests

$ npm install
$ npm test

About

An abstract class implementing Passport's strategy API.

Resources

Stars

0 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages