1.0.0 • Published 6 months ago

truesign-fastify-hook v1.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
6 months ago

This is a simple Fastify hook for handling requests with Truesign tokens on query string.

Usage example

import { getTruesignHook, TruesignHookConfig } from 'truesign-fastify-hook';

function shouldAcceptToken(
  decryptedToken: DecryptedToken, 
  config: TruesignHookConfig
): boolean {
  if (((Date.now() - decryptedToken.timestamp) / 1000) > config.tokenExpirationTimeSeconds) return false;
  return true;
}

const trueSignOptions: TruesignHookConfig = {
  shouldAcceptToken,
  encryptionKey: process.env.TRUESIGN_KEY,
  allowUnauthenticated: false,
  // we can add more configuration so we have this available in shouldAcceptToekn function
  tokenExpirationTimeSeconds: 30,
};

// when dealing with routes
fastify.addHook('onRequest', getTruesignHook(trueSignOptions));

// or in each secured route
fastify.route({
  method: 'GET',
  url: '/',
  schema: { ... },
  onRequest: getTruesignHook(trueSignOptions),
  // or
  preValidation: getTruesignHook(trueSignOptions),
  // or
  // or others
});

The decrypted token interface is a copy from Truesign docs.

This is a simple package for a simple use case. If you want to extend it to more use cases, plese contribute!

Install

npm install

Run tests

npm run test

Author

👤 Julian Toledo

Show your support

Give a ⭐️ if this project helped you!


This README was generated with ❤️ by readme-md-generator