jwt-express.js
function(secret, options) {
        if (!secret) {
            throw new ReferenceError('secret must be defined');
        }
        
        if (typeof secret == 'string') {
            var _secret = secret;
            secret = function(req) {return _secret};
        }
        
        options = options || {};
        var defaults = {
            cookie: 'jwt-express',
            cookieOptions: {
                httpOnly: true
            },
            cookies: true,
            refresh: true,
            reqProperty: 'jwt',
            revoke: function(jwt) {},
            signOptions: {},
            stales: 900000,
            verify: function(jwt) {return true},
            verifyOptions: {}
        };
        
        for (var key in defaults) {
            this.options[key] = options[key] !== undefined? options[key]: defaults[key];
        }
        
        return function(req, res, next) {
            var token;
            if (this.options.cookies) {
                token = req.cookies[this.options.cookie];
            } else if (req.headers.authorization) {
                // Authorization: Bearer abc.abc.abc
                token = req.headers.authorization.split(' ')[1];
            }
            
            var jwt = new JWT(secret(req), this.options);
            req[this.options.reqProperty] = jwt.verify(token);
            
            if (jwt.valid && !jwt.stale && jwt.options.refresh) {
                jwt.resign().store(res);
            }
            
            /**
             * jwt - Creates and signs a new JWT. If cookies are in use, it stores
             *     the JWT in the cookie as well.
             * @param object payload The payload of the JWT
             * @return JWT
             */
            res.jwt = function(payload) {
                var jwt = new JWT(secret(req), this.options);
                return jwt.sign(payload).store(res);
            }.bind(this);
            
            this.clear = function() {
                if (this.options.cookies) {
                    res.clearCookie(this.options.cookie);
                }
            }.bind(this);
            
            next();
        }.bind(this);
    }
function(secret, options) {
        if (!secret) {
            throw new ReferenceError('secret must be defined');
        }
        
        if (typeof secret == 'string') {
            var _secret = secret;
            secret = function(req) {return _secret};
        }
        
        options = options || {};
        var defaults = {
            cookie: 'jwt-express',
            cookieOptions: {
                httpOnly: true
            },
            cookies: true,
            refresh: true,
            reqProperty: 'jwt',
            revoke: function(jwt) {},
            signOptions: {},
            stales: 900000,
            verify: function(jwt) {return true},
            verifyOptions: {}
        };
        
        for (var key in defaults) {
            this.options[key] = options[key] !== undefined? options[key]: defaults[key];
        }
        
        return function(req, res, next) {
            var token;
            if (this.options.cookies) {
                token = req.cookies[this.options.cookie];
            } else if (req.headers.authorization) {
                // Authorization: Bearer abc.abc.abc
                token = req.headers.authorization.split(' ')[1];
            }
            
            var jwt = new JWT(secret(req), this.options);
            req[this.options.reqProperty] = jwt.verify(token);
            
            if (jwt.valid && !jwt.stale && jwt.options.refresh) {
                jwt.resign().store(res);
            }
            
            /**
             * jwt - Creates and signs a new JWT. If cookies are in use, it stores
             *     the JWT in the cookie as well.
             * @param object payload The payload of the JWT
             * @return JWT
             */
            res.jwt = function(payload) {
                var jwt = new JWT(secret(req), this.options);
                return jwt.sign(payload).store(res);
            }.bind(this);
            
            this.clear = function() {
                if (this.options.cookies) {
                    res.clearCookie(this.options.cookie);
                }
            }.bind(this);
            
            next();
        }.bind(this);
    }
init - initialize jwt-express
@param string|function secret If secret is a string, that will be used
to verify / sign with. If secret is a function, that function will be
called with the Express HTTP Request as it's first parameter, and must
return a string which will be used to verify / sign with.
@param object options (Optional) The options of jwt-express
cookie:        The name of the cookie (default: 'jwt-express')
cookieOptions: Options to use when storing the cookie
(default: {httpOnly: true})
cookies:       Boolean indicating to use cookies to look for / save the JWTs
or to use the Authorization header to look for the JWTs (default: true)
refresh:       Boolean indicating if the JWT should be refreshed and stored every request
reqProperty:   The property of req to populate (default: 'jwt')
revoke:        The function to call when jwt.revoke() is called
(default: function(jwt) {})
signOptions:   Options to use when signing the JWT (default: {})
stales:        Milliseconds when the jwt will go stale
verify:        Additional function to call when verifying a JWT
(default: function(jwt) {return true})
verifyOptions: Options to use when verifying the JWT (default: {})
@return function middleware