@mertcanyalhi/express-auth-token v0.0.1
express-auth-token
express-auth-token is an Express middleware, which authenticates users by using JSON Web Token.
Install
npm install express-auth-tokenUsage
Initializing
Options:
parsersArray of token parsers, which will be used to parse the token from the request.secretSecret for JWT HMAC algorithms, which is either string, buffer, or object.privateKeyPEM encoded private key for RSA and ECDSA for JWT.publicKeyPEM encoded public key for RSA and ECDSA for JWT.jwtOptionsJWT options, which will be passed to the JWTverifyandsignfunctions.jwtOptions.verifyOptions for theverifyJWT function. Please refer to the JWT library for the options.jwtOptions.signOptions for thesignJWT function. Please refer to the JWT library for the options.rolesKeyKey for roles within the signed data. When theauthorizemiddleware is added to the Express route, roles will be retrieved from the value of the key under the signed data.userKeyKey for the user (user id/username/etc.) within the signed data. If used, req.user will be set to the value of the key under the signed data.failureRedirectRedirection path, which will be used for redirecting the failed authentications/authorizations.failureCallbackCallback function, which will be used for the failed authentications/authorizations.
Notes:
- Either
secretorpublicKey/privateKeypair should be given during initialization. - At least one token parser should be given under
parsersoption. - In case if there are multiple token parsers:
- Parsers will be used in the given order.
- If a parser successfuly parses the token from the request, following parsers will not be used for the current request.
- If the
rolesKeyoption is set, corresponding key in the token data should contain an array of roles. - If the
userKeyoption is set, corresponding key in the token data should contain the user information (user id/username/etc.).
Including the library:
var expressAuthToken = require('express-auth-token');Initializing with secret:
app.use(expressAuthToken.init({
parsers: [ expressAuthToken.parsers.AuthHeader ],
secret: 'VerySecretKey'
}));Initializing with private/public key:
app.use(expressAuthToken.init({
parsers: [ expressAuthToken.parsers.AuthHeader ],
publicKey: fs.readFileSync('./public.pem'),
privateKey: fs.readFileSync('./private.key'),
jwtOptions: {
sign: {
algorithm: 'RS256',
expiresIn: 60 * 60 * 24 * 365 // 1 year
}
}
}));Setting failureCallback:
app.use(expressAuthToken.init({
parsers: [ expressAuthToken.parsers.AuthHeader ],
secret: 'VerySecretKey',
failureCallback: function(req, res, next) {
res.json({
status: false,
error: req.auth.error
});
}
}));An optional verifyCallback can be supplied, which will allow you to perform user verification for the parsed token. express-auth-token will
always extract the signed data from the provided token, but it may be required to verify the user and/or token session in the application logic.
app.use(expressAuthToken.init({
parsers: [ expressAuthToken.parsers.AuthHeader ],
secret: 'VerySecretKey'
}, function(token, decoded, callback) {
Session.findOne({
userId: decoded.userId,
token: token
}, function(err, session) {
if (!session) {
return callback('SessionNotFound', false);
}
return callback(null, true);
});
}));verifyCallback can also be set after the initialization. This will replace the verifyCallback defined during the initialization in case if it was set.
expressAuthToken.verify(function(token, decoded, callback) {
Session.findOne({
userId: decoded.userId,
token: token
}, (err, session) => {
if (!session) {
return callback('SessionNotFound', false);
}
return callback(null, true);
});
});Authenticating/Authorizing requests
By default, all requests are allowed. express-auth-token will only try to parse the request, and extract the signed data within the token. In case if you want to authenticate
or authorize a request, you can use the authenticate or authorize middlewares.
authenticate middleware will only allow authenticated users to access the Express route. If the user is not authenticated, a 401 response code will be returned.
app.get('/private', expressAuthToken.authenticate());A custom callback can be defined for the failed requests:
app.get('/private', expressAuthToken.authenticate(function(req, res, next) {
res.json({
message: 'Access denied!'
});
}));authorize middleware will only allow users having the defined role. If the user does not have the required role, a 403 response code will be returned.
app.get('/private', expressAuthToken.authorize('administrator'));A custom callback can be defined for the failed requests:
app.get('/private', expressAuthToken.authorize('administrator', function(req, res, next) {
res.json({
message: 'Access denied!'
});
}));Notes:
- If the callback is defined under the
authenticate/authorizecall,failureCallbackcallback will not be called. - If the
failureRedirectoption is set while initializing express-auth-token, failed requests will be redirected to the given path unless there is a callback.
Parsers
express-auth-token uses parsers to parse the token from the incoming request. Parsers are extendable; you can create your own parser to parse the token from the request.
Currently supported parsers:
Authentication Header
- Parses the token from the authentication header. By default, it uses the
Bearerscheme.Authorization: Bearer <TOKEN> Scheme can be easily modified:
expressAuthToken.parsers.AuthHeader.options.authScheme = 'MyScheme';Authorization: MyScheme <TOKEN>
- Parses the token from the authentication header. By default, it uses the
Writing a custom parser
You can create a custom parser by using the following template. express-auth-token will call the parse function, and will provide the Express request object as a parameter.
var options = {
headerKey: 'authkey'
};
var parse = function(req) {
return req.headers[options.headerKey];
};
module.exports = {
parse,
options
};You can include your custom parser while initializing express-auth-token:
app.use(expressAuthToken.init({
parsers: [
expressAuthToken.parsers.AuthHeader,
myCustomParser
],
secret: 'VerySecretKey'
}));Tests
npm install
npm testTo generate test-coverage reports:
npm install -g istanbul
npm run-script coverage
istanbul reportLicense
The MIT License
Copyright (c) 2019 Mert Can Yalhi [http://mert.co](http://mert.co)
6 years ago