Skip to content

Instantly share code, notes, and snippets.

@just-boris
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save just-boris/a017bbbff626f954b8f4 to your computer and use it in GitHub Desktop.

Select an option

Save just-boris/a017bbbff626f954b8f4 to your computer and use it in GitHub Desktop.
HTTP auth error reproduce
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>test</title>
<script src="http://code.jquery.com/jquery-2.1.1.js"></script>
<script>
$.ajax({
method: 'GET',
url: '/login',
headers: { 'authorization': 'Basic YmFkLWxvZ2luOnBhc3M=' },
success: function() {
$('body').html('ok');
}
});
</script>
</head>
<body>
</body>
</html>
{
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.4.4"
}
}
var express = require('express'),
fs = require('fs');
var app = express();
function atob(str) {
return new Buffer(str, 'base64').toString('binary');
}
app.get('/login', function(req, res){
"use strict";
if(req.headers.authorization) {
var auth = req.headers.authorization.split(' ').pop();
var login = atob(auth).split(':');
console.log((new Date()).toJSON() + ': ' + login[0]);
if(login[0] === 'good-login') {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
return;
}
}
res.setHeader('WWW-Authenticate', 'Basic realm="login-me"');
res.statusCode = 401;
res.end('');
});
app.get('/', function(req, res) {
"use strict";
fs.createReadStream('index.html', 'utf-8').pipe(res);
});
var server = app.listen(1337, function() {
console.log('Listening on port %d', server.address().port);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment