-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
214 lines (177 loc) · 6.46 KB
/
server.js
File metadata and controls
214 lines (177 loc) · 6.46 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* Module dependencies.
*/
var express = require('express'),
pltz = require('./pltz'),
openid = require('openid'),
url = require('url'),
querystring = require('querystring');
var app = module.exports = express.createServer();
var extensions = [new openid.UserInterface(),
new openid.SimpleRegistration(
{
"nickname" : false,
"email" : true,
"fullname" : false,
"dob" : false,
"gender" : false,
"postcode" : false,
"country" : false,
"language" : false,
"timezone" : false
}),
new openid.AttributeExchange(
{
"http://axschema.org/contact/email": "required",
"http://axschema.org/namePerson/friendly": "required",
"http://axschema.org/namePerson": "required"
})];
var relyingParty = new openid.RelyingParty(
'http://templay.no.de/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
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.favicon() );
// app.use(express.session({ secret: 'your secret here' }));
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Data
//
// var pltz = [
// { name: 'Home in 5', template: 'homein5@onmachine.org?subject=Home%20in%205&body=Lvoe' },
// { name: 'Running Late', template: 'runninglate@onmachine.org?subject=Running%20late&body=Lvoe' }
// ];
//GLOBAL.DEBUG = true;
sys = require("sys");
test = require("assert");
var Db = require('mongodb').Db,
Connection = require('mongodb').Connection,
Server = require('mongodb').Server,
BSON = require('mongodb').BSONPure;
//BSON = require('mongodb').BSONNative;
var host = process.env.MONGO_NODE_DRIVER_HOST != null ? process.env.MONGO_NODE_DRIVER_HOST : 'localhost';
var port = process.env.MONGO_NODE_DRIVER_PORT != null ? process.env.MONGO_NODE_DRIVER_PORT : Connection.DEFAULT_PORT;
sys.puts("Connecting to " + host + ":" + port);
var db = new Db('node-mongo-examples', new Server(host, port, {}));
// Params
// app.param('id', function(req, res, next, id){
//
//
//
//
// if (req.plt = pltz[id]) {
// next();
// } else {
// next(new Error('failed to find ' + id));
// }
// });
// Routes
app.get('/', function(req, res){
var pltz = {};
db.open(function(err, db) {
db.collection('test', function(err, collection) {
// Erase all records from the collection, if any
collection.remove(function(err, collection) {
// Insert records
collection.insert({ name: 'Running Late', template: 'runninglate@onmachine.org?subject=Running%20late&body=Lvoe' });
collection.insert({ name: 'Home in 5', template: 'homein5@onmachine.org?subject=Home%20in%205&body=Lvoe' });
collection.count(function(err, count) {
sys.puts("There are " + count + " records in the test collection");
collection.find(function(err, cursor) {
cursor.toArray(function(err, pltz) {
res.render('index', { title: 'empltz', pltz: pltz });
db.close();
// collection.drop(function(err, collection) {
// db.close();
// });
});
});
});
});
});
});
});
// app.get('/:id/:op?', pltz.load);
// app.get('/:id', pltz.view);
// app.get('/:id/view', pltz.view);
app.get('/:id/edit', function(req, res, id){
db.open(function(err, db) {
db.collection('test', function(err, collection) {
collection.find(function(err, cursor) {
cursor.toArray(function(err, pltz) {
var plt = pltz[0];
console.log('plt: ', plt);
res.render('edit', {
title: 'Editing user ',
plt: plt
});
collection.drop(function(err, collection) {
db.close();
});
});
});
});
});
});
app.put('/:id/edit', function(req, res){
var plt = req.body.plt;
console.log(req.plt);
req.plt.name = plt.name;
req.plt.template = plt.template;
res.redirect('back');
});
app.get('/login', function(req, res) {
res.render('login', { title: 'login'});
});
app.get('/authenticate', function(req, res) {
var parsedUrl = url.parse(req.url);
var query = querystring.parse(parsedUrl.query);
var identifier = query.openid_identifier;
relyingParty.authenticate(identifier, false, function(authUrl) {
if (!authUrl)
{
res.writeHead(500);
res.end(error);
}
else
{
res.redirect(authUrl);
// res.writeHead(302, { Location: authUrl });
// res.end();
}
});
});
app.get('/verify', function(req, res) {
relyingParty.verifyAssertion(req, function(result)
{
// Result contains properties:
// - authenticated (true/false)
// - error (message, only if not authenticated)
// - answers from any extensions (e.g.
// "http://axschema.org/contact/email" if requested
// and present at provider)
res.writeHead(200);
res.end((result.authenticated ? 'Success :)' : 'Failure :(') +
'\n\n' + JSON.stringify(result));
});
});
// Only listen on $ node app.js
if (!module.parent) {
app.listen(3000);
console.log("Express server listening on port %d", app.address().port);
}