@therobot/jwt-handler

0.0.2 • Public • Published

@therobot/jwt-handler

Set ENV

  • JWT_ALGORITHM - default RS256
  • JWT_PASSPHRASE - default empty string
  • JWT_PRIVATE_KEY - base64 encoded -----BEGIN ENCRYPTED PRIVATE KEY-----\n ...
  • JWT_PUBLIC_KEY - base64 encoded -----BEGIN PUBLIC KEY-----\n ...
  • JWT_PUBLIC_KEY_FALLBACK - previous value, fallback during key rotation

Usage

Only .sign() can throw errors, data and verify will return { error: String }

  • .data() - token data, memorized, calling it multiple times is ok.
  • token - token parsed from header string (no Bearer)
  • verify(token) - return data, without memorization
  • sign(data) - signs values with private key
const jwtHandler = require('@therobot/jwt-handler')
const context = ({ req }) => {
  return {
    jwt: jwtHandler(req.headers['authorization']),
  }
}
const resolvers = {
  Query: {
    me: async (obj, args, { jwt }) => {
      const authData = jwt.data()
      if (authData.error) {
        throw new Error('Unauthorized: ' + authData.error)
      }
    },
  },
}

Readme

Keywords

none

Package Sidebar

Install

npm i @therobot/jwt-handler

Weekly Downloads

3

Version

0.0.2

License

ISC

Unpacked Size

2.97 kB

Total Files

3

Last publish

Collaborators

  • gcoda