Created
February 9, 2012 18:39
-
-
Save mcavage/1781903 to your computer and use it in GitHub Desktop.
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 restify = require('restify'); | |
///--- Globals | |
var NotAuthorizedError = restify.NotAuthorizedError; | |
///--- Helpers | |
function authenticate(req, res, next) { | |
var authz = req.authorization; | |
console.log('%j', authz) | |
if (authz.scheme !== 'Basic' || | |
authz.basic.username !== 'root' || | |
authz.basic.password !== 'secret') { | |
return next(new NotAuthorizedError('%s failed to authenticate', | |
req.authorization.username)); | |
} | |
return next(); | |
} | |
///--- Mainline | |
var server = restify.createServer(); | |
server.use(restify.authorizationParser()); | |
server.use(authenticate); | |
server.get('/hello/:name', function(req, res, next) { | |
res.send(req.params.name); | |
return next(); | |
}); | |
server.listen(8080, function() { | |
console.log('server listening at %s', server.url); | |
console.log('\nTry:\n curl -is localhost:8080/hello/jerry'); | |
console.log('\nAnd:\n curl -is -u root:secret localhost:8080/hello/jerry'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment