root.js 5.71 KB
Newer Older
mntmn's avatar
mntmn committed
1
2
3
4
5
6
7
8
9
10
11
"use strict";

const config = require('config');

const redis = require('../helpers/redis');
const express = require('express');
const crypto = require('crypto');
const router = express.Router();
const mailer = require('../helpers/mailer');
const _ = require('underscore');

Wolfgang Knopki's avatar
Wolfgang Knopki committed
12
13
14
15
16
const fs = require('fs')
const SamlStrategy = require('passport-saml').Strategy
const passport = require('passport')
const Saml2js = require('saml2js');

mntmn's avatar
mntmn committed
17
18
19
20
21
const db = require('../models/db');
const Sequelize = require('sequelize');
const Op = Sequelize.Op;
const uuidv4 = require('uuid/v4');

Wolfgang Knopki's avatar
Wolfgang Knopki committed
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

// =========== PASSPORT =======
  passport.serializeUser(function (user, done) {
    done(null, user);
  });

  passport.deserializeUser(function (user, done) {
    done(null, user);
  });

  var samlStrategy = new SamlStrategy({
      // URL that goes from the Identity Provider -> Service Provider
      callbackUrl: config.path,

      entryPoint: config.entryPoint,
      issuer: config.issuer,
      identifierFormat: null,

      validateInResponseTo: false,
      disableRequestedAuthnContext: true
  },
  function (profile, done) {
    return done(null, {
      id: profile.nameID,
      idFormat: profile.nameIDFormat,
      email: profile.email,
      firstName: profile.givenName,
      lastName: profile.sn
    });
  });

  passport.use(samlStrategy);

 // to generate Service Provider's XML metadata
  router.get('/saml/metadata',
    function(req, res) {
      res.type('application/xml');
      var spMetadata = samlStrategy.generateServiceProviderMetadata(fs.readFileSync('/cert/certificate.pem', 'utf8'));
      res.status(200).send(spMetadata);
    }
  );

router.post('/saml/SSO', passport.authenticate('saml', { failureRedirect: '/login', failureFlash: true}), function(req, res){
    const xmlResponse = req.body.SAMLResponse;
    const parser = new Saml2js(xmlResponse);
    const userid = parser.get('email');
68
69
70
71
72

    //check, if user exists, if not create.

    //else get userid and create session -> set cookie

Wolfgang Knopki's avatar
Wolfgang Knopki committed
73
74
75
76
77
78
79
80
    crypto.randomBytes(48, function(ex, buf) {
              var token = buf.toString('hex');

              var session = {
                user_id: userid,
                token: token,
                ip: req.ip,
                device: "web",
81
82
                created_at: new Date(),
                url : "/"
Wolfgang Knopki's avatar
Wolfgang Knopki committed
83
84
85
86
87
              };

              db.Session.create(session)
                .error(err => {
                  console.error("Error creating Session:",err);
88
                  res.redirect(500, "/");
Wolfgang Knopki's avatar
Wolfgang Knopki committed
89
90
91
92
                })
                .then(() => {
                  var domain = (process.env.NODE_ENV == "production") ? new URL(config.get('endpoint')).hostname : req.headers.hostname;
                  res.cookie('sdsession', token, { domain: domain, httpOnly: true });
93
                  res.redirect(302, "/")
Wolfgang Knopki's avatar
Wolfgang Knopki committed
94
95
96
97
                });
    });
});

mntmn's avatar
mntmn committed
98
router.get('/', (req, res) => {
99
  res.render('index', { config:config, user:req.user });
mntmn's avatar
mntmn committed
100
101
102
103
104
105
106
});

router.get('/ping', (req, res) => {
  res.status(200).json({"status": "ok"})
});

router.get('/spaces', (req, res) => {
107
  res.render('spacedeck', { config:config, user:req.user });
mntmn's avatar
mntmn committed
108
109
110
});

router.get('/not_found', (req, res) => {
111
  res.render('not_found', {});
mntmn's avatar
mntmn committed
112
113
114
});

router.get('/confirm/:token', (req, res) => {
115
  res.render('spacedeck', { config:config, user:req.user });
mntmn's avatar
mntmn committed
116
117
118
});

router.get('/folders/:id', (req, res) => {
119
  res.render('spacedeck', { config:config, user:req.user });
mntmn's avatar
mntmn committed
120
121
122
});

router.get('/signup', (req, res) => {
123
  res.render('spacedeck', { config:config, user:req.user });
mntmn's avatar
mntmn committed
124
125
126
});

router.get('/accept/:id', (req, res) => {
127
  res.render('spacedeck', { config:config, user:req.user });
mntmn's avatar
mntmn committed
128
129
130
});

router.get('/password-reset', (req, res) => {
131
  res.render('spacedeck', { config:config, user:req.user });
mntmn's avatar
mntmn committed
132
133
134
});

router.get('/password-confirm/:token', (req, res) => {
135
  res.render('spacedeck', { config:config, user:req.user });
mntmn's avatar
mntmn committed
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
});

router.get('/de/*', (req, res) => {
  res.redirect("/t/de");
});

router.get('/de', (req, res) => {
  res.redirect("/t/de");
});

router.get('/fr/*', (req, res) => {
  res.redirect("/t/fr");
});

router.get('/fr', (req, res) => {
  res.redirect("/t/fr");
});
153

Mejans's avatar
Mejans committed
154
155
156
router.get('/oc/*', (req, res) => {
  res.redirect("/t/oc");
});
mntmn's avatar
mntmn committed
157

Mejans's avatar
Mejans committed
158
159
160
router.get('/oc', (req, res) => {
  res.redirect("/t/oc");
});
161

mntmn's avatar
mntmn committed
162
163
164
165
166
167
168
169
170
171
172
173
router.get('/en/*', (req, res) => {
  res.redirect("/t/en");
});

router.get('/en', (req, res) => {
  res.redirect("/t/end");
});

router.get('/account', (req, res) => {
  res.render('spacedeck');
});

Wolfgang Knopki's avatar
Wolfgang Knopki committed
174
175
176
177
178
179
180
181
182
183
router.get('/login', passport.authenticate('saml',
                           {
                             successRedirect: '/',
                             failureRedirect: '/login'
                           })
);


//  res.render('spacedeck', { config:config, user:req.user });
//});
mntmn's avatar
mntmn committed
184
185

router.get('/logout', (req, res) => {
186
  res.render('spacedeck', { config:config, user:req.user });
mntmn's avatar
mntmn committed
187
188
189
190
191
192
193
194
195
196
197
});

router.get('/t/:id', (req, res) => {
  res.cookie('spacedeck_locale', req.params.id, { maxAge: 900000, httpOnly: true });
  var path = "/";
  if (req.query.r=="login" || req.query.r=="signup") {
    path = "/"+req.query.r;
  }
  res.redirect(path);
});

198
199
200
201
router.get('/s/:hash', (req, res) => {
  var hash = req.params.hash;
  if (hash.split("-").length > 0) {
    hash = hash.split("-")[0];
mntmn's avatar
mntmn committed
202
  }
mntmn's avatar
mntmn committed
203

204
  db.Space.findOne({where: {"edit_hash": hash}}).then(function (space) {
mntmn's avatar
mntmn committed
205
206
    if (space) {
      if (req.accepts('text/html')){
207
	      res.redirect("/spaces/"+space._id + "?spaceAuth=" + hash);
mntmn's avatar
mntmn committed
208
      } else {
mntmn's avatar
mntmn committed
209
	      res.status(200).json(space);
mntmn's avatar
mntmn committed
210
      }
mntmn's avatar
mntmn committed
211
    } else {
mntmn's avatar
mntmn committed
212
      if (req.accepts('text/html')) {
213
	      res.status(404).render('not_found', {});
mntmn's avatar
mntmn committed
214
      } else {
mntmn's avatar
mntmn committed
215
	      res.status(404).json({});
mntmn's avatar
mntmn committed
216
      }
mntmn's avatar
mntmn committed
217
218
219
220
221
    }
  });
});

router.get('/spaces/:id', (req, res) => {
222
  res.render('spacedeck', { config:config, user:req.user });
mntmn's avatar
mntmn committed
223
224
});

Wolfgang Knopki's avatar
Wolfgang Knopki committed
225
module.exports = {router: router, passport:passport};