Skip to content

Simple Fastify hook for handling requests with truesign tokens on query string

Notifications You must be signed in to change notification settings

julianitor/truesign-fastify-hook

Repository files navigation

Welcome to truesign-fastify-hook 👋

Version License: ISC Twitter: julipan37

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 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

About

Simple Fastify hook for handling requests with truesign tokens on query string

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published