Coder Social home page Coder Social logo

jsonwebtokenexample's Introduction

jsonWebTokenExample

Example of using JWT (JSON Web Tokens) to generate an access token as if it came from an Auth server such as a OAuth2 provider. This uses a public/private key pair based on a Elliptic Curve Asymmetric algorithm. The private key is used to encrypt tokens on the Auth server and the public key is available for download by any service needing to verify tokens on a continuous basis without wanting to make additional roundtrips to the Auth server.

More information on JWT, Elliptic Curve, RSA private/public keys, OAuth2, and node web token

Install and Run

git clone https://github.com/FrankHassanabad/jsonWebTokenExample.git
cd jsonWebTokenExample
npm install
node app.js

Output should be something like the following:

Reading private key of: keys/private_key.pem
Reading public key of: keys/public_key.pem

Generated token from private key (size 375):

eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJqdGkiOiI4ODQ0NGFjNS1jZGI3LTRkMjctYjIyMS1kZTMyZDY0Zjc5YWQiLCJpc3MiOiJTdHJpbmcgb2YgeW91ciBBdXRoIFNlcnZlciB3aGljaCBnZW5lcmF0ZWQgdGhpcyIsImF1ZCI6IkNsaWVudCBJRCBvZiB3aG8gdGhpcyBpcyBpbnRlbmRlZCBmb3IiLCJleHAiOjE0NDI3OTY0NDc3NTcsImlhdCI6MTQ0Mjc5NjIzMTc1N30.59813RD2iKWcL5av9GgW600ubf22EDo8CmtEcWqMXLkHy-ivX5VU5Ainpg8IvvSC0TPqKvbRcImiVOcYXidwFg

Decoded token from public key is:
{
  "jti": "88444ac5-cdb7-4d27-b221-de32d64f79ad",
  "iss": "String of your Auth Server which generated this",
  "aud": "Client ID of who this is intended for",
  "exp": 1442796447757,
  "iat": 1442796231757
}

Check out the shell script in the keys directory to generate new elliptic curve or RSA private/public keys. You can play with the different elliptic curve types or sizes of RSA to see the different length differences of the tokens.

Don't deploy any checked-in private key to a server (obviously).

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.