comparison index.js_to_pump.io-client-app_lib_routes @ 0:b73191efc65b

Initial import of pumpbridge (bloody bloody alpha)
author Mathias Gebbe <mgebbe@intevation.de>
date Thu, 05 Jun 2014 10:35:15 +0200
parents
children f190abf340ef
comparison
equal deleted inserted replaced
-1:000000000000 0:b73191efc65b
1 // index.js
2 //
3 // Most of the routes in the application
4 //
5 // Copyright 2013, E14N https://e14n.com/
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18
19 var wf = require("webfinger"),
20 async = require("async"),
21 _ = require("underscore"),
22 uuid = require("node-uuid"),
23 User = require("../models/user"),
24 Host = require("../models/host"),
25 RequestToken = require("../models/requesttoken"),
26 RememberMe = require("../models/rememberme"),
27 site = require("../models/site");
28
29 exports.hostmeta = function(req, res) {
30 res.json({
31 links: [
32 {
33 rel: "dialback",
34 href: site.url("/dialback")
35 }
36 ]
37 });
38 };
39
40 exports.index = function(req, res, next) {
41 var hosts, users, bank = Host.bank();
42
43 if (req.user) {
44 res.render('index', { pageTitle: "pumpbridge" , user: req.user});
45 } else {
46 res.render('login', { pageTitle: "pumpbridge" });
47 }
48 };
49
50 exports.about = function(req, res) {
51 res.render('about', { pageTitle: "About" });
52 };
53
54 exports.login = function(req, res) {
55 res.render('login', { pageTitle: "Login" });
56 };
57
58 exports.handleLogin = function(req, res, next) {
59
60 var id = req.body.webfinger,
61 rememberme = req.body.rememberme,
62 hostname = User.getHostname(id),
63 host;
64
65 req.log.debug(req.body, "Handling login");
66
67 async.waterfall([
68 function(callback) {
69 Host.ensureHost(hostname, callback);
70 },
71 function(results, callback) {
72 host = results;
73 host.getRequestToken(callback);
74 }
75 ], function(err, rt) {
76 if (err) {
77 if (err instanceof Error) {
78 next(err);
79 } else if (err.data) {
80 next(new Error(err.data));
81 }
82 } else {
83 // Remember if the user asked for a rememberme cookie
84 req.session.remembermeChecked = (rememberme == "on");
85 res.redirect(host.authorizeURL(rt));
86 }
87 });
88 };
89
90 exports.authorized = function(req, res, next) {
91
92 var hostname = req.params.hostname,
93 token = req.query.oauth_token,
94 verifier = req.query.oauth_verifier,
95 rt,
96 host,
97 access_token,
98 token_secret,
99 id,
100 object,
101 user,
102 newUser = false;
103
104 async.waterfall([
105 function(callback) {
106 async.parallel([
107 function(callback) {
108 RequestToken.get(RequestToken.key(hostname, token), callback);
109 },
110 function(callback) {
111 Host.get(hostname, callback);
112 }
113 ], callback);
114 },
115 function(results, callback) {
116 rt = results[0];
117 host = results[1];
118 host.getAccessToken(rt, verifier, callback);
119 },
120 function(token, secret, extra, callback) {
121 access_token = token;
122 token_secret = secret;
123 async.parallel([
124 function(callback) {
125 rt.del(callback);
126 },
127 function(callback) {
128 host.whoami(access_token, token_secret, callback);
129 }
130 ], callback);
131 },
132 function(results, callback) {
133 object = results[1];
134 id = object.id;
135 if (id.substr(0, 5) == "acct:") {
136 id = id.substr(5);
137 }
138 User.get(id, function(err, user) {
139 if (err && err.name === "NoSuchThingError") {
140 newUser = true;
141 User.fromPerson(object, access_token, token_secret, callback);
142 } else if (err) {
143 callback(err, null);
144 } else {
145 callback(null, user);
146 }
147 });
148 },
149 function(results, callback) {
150 user = results;
151 if (req.session.remembermeChecked) {
152 req.log.debug("Setting rememberme cookie");
153 RememberMe.create({user: user.id}, function(err, rm) {
154 if (err) {
155 callback(err);
156 } else {
157 req.log.debug({rm: rm}, "Created rememberme record");
158 res.cookie("rememberme", rm.uuid, {path: "/", expires: new Date(Date.now() + 180 * 24 * 60 * 60 * 1000), httpOnly: true});
159 req.log.debug({rememberme: rm.uuid}, "Set rememberme cookie");
160 callback(null);
161 }
162 });
163 } else {
164 callback(null);
165 }
166 }
167 ], function(err) {
168 if (err) {
169 next(err);
170 } else {
171 req.session.userID = user.id;
172 delete req.session.remembermeChecked;
173 res.redirect("/");
174 }
175 });
176 };
177
178 exports.handleLogout = function(req, res) {
179
180 delete req.session.userID;
181 delete req.session.remembermeChecked;
182 delete req.user;
183
184 res.clearCookie("rememberme");
185
186 res.redirect("/", 303);
187 };
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)