GithubHelp home page GithubHelp logo

json-web-token's Introduction

json-web-token

JWT encode and decode for Node.js that can use callbacks or by returning an object {error:, value:}

WIKI

JSON Web Token (JWT) is a compact URL-safe means of representing claims to be transferred between two parties. The claims in a JWT are encoded as a JavaScript Object Notation (JSON) object that is used as the payload of a JSON Web Signature (JWS) structure or as the plaintext of a JSON Web Encryption (JWE) structure, enabling the claims to be digitally signed or MACed and/or encrypted.

info & more info

Build StatusCode Coverage 100%ISC License

the version 2.*.* should work only for NodeJS >= 4 for NodeJS 0.10 and 0.12 should install the version 1.6.3

API

jwt#encode(key, payload, [algorithm], cb)
  • key, your secret
  • payload, the payload or Claim Names or an object with {payload, header}

ex:

{
   "iss": "my_issurer",
  "aud": "World",
  "iat": 1400062400223,
  "typ": "/online/transactionstatus/v2",
  "request": {
    "myTransactionId": "[myTransactionId]",
    "merchantTransactionId": "[merchantTransactionId]",
    "status": "SUCCESS"
  }
}

attention that exists some reserved claim names (like "iss", "iat", etc..) check in here for more info about JWT Claims.

  • algorithm, default to 'sha256', use jwt#getAlgorithms() to get the supported algorithms
  • cb, the callback(err[name, message], token)
jwt#decode(key, token, cb)
  • key, your secret
  • token, the JWT token
  • cb, the callback(err[name, message], decodedPayload[, decodedHeader])

Example

var jwt = require('json-web-token');

var payload = {
  "iss": "my_issurer",
  "aud": "World",
  "iat": 1400062400223,
  "typ": "/online/transactionstatus/v2",
  "request": {
    "myTransactionId": "[myTransactionId]",
    "merchantTransactionId": "[merchantTransactionId]",
    "status": "SUCCESS"
  }
};

var secret = 'TOPSECRETTTTT';

// encode
jwt.encode(secret, payload, function (err, token) {
  if (err) {
    console.error(err.name, err.message);
  } else {
    console.log(token);

    // decode
    jwt.decode(secret, token, function (err_, decodedPayload, decodedHeader) {
      if (err) {
        console.error(err.name, err.message);
      } else {
        console.log(decodedPayload, decodedHeader);
      }
    });
  }
});

using the optional reserved headers (alg and typ can't be set using this method)

var settingAddHeaders = {
  payload: {
    "iss": "my_issurer",
    "aud": "World",
    "iat": 1400062400223,
    "typ": "/online/transactionstatus/v2",
    "request": {
      "myTransactionId": "[myTransactionId]",
      "merchantTransactionId": "[merchantTransactionId]",
      "status": "SUCCESS"
    }
  },
  header: {
    kid: 'key ID'
  }
}

jwt.encode(secret, settingAddHeaders, function (err, token) {

})

this projet has been set up with a precommit that forces you to follow a code style, no jshint issues and 100% of code coverage before commit

to run test

npm test

to run jshint

npm run lint

to run code style

npm run style

to run code coverage

npm run coverage

to open the code coverage report

npm run coverage:open

to run benchmarks

npm run bench

to run the source complexity tool

npm run complexity

to open the complexity report

npm run complexity:open

json-web-token's People

Contributors

callmepjs avatar dependabot[bot] avatar joaquimserafim avatar tjconcept avatar

Stargazers

 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

Watchers

 avatar  avatar  avatar  avatar  avatar

json-web-token's Issues

Extend support for additional JWT headers

I've been using this library for my JWT tokens and have found it really useful. Recently came across a requirement to include a key id (kid) of the key used to sign the JWT token in the header section (Ref: Section 5.1 @ http://self-issued.info/docs/draft-jones-json-web-token-01.html)

For example:

	{
	  "alg": "RS256",
	  "typ": "JWT",
	  "kid": "DZjSgUGcezHMQ1rZAtyLhg=="  // Key ID used to sign the token
	}

It would be awesome to extend the library to add additional headers to the JWT header section. I already have a patch ready for review if you think this is a useful addition to your library.

Should we Switch from Auth0 jsonwebtoken to json-web-token ?

Hi @joaquimserafim,

Hope you're having a good weekend.

We are currently using the jsonwebtoken by Auth0: https://github.com/auth0/node-jsonwebtoken - mostly because at the time we were doing our research into JTW: https://github.com/docdis/learn-json-web-tokens - for our Hapi.js JWT Auth plugin: https://github.com/ideaq/hapi-auth-jwt2

Our questions are:

  1. what are the advantages of switching to this module over jsonwebtoken (besides the fact that you have 100% Coverage - which is great!) ... have you benchmarked performance?
    and
  2. Do you plan to support verification (or decoding) options? see:
    https://github.com/auth0/node-jsonwebtoken#jwtverifytoken-secretorpublickey-options-callback

Thanks! ๐Ÿ˜ธ

Catchable errors

It would be nice if I could catch errors from your API and distinguish them from lower level errors.
When I pass a JWT to your .decode method I consider all errors to be user-errors (such as an invalid jwt), but it would be even better, if I could catch these errors specifically.

Or am I doing something wrong?

Handle Token error

If client sends wrong token this error should be caught instead of sending

500  Internal server error
SyntaxError: Unexpected token 

Any way to handle ?

Invalid Signature

Hi,

Whenever I'm checking the token on https://jwt.io/, the site is throwing the error that invalid signature.
And am generating the token as follows,
jwToken.issue({email: mymail})
Do I miss anything?

Thanks in advance.
Shiva.P

Security Issue with dependency

This package uses
"base64-url": "^1.2.2".

Running npm audit command shows:
=== npm audit security report ===
Manual Review
Some vulnerabilities require your attention to resolve
Visit https://go.npm.me/audit-guide for additional guidance

High Out-of-bounds Read
Package base64-url
Patched in >=2.0.0
Dependency of json-web-token
Path json-web-token > base64-url
More info https://nodesecurity.io/advisories/660

Please, would you mind to update it to the latest version?

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.