public
Last active

nodejs sample using Persona

  • Download Gist
index.html
HTML
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
<!doctype html>
<html>
<head>
<style>
.persona {
border: 1px solid #666;
width: 100px;
text-align: center;
margin: 5px;
}
.persona:hover {
background-color: #f4f4f4;
cursor: pointer;
}
</style>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="https://login.persona.org/include.js"></script>
</head>
<body>
<div id="signin" class="persona">SignIn</div>
<div id="signout" class="persona">SignOut</div>
<script>
var signin = document.getElementById('signin');
if (signin) {
signin.onclick = function() { navigator.id.request(); };
}
var signout = document.getElementById('signout');
if (signout) {
signout.onclick = function() { navigator.id.logout(); };
}
// decide what to do
navigator.id.watch({
onlogin: function(assertion) {
console.log('login');
$.ajax({
type: 'POST',
url: '/auth/login',
data: {assertion: assertion},
success: function(res, status, xhr) {
window.location.reload();
},
error: function(res, status, xhr) {
console.log(res);
}
});
},
onlogout: function() {
console.log('logout');
}
});
</script>
</body>
</html>
server.js
JavaScript
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 38 39 40 41 42 43 44 45 46 47
var express = require('express');
var http = require('https');
var fs = require('fs');
var app = express();
var config = JSON.parse(fs.readFileSync('misc/config.json'));
 
app.use(express.static('public'));
app.use(express.bodyParser());
app.post('/auth/login', function(req, res) {
if (!req.body.assertion) {
console.log('miss assertion');
res.send(400);
}
 
var data = JSON.stringify({
assertion: req.body.assertion,
audience: config.host + ':' + config.port,
verify: true
});
 
var options = {
host: 'verifier.login.persona.org',
port: 443,
path: '/verify',
method: 'POST',
headers: {
'Content-Length': data.length,
'Content-Type': 'application/json'
}
};
 
var verify_req = http.request(options, function(res) {
console.log(res.statusCode);
res.setEncoding('utf8');
res.on('data', function(chunk) {
console.log(chunk);
});
});
verify_req.write(data);
verify_req.end();
 
verify_req.on('error', function(e) {
console.log(e);
});
});
 
app.listen(config.port);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.