Johannes Zellner 11 лет назад
Родитель
Сommit
1e256b4b67
5 измененных файлов с 91 добавлено и 30 удалено
  1. 54 16
      app.js
  2. 5 0
      frontend/index.html
  3. 25 14
      frontend/js/core.js
  4. 3 0
      frontend/js/settings.js
  5. 4 0
      package.json

+ 54 - 16
app.js

@@ -5,38 +5,76 @@
 require('supererror')({ splatchError: true });
 
 var express = require('express'),
+    passport = require('passport'),
     json = require('body-parser').json,
+    cookieParser = require('cookie-parser'),
+    session = require('express-session'),
+    CloudronStrategy = require('passport-cloudron'),
     cors = require('cors'),
     routes = require('./src/routes.js'),
     things = require('./src/things.js'),
     lastmile = require('connect-lastmile'),
     serveStatic = require('serve-static');
 
+passport.serializeUser(function (user, done) {
+    console.log('Now serializeUser', user);
+    done(null, user.id);
+});
+
+passport.deserializeUser(function (id, done) {
+    console.log('Now deserializeUser', id);
+    done(null, { id: id });
+});
+
+passport.use(new CloudronStrategy({
+        callbackURL: 'http://localhost:3000/auth/cloudron/callback'
+    }, function verify(token, tokenSecret, profile, done) {
+        done(null, profile);
+    }
+));
+
+function auth(req, res, next) {
+    if (process.env.NO_AUTH) return next();
+    if (req.isAuthenticated()) return next();
+
+    res.status(401).end();
+}
+
 var app = express();
 var router = new express.Router();
 
 router.del = router.delete;
 
-router.post('/api/things', routes.add);
-router.get ('/api/things', routes.getAll);
-router.get ('/api/things/:id', routes.get);
-router.put ('/api/things/:id', routes.put);
-router.del ('/api/things/:id', routes.del);
+router.post('/api/things', auth, routes.add);
+router.get ('/api/things', auth, routes.getAll);
+router.get ('/api/things/:id', auth, routes.get);
+router.put ('/api/things/:id', auth, routes.put);
+router.del ('/api/things/:id', auth, routes.del);
+
+router.get ('/api/tags', auth, routes.getTags);
 
-router.get ('/api/tags', routes.getTags);
+router.post('/api/settings', auth, routes.settingsSave);
+router.get ('/api/settings', auth, routes.settingsGet);
 
-router.post('/api/settings', routes.settingsSave);
-router.get ('/api/settings', routes.settingsGet);
+router.get ('/api/export', auth, routes.exportThings);
 
-router.get ('/api/export', routes.exportThings);
+router.get ('/auth/cloudron/callback', passport.authenticate('cloudron', { successRedirect: '/', failureRedirect: '/auth/login' }));
+
+router.get ('/auth/login', passport.authenticate('cloudron'));
+router.get ('/auth/logout', function (req, res) {
+    req.logout();
+    res.redirect('/');
+});
 
-app
-    .use(cors())
-    .use(json({ strict: true })) // only parse objects and arrays
-    .use(serveStatic(__dirname + '/public'))
-    .use(serveStatic(__dirname + '/bower_components'))
-    .use(router)
-    .use(lastmile());
+app.use(serveStatic(__dirname + '/public'));
+app.use(cors());
+app.use(json({ strict: true }));
+app.use(cookieParser());
+app.use(session({ secret: 'holy guacamoly' }));
+app.use(passport.initialize());
+app.use(passport.session());
+app.use(router);
+app.use(lastmile());
 
 function exit(error) {
     if (error) console.error(error);

+ 5 - 0
frontend/index.html

@@ -120,6 +120,11 @@
             <button class="btn btn-primary pull-right" v-on:click="exportThings">Export</button>
           </div>
         </div>
+        <div class="row">
+          <div class="col-md-offset-1 col-md-10">
+            <button class="btn btn-danger pull-right" v-on:click="logout">Logout</button>
+          </div>
+        </div>
       </div>
     </div>
 

+ 25 - 14
frontend/js/core.js

@@ -8,6 +8,17 @@ function guid() {
     return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
 }
 
+function errorWrapper(callback) {
+    return function (error, result) {
+        if (error && error.status === 401) {
+            window.location.href = '/auth/login';
+            return;
+        }
+
+        callback(error, result);
+    };
+}
+
 function Thing(id, createdAt, tags, content, richContent) {
     this.id = id;
     this.createdAt = createdAt || 0;
@@ -34,7 +45,7 @@ Things.prototype.get = function (filter, callback) {
 
     if (filter) url += '?filter=' + encodeURIComponent(filter);
 
-    superagent.get(url).end(function (error, result) {
+    superagent.get(url).end(errorWrapper(function (error, result) {
         // ignore this if we moved on
         if (that._operation !== operation) {
             console.log('ignore this call');
@@ -49,14 +60,14 @@ Things.prototype.get = function (filter, callback) {
         });
 
         callback(null, that.data);
-    });
+    }));
 };
 
 Things.prototype.add = function (content, callback) {
     var that = this;
     var url = this._server + '/api/things';
 
-    superagent.post(url).send({ content: content }).end(function (error, result) {
+    superagent.post(url).send({ content: content }).end(errorWrapper(function (error, result) {
         if (error) return callback(error);
         if (result.status !== 201) return callback(new Error('Failed: ' + result.status + '. ' + result.text));
 
@@ -65,14 +76,14 @@ Things.prototype.add = function (content, callback) {
         });
 
         callback(null);
-    });
+    }));
 };
 
 Things.prototype.edit = function (thing, callback) {
     var that = this;
     var url = this._server + '/api/things/' + thing.id;
 
-    superagent.put(url).send({ content: thing.content }).end(function (error, result) {
+    superagent.put(url).send({ content: thing.content }).end(errorWrapper(function (error, result) {
         if (error) return callback(error);
         if (result.status !== 201) return callback(new Error('Failed: ' + result.status + '. ' + result.text));
 
@@ -81,14 +92,14 @@ Things.prototype.edit = function (thing, callback) {
         });
 
         callback(null);
-    });
+    }));
 };
 
 Things.prototype.del = function (thing, callback) {
     var that = this;
     var url = this._server + '/api/things/' + thing.id;
 
-    superagent.del(url).end(function (error, result) {
+    superagent.del(url).end(errorWrapper(function (error, result) {
         if (error) return callback(error);
         if (result.status !== 200) return callback(new Error('Failed: ' + result.status + '. ' + result.text));
 
@@ -97,7 +108,7 @@ Things.prototype.del = function (thing, callback) {
         });
 
         callback(null);
-    });
+    }));
 };
 
 Things.prototype.onAdded = function (callback) {
@@ -128,26 +139,26 @@ function Settings(server) {
 Settings.prototype.save = function (callback) {
     var url = this._server + '/api/settings';
 
-    superagent.post(url).send({ settings: this.data }).end(function (error, result) {
+    superagent.post(url).send({ settings: this.data }).end(errorWrapper(function (error, result) {
         if (error) return callback(error);
         if (result.status !== 202) return callback(new Error('Failed: ' + result.status + '. ' + result.text));
 
         callback(null);
-    });
+    }));
 };
 
 Settings.prototype.get = function (callback) {
     var that = this;
     var url = this._server + '/api/settings';
 
-    superagent.get(url).end(function (error, result) {
+    superagent.get(url).end(errorWrapper(function (error, result) {
         if (error) return callback(error);
         if (result.status !== 200) return callback(new Error('Failed: ' + result.status + '. ' + result.text));
 
         that.set(result.body.settings);
 
         callback(null, result.body.settings);
-    });
+    }));
 };
 
 Settings.prototype.set = function (data) {
@@ -172,12 +183,12 @@ function Tags(server) {
 Tags.prototype.get = function (callback) {
     var url = this._server + '/api/tags';
 
-    superagent.get(url).end(function (error, result) {
+    superagent.get(url).end(errorWrapper(function (error, result) {
         if (error) return callback(error);
         if (result.status !== 200) return callback(new Error('Failed: ' + result.status + '. ' + result.text));
 
         callback(null, result.body.tags);
-    });
+    }));
 };
 
 function Core() {

+ 3 - 0
frontend/js/settings.js

@@ -41,6 +41,9 @@ var vueSettings = new Vue({
         exportThings: function (event) {
             Core.things.export();
             vueSettings.hide();
+        },
+        logout: function () {
+            window.location.href = '/auth/logout';
         }
     }
 });

+ 4 - 0
package.json

@@ -16,9 +16,13 @@
     "async": "^1.5.0",
     "body-parser": "^1.14.1",
     "connect-lastmile": "0.0.13",
+    "cookie-parser": "^1.4.0",
     "cors": "^2.7.1",
     "express": "^4.13.3",
+    "express-session": "^1.12.1",
     "mongodb": "^2.0.49",
+    "passport": "^0.3.2",
+    "passport-cloudron": "^0.3.1",
     "serve-static": "^1.10.0",
     "superagent": "^1.5.0",
     "supererror": "^0.7.1"