Coder Social home page Coder Social logo

express-header-token-auth's Introduction

Express Header Token Authentication

This package parses Authorization headers from an Express request object for an authorization token.

Installation

npm install express-header-token-auth

Usage

Token validation can be done through a routing middleware or part of the routed action.

Incoming Payload can be simulated by:

curl -IH "Authorization: Token token=test;param1=option1;param2=option2" http://myapp.com/protected/resource

Accepted Authorization Header formats are:

Authorization: Token token=test,param1=option1,param2=option2
Authorization: Token token="test",param1="option1",param2="option2"
//Any variation of commas(,), semicolons(;) or tabs(   ) are accepted as delimiters

As routing middleware

router.use('/protected/resource', function(req, res, next) {
  new TokenAuth(req, res).authenticateOrRequestWithHttpToken(function(err, token, options) {
    if (!err) {
      if (isValidToken(token, options))
        next();
      else {
        // send out invalid response
      }
    }
    // `else` case is not needed. If `err` is present, a HTTP response with status 401 will automatically be sent out.
  });
});

As action

router.get('/protected/resource', function(req, res, next) {
  new TokenAuth(req, res).authenticateOrRequestWithHttpToken(function(err, token, options) {
    if (!err) {
      if (isValidToken(token, options))
        // valid response
      else {
        // send out invalid response
      }
    }
    // `else` case is not needed. If `err` is present, a HTTP response with status 401 will automatically be sent out.
  });
});

Methods

authenticateOrRequestWithHttpToken([realm], callback)

Used to grab token and options from callback. When token is not found, HTTP status 401 challenge is automatically sent. The realm parameter defaults to Application, which is used when challenging for token.

authenticate(callback, [callbackOnError])

Similar to authenticateOrRequestWithHttpToken, except the HTTP status 401 challenge will not be sent on failure to identify token. User is responsible for deciding the next course of action. The callbackOnError parameter defaults to true and should rarely be needed.

requestHttpTokenAuthentication(realm)

Issues HTTP Status 401 challenge to current Response object.

express-header-token-auth's People

Contributors

kerncheh avatar

Stargazers

Andrew Clancy avatar Kenny Yap avatar  avatar

Watchers

James Cloos avatar  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.