-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
155 lines (154 loc) · 7 KB
/
index.js
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
class IOCore {constructor (ioFunc) {this.then = cb => ioFunc((...args) => { cb(...args) });};reject (pred) {let saveThen = this.then;this.then = cb => {saveThen((...args) => {let result = pred(...args);if (result !== null) {if (Array.isArray(result)) {cb(...result);} else {cb(result);}};});};return this;};maybeFalse (mv, handler) {let saveThen = this.then;this.then = cb => {saveThen((...args) => {let result = mv(...args);if (result === false) {handler(...args);} else {cb(...args);}});};return this;};maybeNull (mv, handler) {let saveThen = this.then;this.then = cb => {saveThen((...args) => {let result = mv(...args);if (result === null) {handler(...args);} else {cb(...args);}});};return this;};maybeErr (mv, handler) {let saveThen = this.then;this.then = cb => {saveThen((...args) => {let result = mv(...args);if (result instanceof Error) {handler(...args);} else {cb(...args);}});};return this;};maybeTrue (mv, handler) {let saveThen = this.then;this.then = cb => {saveThen((...args) => {let result = mv(...args);if (result === true) {handler(...args);} else {cb(...args);}});};return this;};maybeUndefined (mv, handler) {let saveThen = this.then;this.then = cb => {saveThen((...args) => {let result = mv(...args);if (result === undefined) {handler(...args);} else {cb(...args);}});};return this;};map (transform) {let saveThen = this.then;this.then = cb => {saveThen((...args) => {let result = transform(...args);if (Array.isArray(result)) {cb(...result);} else {cb(result);}});};return this;};bind (ioFunc) {let saveThen = this.then;this.then = cb => {saveThen((...args) => {let io = ioFunc(...args);io.then((...ioargs) => cb(...args, ...ioargs));});};return this;};static timer (s) {var intervalId;var timer = new IOCore(cb => {intervalId = setInterval(cb, Math.floor(s * 1000))});timer.clear = () => clearInterval(intervalId);return timer;};static createIO (ioFunc) {return new IOCore(ioFunc);};};const readline = require('readline');const fs = require('fs');const rlConfig = {input: process.stdin,output: process.stdout}; class IO extends IOCore {static getLine (str) {const rl = readline.createInterface(rlConfig);return new IOCore(cb => rl.question(str, cb)).map(data => {rl.close();return data;});};static putLine (...data) {return new IOCore(cb => process.nextTick(cb, data)).map(data => {console.log(...data);return data});};static readFile (filename) {return new IOCore(cb => fs.readFile(filename, cb)).map((_, data) => data.toString());};static writeFile (filename, data) {return new IOCore(cb => fs.writeFile(filename, data, cb));};};
const express = require('express');
const path = require('path');
const sessions = require('client-sessions');
const bodyParser = require('body-parser');
const Mustache = require('mustache');
const Request = require('request');
const Querystring = require('querystring');
const Guid = require('guid');
const csrfGuid = Guid.raw();
const akConfig = require('./account-kit-config.json');
const AKINIT = {
appId: akConfig.appID,
csrf: csrfGuid,
version: akConfig.version
};
const bodyParserJSON = bodyParser.json();
const app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParserJSON);
app.use(bodyParser.urlencoded({ extended: true }));
const tokenExchangeBaseURL = 'https://graph.accountkit.com/' + akConfig.version + '/access_token?';
const meEndpointBaseURL = 'https://graph.accountkit.com/' + akConfig.version + '/me' + '?access_token=';
const params = {
grant_type: 'authorization_code',
access_token: [
'AA',
akConfig.appID,
akConfig.appSecret
].join('|')
};
app.use(sessions({
cookieName: 'session',
secret: 'mysecret',
duration: 7 * 24 * 60 * 60 * 1000,
activeDuration: 24 * 60 * 60 * 1000
}));
IO.createIO(cb => app.get('/', cb)).map((req, res) => [
req,
res
]).maybeUndefined((req, res) => req.session.user, (req, res) => res.redirect('/login')).map((req, res) => {
(res.send('hello world'))
return [
req,
res
];
}).then((req, res) => []);
IO.createIO(cb => app.get('/login', cb)).map((request, response) => [
request,
response
]).maybeTrue((request, response) => !!request.session.user, (request, response) => response.redirect('/')).bind((request, response) => IO.readFile('views/login.html')).map((request, response, loginTemplate) => [
request,
response,
loginTemplate
]).map((request, response, loginTemplate) => {
(response.send(Mustache.to_html(loginTemplate, AKINIT)))
return [
request,
response,
loginTemplate
];
}).then((request, response, loginTemplate) => []);
IO.createIO(cb => app.post('/login', cb)).map((request, response) => [
request,
response
]).map((request, response) => [
request.body.csrf === csrfGuid,
request,
response
]).maybeFalse((csrfCheck, request, response) => csrfCheck, (csrfCheck, request, response) => response.send({ success: false })).map((csrfCheck, request, response) => {
Object.defineProperty(params, 'code', {
value: request.body.code,
enumerable: true,
writable: false,
configurable: true
});
return [
csrfCheck,
request,
response
];
}).bind((csrfCheck, request, response) => IO.createIO(cb => Request.get({
url: tokenExchangeBaseURL + Querystring.stringify(params),
json: true
}, cb))).map((csrfCheck, request, response, err, resp, respBody) => [
csrfCheck,
request,
response,
err,
resp,
respBody
]).bind((csrfCheck, request, response, err, resp, respBody) => IO.createIO(cb => Request.get({
url: meEndpointBaseURL + respBody.access_token,
json: true
}, cb))).map((csrfCheck, request, response, err, resp, respBody, errURL, respURL, respBodyURL) => [
csrfCheck,
request,
response,
err,
resp,
respBody,
errURL,
respURL,
respBodyURL
]).map((csrfCheck, request, response, err, resp, respBody, errURL, respURL, respBodyURL) => {
Object.defineProperty(request.session, 'user', {
value: respBodyURL.phone.number,
enumerable: true,
writable: false,
configurable: true
});
return [
csrfCheck,
request,
response,
err,
resp,
respBody,
errURL,
respURL,
respBodyURL
];
}).map((csrfCheck, request, response, err, resp, respBody, errURL, respURL, respBodyURL) => {
(response.send({ success: true }))
return [
csrfCheck,
request,
response,
err,
resp,
respBody,
errURL,
respURL,
respBodyURL
];
}).then((csrfCheck, request, response, err, resp, respBody, errURL, respURL, respBodyURL) => []);
IO.createIO(cb => app.get('/logout', cb)).map((request, response) => [
request,
response
]).map((request, response) => {
(delete request.session.user)
return [
request,
response
];
}).map((request, response) => {
(response.redirect('/'))
return [
request,
response
];
}).then((request, response) => []);
const PORT = process.env.PORT || 3000;
app.listen(PORT);