Code Monkey home page Code Monkey logo

jwt's Introduction

Json Web Tokens

Importance

  • allows for authentication by checking against a secret key on the server instead of using expensive database queries and other network operations.

Security

The way you store a JWT will mean different things for your web apps security. There are three main risk correlated to the storage of a JWT and they are as follows:

  • Man in the Middle Attacks --- we have to ensure that our app only works over https to prevent possible sniffing of our token over public wifi networks.

  • Cross-Site Scripting(JS injection/XSS) Attacks --- Because the local storage/session is accessible through JS API, it makes it vulnerable to rouge JS code in our browser. If this occurs, our session data will be compromised. Because of this it is suggested to utilize cookies to store our tokens.

  • Cross-Site Request Forgery (CSRF) Attacks --- Although XSS and Man in the middle attacks can be prevented by strictly using https-only flags in our cookies, this does not prevent CSRF Attacks which can be done by sending request to our site from an unknown source or domain. A solution for this would be to specify Same-Site cookie attribute to strict, but doing this may impact the functionality of your application. So discretion is advised.

  • it is important to note that when a token is created, we cannot magically or remotely change the expiration of these tokens, so to revoke privileges for a given token, we would have to make each token short lived and revalidate the token on each request or generate a new one if it is expired and the user still has permission to access the application. This seems to be the most ideal way of handling the revocation of user privileges.

Structure

header.payload.signature

  • Header --- Will determine how our JWT signature will be generated
  • Payload(Claims) --- Contains the information about the user. This is trivially encoded and should NOT contain sensitive information, just what we need to validate a user and other options related to the token. i.e expiration date.
  • Signature is base64 encoded string of our header and payload(Utilizes a secret key when generate on our server).

Example

import JWTModule from "../jwt/"
const jwt = new JWTModule()

const payload: JWTModule.Payload = {
  username: "root",
  email: "[email protected]",
}

const token = jwt.generate({ payload }) // Returns new JWT token
const decode = jwt.decode(token) // Returns decoded payload
const valid = jwt.verify(token) // Returns true or false if token signature is valid

console.table({
  token,
  decode,
  valid,
})

jwt's People

Contributors

makanamakesstuff avatar

Stargazers

 avatar Micheal Pearce avatar

Watchers

 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.