-
Notifications
You must be signed in to change notification settings - Fork 25
/
server.js
160 lines (133 loc) · 3.44 KB
/
server.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
156
157
158
159
160
"use strict";
const Client = require("coinbase").Client;
const async = require("async");
const bodyParser = require("body-parser");
const express = require("express");
const session = require("express-session");
const ExpressOIDC = require("@okta/oidc-middleware").ExpressOIDC;
// Globals
const OKTA_ISSUER_URI = process.env.OKTA_ISSUER_URI;
const OKTA_CLIENT_ID = process.env.OKTA_CLIENT_ID;
const OKTA_CLIENT_SECRET = process.env.OKTA_CLIENT_SECRET;
const REDIRECT_URI = process.env.REDIRECT_URI;
const PORT = process.env.PORT || "3000";
const SECRET = process.env.SECRET;
const client = new Client({
apiKey: process.env.COINBASE_APIKEY_ID,
apiSecret: process.env.COINBASE_APIKEY_SECRET
});
let account;
let transactions;
let app = express();
// App settings
app.set("view engine", "pug");
// App middleware
app.use("/static", express.static("static"));
app.use(session({
cookie: { httpOnly: true },
secret: SECRET
}));
// Authentication
let oidc = new ExpressOIDC({
issuer: OKTA_ISSUER_URI,
client_id: OKTA_CLIENT_ID,
client_secret: OKTA_CLIENT_SECRET,
redirect_uri: REDIRECT_URI,
routes: { callback: { defaultRedirect: "/dashboard" } },
scope: "openid profile"
});
app.use(oidc.router);
// App routes
app.get("/", (req, res) => {
res.render("index");
});
app.get("/dashboard", oidc.ensureAuthenticated(), (req, res) => {
res.render("dashboard", { transactions: transactions });
});
app.post("/dashboard", oidc.ensureAuthenticated(), bodyParser.urlencoded(), (req, res) => {
account.requestMoney({
to: req.body.email,
amount: req.body.amount,
currency: "USD",
description: req.body.description
}, (err, txn) => {
if (err) {
console.error(err);
return res.render("dashboard", { error: err });
}
updateTransactions((err, transactions) => {
if (err) {
console.error(err);
return res.render("dashboard", { error: err.message });
}
return res.render("dashboard", { transactions: transactions })
});
});
});
app.get("/logout", (req, res) => {
req.logout();
res.redirect("/");
});
// Helpers
function updateTransactions(cb) {
transactions = [];
let pagination = null;
async.doWhilst(
function(callback) {
account.getTransactions(pagination, (err, txns, page) => {
if (err) {
return callback(err);
}
pagination = page.next_uri ? page : false;
txns.forEach(txn => {
if (txn.type === "request") {
transactions.push(txn);
}
});
callback();
});
},
function() {
return pagination ? true: false;
},
function(err) {
if (err) {
return cb(err);
}
cb(null, transactions);
}
);
}
// Startup jobs
client.getAccounts({}, (err, accounts) => {
if (err) {
console.error(err);
}
accounts.forEach(acct => {
if (acct.primary) {
account = acct;
console.log("Found primary account: " + account.name + ". Current balance: " + account.balance.amount + " " + account.currency + ".");
console.log("Downloading initial list of transactions.");
updateTransactions(err => {
if (err) {
console.error(err);
}
});
}
});
});
// Cron jobs
setInterval(() => {
updateTransactions(err => {
if (err) {
console.error(err);
}
})
}, 1000 * 60 * 60);
// Server management
oidc.on("ready", () => {
app.listen(PORT);
});
oidc.on("error", err => {
console.error(err);
});