Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
fastify-tokenize
Advanced tools
An extremely tiny plugin for Fastify for @cyyynthia/tokenize. Allows you to share the same instance of Tokenize on every part of your server.
Also includes compatibility for the fastify-auth plugin for enhanced experience and flexibility in your Fastify server.
Tokenize removes the pain of generating secure tokens and makes it easy to issue and validate tokens in your application.
pnpm i fastify-tokenize
yarn add fastify-tokenize
npm i fastify-tokenize
This plugin decorates the fastify
instance with a tokenize
object. This object is an instance of Tokenize
initialized with the secret provided.
fastify.register(require('fastify-tokenize'), { secret: 'btw have i told you i use arch' })
fastify-auth
compatibilityYou can make use of the very flexible fastify-auth to authenticate users,
and let fastify-tokenize handle the whole part of authenticating the user. To enable it, just set fastifyAuth
to
true, and compatibility functions will magically get added.
On successful authentications, fastify-tokenize will decorate the request with the user
property. This property can
then be used within your app to greet users with their username or perform more specific checks.
It is mandatory to provide a fetchAccount
option when registering fastify-tokenize. This method will receive the
account ID as unique argument and should the user account (or a promise resolving to a user account). The only
required property is lastTokenReset
(or last_token_reset
) which is used to invalidate tokens generated prior
this date.
// We'll assume we use mongodb as our database here.
fastify.register(require('fastify-auth'))
fastify.register(require('fastify-mongodb'), { url: 'mongodb://localhost:27017/my-awesome-db' })
fastify.register(require('fastify-tokenize'), {
fastifyAuth: true,
fetchAccount: (userId) => fastify.mongo.db.collection('users').findOne({ _id: userId }),
secret: 'btw have i told you i use arch'
})
fastify.route({
method: 'GET',
url: '/secure-place',
// fastify.verifyTokenizeToken is added by fastify-tokenize when fastifyAuth is set to "true"
preHandler: fastify.auth([ fastify.verifyTokenizeToken ]),
handler: (req, reply) => {
req.log.info('Auth route')
reply.send({ hello: 'world' })
}
})
By default, fastify-tokenize checks for either the token
cookie without performing signature checks (will only work if
fastify-cookie) is registered, or a token passed in the authorization
header. You can obviously customize this for yourself through the following options:
cookie
to false will disable authentication through cookies. Same thing for header
cookie
to any string will tell fastify-tokenize to check for this cookie when attempting to authenticate a
requestcookieSigned
to true so fastify-tokenize knows the cookie has to be passed through unsignCookie
header
to null
(default) will attempt to look for a naked tokenheader
to any string will tell fastify-tokenize to only look for specific authorization types
Example: if you set header
to User
, it'll look for authorization: User <token>
You can type the request.user
field just like Fastify lets you type the querystring and various other request metadata:
import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify'
import type { User } from './models'
async function process (request: FastifyRequest<{ TokenizeUser: User }>, reply: FastifyReply) {
if ('user' in request && request.user) {
// typeof request.user is User
}
}
FAQs
A fastify plugin to add Tokenize support through a decorator.
The npm package fastify-tokenize receives a total of 32 weekly downloads. As such, fastify-tokenize popularity was classified as not popular.
We found that fastify-tokenize demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
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.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.