New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

@luckbox/token-data-middleware

Package Overview
Dependencies
Maintainers
2
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@luckbox/token-data-middleware

Validates and extracts data from signed tokens

latest
Source
npmnpm
Version
1.2.1
Version published
Maintainers
2
Created
Source

token-data-middleware

Validates and extracts data from signed tokens

Examples

Express 4.x middleware

const express = require('express');
const fs = require('fs');

const app = express();

const { tokenData } = require('@luckbox/token-data-middleware');
const publicKey = fs.readFileSync('id_ecdsa.pub.pem');

app.use(tokenData(publicKey, 'Custom-Token-Header'));

app.post('/message', (req, res) => {
  if (!req.tokenData.id) {
    return res.status(403).send('Permission denied');
  }

  // new message logic goes here...
});

app.listen(80);

Manual usage

const fs = require('fs');

const { parser } = require('@luckbox/token-data-middleware');
const publicKey = fs.readFileSync('id_ecdsa.pub.pem');

const tokenParser = parser(publicKey);

const token = 'qZPb3DyNIOnTeRv4oSy5TraslRO41AYjDlxSttpW5PWIcKIwqvlGDgMWPjhbIKpdcoX6mfsG9dwC-JRz5wbVAgeyJpZCI6MTIzNDV9';

const tokenData = tokenParser(token);
console.log(tokenData); // { id: 12345 }

API

Methods

module#parser(publicKey) : tokenParser()

Initializes a new token parser.

NameTypeDescription
publicKey`StringBuffer`

module#tokenData(publicKey[, headerName]) : ExpressMiddleware

Creates a new Express 4.x middleware.

NameTypeDefaultDescription
publicKey`StringBuffer`
headerNameString"Token"An optional name for the header from which to extract the token data

sign(payload, privateKey) : Object

Signs a payload and generates a token provided an ECDSA private key.

NameTypeDescription
payloadanyPayload to sign
privateKeyStringPEM-formatted private key

tokenParser(token) : Object

Parses a signed token and returns the data contained within. In case the token is invalid, an empty object is returned.

NameTypeDescription
publicKeyStringPEM-formatted public key

FAQs

Package last updated on 07 Mar 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts