Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use restify-errors for use with restify > 4.x #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
var jwt = require('jsonwebtoken');
var unless = require('express-unless');
var restify = require('restify');
var restifyErrors = require('restify-errors');
var async = require('async');

var InvalidCredentialsError = require('restify-errors').InvalidCredentialsError;

var DEFAULT_REVOKED_FUNCTION = function(_, __, cb) { return cb(null, false); };

var getClass = {}.toString;
Expand Down Expand Up @@ -61,16 +60,16 @@ module.exports = function(options) {
if (/^Bearer$/i.test(scheme)) {
token = credentials;
} else {
return next(new InvalidCredentialsError('Format is Authorization: Bearer [token]'));
return next(new restifyErrors.InvalidCredentialsError('Format is Authorization: Bearer [token]'));
}
} else {
return next(new InvalidCredentialsError('Format is Authorization: Bearer [token]'));
return next(new restifyErrors.InvalidCredentialsError('Format is Authorization: Bearer [token]'));
}
}

if (!token) {
if (credentialsRequired) {
return next(new InvalidCredentialsError('No authorization token was found'));
return next(new restifyErrors.InvalidCredentialsError('No authorization token was found'));
} else {
return next();
}
Expand All @@ -94,13 +93,13 @@ module.exports = function(options) {
if (err) { return next(err); }
var revoked = results[1];
if (revoked){
return next(new restify.UnauthorizedError('The token has been revoked.'));
return next(new restifyErrors.UnauthorizedError('The token has been revoked.'));
}

var secret = results[0];

jwt.verify(token, secret, options, function(err, decoded) {
if (err && credentialsRequired) return next(new InvalidCredentialsError(err));
if (err && credentialsRequired) return next(new restifyErrors.InvalidCredentialsError(err));

req[_requestProperty] = decoded;
next();
Expand Down