Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
fastify-jwt
Advanced tools
JWT utils for Fastify, internally uses jsonwebtoken.
npm i fastify-jwt --save
Register as a plugin. This will decorate your fastify
instance with the standard jsonwebtoken methods decode
, sign
, and verify
; refer to their documentation to find how to use the utilities. It will also register request.jwtVerify
and reply.jwtSign
. You must pass a secret
when registering the plugin.
const fastify = require('fastify')
fastify.register(require('fastify-jwt'), {
secret: 'supersecret'
})
fastify.post('/signup', (req, reply) => {
// some code
const token = fastify.jwt.sign({ payload })
reply.send({ token })
})
fastify.listen(3000, err => {
if (err) throw err
})
fastify-jwt
is a fastify plugin. You must pass a secret
to the options
parameter. The secret
can be a primitive type String or a function that returns a String. Function based secret
is supported by the request.jwtVerify()
and reply.jwtSign()
methods and is called with request
, reply
, and callback
parameters.
const fastify = require('fastify')()
const jwt = require('fastify-jwt')
// secret as a string
fastify.register(jwt, { secret: 'supersecret' })
// secret as a function
fastify.register(jwt, {
secret: function (request, reply, callback) {
// do something
callback(null, 'supersecret')
}
})
The sign
method is an implementation of jsonwebtoken .sign()
. Can be used asynchronously by passing a callback function; synchronously without a callback.
The verify
method is an implementation of jsonwebtoken .verify()
. Can be used asynchronously by passing a callback function; synchronously without a callback.
const token = fastify.jwt.sign({ foo: 'bar' })
// synchronously
const decoded = fastify.jwt.verify(token)
// asycnhronously
fastify.jwt.verify(token, (err, decoded) => {
if (err) fastify.log.error(err)
fastify.log.info(`Token verified. Foo is ${decoded.foo}`)
})
The decode
method is an implementation of jsonwebtoken .decode()
. Can only be used synchronously.
const token = fastify.jwt.sign({ foo: 'bar' })
const decoded = fastify.jwt.decode(token)
fastify.log.info(`Decoded JWT: ${decoded}`)
For your convenience, the secret
you specify during .register
is made available via fastify.jwt.secret
. request.jwtVerify()
and reply.jwtSign()
will wrap non-function secrets in a callback function. request.jwtVerify()
and reply.jwtSign()
use an asynchronous waterfall method to retrieve your secret. It's recommended that your use these methods if your secret
method is asynchronous.
These methods are very similar to their standard jsonwebtoken counterparts.
const fastify = require('fastify')()
fastify.register(jwt, {
secret: function (request, reply, callback) {
// do something
callback(null, 'supersecret')
}
})
fastify.post('/sign', function (request, reply) {
reply.jwtSign(request.body.payload, function (err, token) {
return reply.send(err || { 'token': token })
})
})
fastify.get('/verify', function (request, reply) {
request.jwtVerify(function (err, decoded) {
return reply.send(err || decoded)
})
})
fastify.listen(3000, function (err) {
if (err) fastify.log.error(err)
fastify.log.info(`Server live on port: ${fastify.server.address().port}`)
// sign payload and get JWT
request({
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: {
payload: {
foo: 'bar'
}
},
uri: `http://localhost:${fastify.server.address().port}/sign`,
json: true
}, function (err, response, body) {
if (err) fastify.log.error(err)
fastify.log.info(`JWT token is ${body}`)
// verify JWT
request({
method: 'GET',
headers: {
'Content-Type': 'application/json',
authorization: 'Bearer ' + sign.token
},
uri: 'http://localhost:' + fastify.server.address().port + '/verify',
json: true
}, function (err, response, body) {
if (err) fastify.log.error(err)
fastify.log.info(`JWT verified. Foo is ${body.bar}`)
})
})
})
This project is kindly sponsored by:
Licensed under MIT.
FAQs
`fastify-jwt@4.2.0` has been deprecated. Please use `@fastify/jwt@5.0.0` instead.
The npm package fastify-jwt receives a total of 5,079 weekly downloads. As such, fastify-jwt popularity was classified as popular.
We found that fastify-jwt demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 17 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.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.