GithubHelp home page GithubHelp logo

auth0 / hapi-auth-jwt Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ryanfitz/hapi-auth-jwt

15.0 98.0 12.0 75 KB

JSON Web Token (JWT) authentication plugin

License: Other

JavaScript 98.99% Makefile 1.01%

hapi-auth-jwt's Introduction

hapi-auth-jwt

hapi JSON Web Token (JWT) authentication plugin

JSON Web Token authentication requires verifying a signed token. The 'jwt' scheme takes the following options:

Hapi < 17

Use hapi-auth-jwt < 5 for hapi < 16

Hapi >= 17

Use hapi-auth-jwt >= 5 for hapi >= 17

  • key - (required) The private key the token was signed with.
  • validateFunc - (optional) validation and user lookup function with the signature function(token, callback) where:
    • token - the verified and decoded jwt token
    • callback - a callback function with the signature function(err, isValid, credentials) where:
      • err - an internal error.
      • isValid - true if the token was valid otherwise false.
      • credentials - a credentials object passed back to the application in request.auth.credentials. Typically, credentials are only included when isValid is true, but there are cases when the application needs to know who tried to authenticate even when it fails (e.g. with authentication mode 'try').
  • audience (optional): string or array of strings of valid values for the aud field.
  • issuer (optional): string or array of strings of valid values for the iss field.
  • algorithms (optional): List of strings with the names of the allowed algorithms. For instance, ["HS256", "RS256"].
  • subject (optional): string of valid values for the sub field

See the example folder for an executable example.

const Hapi = require('hapi');
const jwt = require('jsonwebtoken');
const server = new Hapi.Server({ port: 8080 });


const accounts = {
    123: {
        id: 123,
        user: 'john',
        fullName: 'John Doe',
        scope: ['a', 'b']
    }
};


const privateKey = 'BbZJjyoXAdr8BUZuiKKARWimKfrSmQ6fv8kZ7OFfc';

// Use this token to build your request with the 'Authorization' header.
// Ex:
//     Authorization: Bearer <token>
const token = jwt.sign({ accountId: 123 }, privateKey);


const validate = async function (decodedToken, extraInfo) {

    var credentials = accounts[decodedToken.accountId] || {};

    if (!credentials) {
        return { isValid: false };
    }

    return {
      isValid: true,
      credentials
    }
};


await server.register(require('hapi-auth-jwt'));

server.auth.strategy('token', 'jwt', {
    key: privateKey,
    validateFunc: validate
});

server.route({
    method: 'GET',
    path: '/',
    config: {
        auth: 'token'
    }
});

    // With scope requirements
server.route({
    method: 'GET',
    path: '/withScope',
    config: {
        auth: {
            strategy: 'token',
            scope: ['a']
        }
    }
});


await server.start();

You can specify audience, issuer, algorithms and/or subject as well:

server.auth.strategy('token', 'jwt', {
    key: privateKey,
    validateFunc: validate,
    audience: 'http://myapi/protected',
    issuer: 'http://issuer',
    algorithms: ['RS256'],
    subject: 'myRequiredSubject'
});

hapi-auth-jwt's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hapi-auth-jwt's Issues

Working example

Could you please provide a working example? I’ve been trying to put it together but wasn’t able.

is v5 at npmjs.com?

I'm installing v5 using the tarball, but I'm wondering if it's published at npmjs or similar.

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.