Coder Social home page Coder Social logo

jeanfw / passport-http Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jaredhanson/passport-http

0.0 1.0 0.0 193 KB

HTTP Basic and Digest authentication strategies for Passport and Node.js.

License: MIT License

JavaScript 99.15% Makefile 0.85%

passport-http's Introduction

Passport-HTTP

HTTP Basic and Digest authentication strategies for Passport.

This module lets you authenticate HTTP requests using the standard basic and digest schemes in your Node.js applications. By plugging into Passport, support for these schemes can be easily and unobtrusively integrated into any application or framework that supports Connect-style middleware, including Express.

Install

$ npm install passport-http

Usage of HTTP Basic

Configure Strategy

The HTTP Basic authentication strategy authenticates users using a userid and password. The strategy requires a verify callback, which accepts these credentials and calls done providing a user.

passport.use(new BasicStrategy(
  function(userid, password, done) {
    User.findOne({ username: userid }, function (err, user) {
      if (err) { return done(err); }
      if (!user) { return done(null, false); }
      if (!user.verifyPassword(password)) { return done(null, false); }
      return done(null, user);
    });
  }
));

Authenticate Requests

Use passport.authenticate(), specifying the 'basic' strategy, to authenticate requests. Requests containing an 'Authorization' header do not require session support, so the session option can be set to false.

For example, as route middleware in an Express application:

app.get('/private', 
  passport.authenticate('basic', { session: false }),
  function(req, res) {
    res.json(req.user);
  });

Examples

For a complete, working example, refer to the Basic example.

Usage of HTTP Digest

Configure Strategy

The HTTP Digest authentication strategy authenticates users using a username and password (aka shared secret). The strategy requires a secret callback, which accepts a username and calls done providing a user and password known to the server. The password is used to compute a hash, and authentication fails if it does not match that contained in the request.

The strategy also accepts an optional validate callback, which receives nonce-related params that can be further inspected to determine if the request is valid.

passport.use(new DigestStrategy({ qop: 'auth' },
  function(username, done) {
    User.findOne({ username: username }, function (err, user) {
      if (err) { return done(err); }
      if (!user) { return done(null, false); }
      return done(null, user, user.password);
    });
  },
  function(params, done) {
    // validate nonces as necessary
    done(null, true)
  }
));

Authenticate Requests

Use passport.authenticate(), specifying the 'digest' strategy, to authenticate requests. Requests containing an 'Authorization' header do not require session support, so the session option can be set to false.

For example, as route middleware in an Express application:

app.get('/private', 
  passport.authenticate('digest', { session: false }),
  function(req, res) {
    res.json(req.user);
  });

Examples

For a complete, working example, refer to the Digest example.

Tests

$ npm install --dev
$ make test

Build Status

Credits

License

The MIT License

Copyright (c) 2011-2013 Jared Hanson <http://jaredhanson.net/>

passport-http's People

Contributors

jaredhanson avatar awick avatar jeanfw avatar julson avatar jrtc27 avatar sameersegal avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.