Skip to content

Instantly share code, notes, and snippets.

@siygle
Created October 1, 2012 10:19
Show Gist options
  • Save siygle/3810731 to your computer and use it in GitHub Desktop.
Save siygle/3810731 to your computer and use it in GitHub Desktop.
nodejs sample using Persona
<!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>
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);
@jackson021102
Copy link

hi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment