Created
February 3, 2015 20:05
-
-
Save kriswill/6f27299f95d8e1fe86a1 to your computer and use it in GitHub Desktop.
api/service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var jwt = require('jsonwebtoken') | |
module.exports = { | |
issue: function(payload) { | |
sails.log.silly(__filename + ':' + __line + ' [Service.Passport.deserializeUser() called]') | |
return jwt.sign(payload, sails.config.jwt.secret) | |
}, | |
verify: function(token, next) { | |
return jwt.verify(token, sails.config.jwt.secret, {}, next) | |
}, | |
getToken: function(req, next, throwError) { | |
var token = '' | |
if (req.headers && req.headers.authorization) { | |
var parts = req.headers.authorization.split(' ') | |
if (parts.length === 2) { | |
var scheme = parts[0] | |
var credentials = parts[1] | |
if (/^Bearer$/i.test(scheme)) { | |
token = credentials | |
} | |
} else if (throwError) { | |
throw new Error('Invalid authorization header format. Format is Authorization: Bearer [token]') | |
} | |
} else if (req.param('token')) { | |
token = req.param('token') | |
} else if (throwError) { | |
throw new Error('No authorization header was found') | |
} | |
return sails.services['token'].verify(token, next) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment