all files / security/ index.js

100% Statements 16/16
100% Branches 4/4
100% Functions 3/3
100% Lines 16/16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37                                          
var jwt = require('jsonwebtoken');
 
module.exports = {
 
    inbound: function(req, res, next) {
        self = this;
 
        // first check for a header
        if (req.headers['x-jetstream-jwt']) {
            // now we need to check the jwt is legit
            var token = req.headers['x-jetstream-jwt'];
            jwt.verify(token, jetstreamConfig.jwt_secret, { issuer: jetstreamConfig.registry_url, audience: jetstreamConfig.location }, function(err, decoded) {
                if (decoded) {
                    next();
                } else {
                    res.status(401).send("Unauthorized")
                    console.log("Error: " + err);
                    console.log("Token: " + token);
                    console.log("Expected registry: " + jetstreamConfig.registry_url);
                    console.log("Expected audience: " + jetstreamConfig.location);
                }
            });
        } else {
            res.status(401).send("Unauthorized")
            console.log("Unauthorized: No header");
        }
    },
    outbound: function() {
        return jwt.sign({},
            jetstreamConfig.jwt_secret, {
                issuer: jetstreamConfig.location, // iss - issuer
                audience: jetstreamConfig.registry_url, // aud - audience
                expiresIn: 5,       // exp - expires after
                noTimestamp: false  // iat - issued at
        });
    }
}