-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.js
More file actions
executable file
·104 lines (98 loc) · 3.84 KB
/
sample.js
File metadata and controls
executable file
·104 lines (98 loc) · 3.84 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
var openid = require('./openid-request');
var url = require('url');
var querystring = require('querystring');
var extensions = [new openid.UserInterface(),
new openid.SimpleRegistration(
{
"nickname" : true,
"email" : true,
"fullname" : true,
"dob" : true,
"gender" : true,
"postcode" : true,
"country" : true,
"language" : true,
"timezone" : true
}),
new openid.AttributeExchange(
{
"http://axschema.org/contact/email": "required",
"http://axschema.org/namePerson/friendly": "required",
"http://axschema.org/namePerson": "required"
}),
new openid.PAPE(
{
"max_auth_age": 24 * 60 * 60, // one day
"preferred_auth_policies" : "none" //no auth method preferred.
})];
var relyingParty = new openid.RelyingParty(
'http://example.com/verify', // Verification URL (yours)
null, // Realm (optional, specifies realm for OpenID authentication)
false, // Use stateless verification
false, // Strict mode
extensions); // List of extensions to enable and include
var server = require('http').createServer(
function(req, res)
{
var parsedUrl = url.parse(req.url);
if(parsedUrl.pathname == '/authenticate')
{
// User supplied identifier
var query = querystring.parse(parsedUrl.query);
var identifier = query.openid_identifier;
// Resolve identifier, associate, and build authentication URL
relyingParty.authenticate(identifier, false, function(error, authUrl)
{
if(error)
{
res.writeHead(200, { 'Content-Type' : 'text/plain; charset=utf-8' });
res.end('Authentication failed: ' + error.message);
}
else if (!authUrl)
{
res.writeHead(200, { 'Content-Type' : 'text/plain; charset=utf-8' });
res.end('Authentication failed');
}
else
{
res.writeHead(302, { Location: authUrl });
res.end();
}
});
}
else if(parsedUrl.pathname == '/verify')
{
// Verify identity assertion
// NOTE: Passing just the URL is also possible
relyingParty.verifyAssertion(req, function(error, result)
{
res.writeHead(200, { 'Content-Type' : 'text/plain; charset=utf-8' });
if(error)
{
res.end('Authentication failed: ' + error.message);
}
else
{
// Result contains properties:
// - authenticated (true/false)
// - answers from any extensions (e.g.
// "http://axschema.org/contact/email" if requested
// and present at provider)
res.end((result.authenticated ? 'Success :)' : 'Failure :(') +
'\n\n' + JSON.stringify(result));
}
});
}
else
{
// Deliver an OpenID form on all other URLs
res.writeHead(200, { 'Content-Type' : 'text/html; charset=utf-8' });
res.end('<!DOCTYPE html><html><body>'
+ '<form method="get" action="/authenticate">'
+ '<p>Login using OpenID</p>'
+ '<input name="openid_identifier" />'
+ '<input type="submit" value="Login" />'
+ '</form></body></html>');
}
});
server.listen(8888);