Skip to content

Instantly share code, notes, and snippets.

@igrigorik
Created June 20, 2012 22:44
Show Gist options
  • Save igrigorik/2962677 to your computer and use it in GitHub Desktop.
Save igrigorik/2962677 to your computer and use it in GitHub Desktop.
SPDY basic auth
var spdy = require('spdy'),
fs = require('fs');
var options = {
key: fs.readFileSync(__dirname + '/keys/mykey.pem'),
cert: fs.readFileSync(__dirname + '/keys/mycert.pem'),
ca: fs.readFileSync(__dirname + '/keys/mycsr.pem')
};
var server = spdy.createServer(options, function(req, res) {
var header = req.headers['authorization'] || '',
token = header.split(/\s+/).pop() || '',
auth = new Buffer(token, 'base64').toString(),
parts = auth.split(/:/),
username = parts[0],
password = parts[1];
console.log(parts);
if(username == 'test') {
res.writeHead(200)
res.end('hello world!');
} else {
res.writeHead(401, {'WWW-Authenticate': 'Basic realm="Secure Area"'});
res.end();
}
});
server.listen(44300);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment