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.
express-jwt-permissions
Advanced tools
Middleware that checks JWT tokens for permissions, recommended to be used in conjunction with express-jwt.
npm install express-jwt-permissions --save
This middleware assumes you already have a JWT authentication middleware such as express-jwt.
The middleware will check a decoded JWT token to see if a token has permissions to make a certain request.
Permissions should be described as an array of strings inside the JWT token, or as a space-delimited OAuth 2.0 Access Token Scope string.
"permissions": [
"status",
"user:read",
"user:write"
]
"scope": "status user:read user:write"
If your JWT structure looks different you should map or reduce the results to produce a simple Array or String of permissions.
To verify a permission for all routes using an array:
var guard = require('express-jwt-permissions')()
app.use(guard.check('admin'))
If you require different permissions per route, you can set the middleware per route.
var guard = require('express-jwt-permissions')()
app.get('/status', guard.check('status'), function(req, res) { ... })
app.get('/user', guard.check(['user:read']), function(req, res) { ... })
Logical combinations of required permissions can be made using nested arrays.
Single string
// Required: "admin"
app.use(guard.check(
'admin'
))
Array of strings
// Required: "read" AND "write"
app.use(guard.check(
['read', 'write']
))
Array of arrays of strings
// Required: "read" OR "write"
app.use(guard.check([
['read'],
['write']
]))
// Required: "admin" OR ("read" AND "write")
app.use(guard.check([
['admin'],
['read', 'write']
]))
To set where the module can find the user property (default req.user
) you can set the requestProperty
option.
To set where the module can find the permissions property inside the requestProperty
object (default permissions
), set the permissionsProperty
option.
Example:
Consider you've set your permissions as scope
on req.identity
, your JWT structure looks like:
"scope": "user:read user:write"
You can pass the configuration into the module:
var guard = require('express-jwt-permissions')({
requestProperty: 'identity',
permissionsProperty: 'scope'
})
app.use(guard.check('user:read'))
The default behavior is to throw an error when the token is invalid, so you can add your custom logic to manage unauthorized access as follows:
app.use(guard.check('admin'))
app.use(function (err, req, res, next) {
if (err.code === 'permission_denied') {
res.status(403).send('Forbidden');
}
});
Note that your error handling middleware should be defined after the jwt-permissions middleware.
This library has integration with express-unless to allow excluding paths, please refer to their usage.
const checkForPermissions = guard
.check(['admin'])
.unless({ path: '/not-secret' })
app.use(checkForPermissions)
$ npm install
$ npm test
This project is licensed under the MIT license. See the LICENSE file for more info.
FAQs
Express middleware for JWT permissions
The npm package express-jwt-permissions receives a total of 9,498 weekly downloads. As such, express-jwt-permissions popularity was classified as popular.
We found that express-jwt-permissions 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.
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.