This is a simple Fastify hook for handling requests with Truesign tokens on query string.
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!
npm install
npm run test
👤 Julian Toledo
- Twitter: @julipan37
- Github: @julianitor
Give a ⭐️ if this project helped you!
This README was generated with ❤️ by readme-md-generator