aboutsummaryrefslogtreecommitdiff
path: root/lib/dasein.js
blob: 38654a4822261fe447ece81d6fbba384ce16ef8a (plain)
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
'use strict';

const Koa = require('koa');
const KoaJwt = require('koa-jwt');
const KoaStatic = require('koa-static');
const KoaRoute = require('koa-route');

const AuthHandler = require('./handlers/auth');

const internals = {};

internals.k401Location = '/401.html';
internals.kMainLocation = '/';

module.exports = internals.Dasein = class Dasein {

  constructor(config) {

    Object.assign(this, config);
  }

  run() {

    this._initializeServer();
    this._startServer();
    this._printBanner();

    return Promise.resolve();
  }

  _initializeServer() {

    this._app = Koa();

    this._app.keys = this.cookieKeys;

    this._app.use(KoaStatic(this.staticDirectory));

    // Redirect all 401s to the 401 static page

    this._app.use(function * (next) {

      try {
        yield next;
      }
      catch (err) {
        if (err.status === 401) {
          return this.redirect(internals.k401Location);
        }

        throw err;
      }
    });

    this._app.use(KoaJwt({
      secret: this.jwt.secret,
      passthrough: true,
      cookie: this.jwt.cookieName
    }));

    // Handlers for Twitter Auth Related Routes

    const authHandler = new AuthHandler({
      hostname: this.hostname,
      jwt: this.jwt,
      twitter: this.twitter
    });
    this._app.use(KoaRoute.get('/login', authHandler.login()));
    this._app.use(KoaRoute.get('/login-callback', authHandler.callback()));
    this._app.use(KoaRoute.get('/logout', authHandler.logout()));

    // The index

    this._app.use(function * () {

      if (this.state.user) {
        this.body = `<img src="${this.state.user.profile_image_url_https}"> Hello ${this.state.user.screen_name}`;
        return;
      }

      this.body = 'Go to /login to login';
      return;
    });
  }

  _startServer() {

    this._app.listen(this.port);
  }

  // Prints the banner.
  _printBanner() {

    console.log('        .');
    console.log('       /');
    console.log('    +-----+');
    console.log(`    | o o |  - Listening Gladly, Try me on port: ${this.port}`);
    console.log('    +-----+');
    console.log('  +---------+');
    console.log(' /|    [][] |\\');
    console.log(' ||         | |');
    console.log(' ||         |  \\c');
    console.log(' ^+---------+');
    console.log('      (.) ');
  }
};