-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
156 lines (136 loc) · 4.69 KB
/
app.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
"use strict";
var bunyan = require("bunyan");
var express = require("express");
var open = require("open");
var request = require("request");
var conf = require("./conf.json");
var helpers = require("./lib/helpers.js");
var workflowReader = require("./lib/workflowReader.js");
var flow = require("./flow.js")
var logger = bunyan.createLogger({ name: "dwolla-api-workflows-server", serializers: bunyan.stdSerializers });
var app = express();
app.use(express.static("public"));
app.use(function(req, res, next) {
logger.info(req.method, req.path, req.query, req.headers, req.body ? req.body : "no body" );
next();
})
/*
server configurations
*/
/*
Note:
ManageCustomers is specific to whitelabel customers, and requires extra steps to turn on in production
so in production to get a token that can create/manipulate whitelabel customers you need:
1) A real dwolla account that has the ManageCustomers ability - contact Dwolla to turn this on
2) An application with the ManageCustomers scope - select this when creating the application
3) To Request an oauth token that has ManageCustomers scope with your ManageCustomers enabled account.
These scopes are from part 3. In Dwolla UAT 1 and 2 are taken care of for every user.
*/
var scopes = [
"Send",
"Transactions",
"Balance",
"Request",
"Contacts",
"AccountInfoFull",
"Funding",
"ManageAccount",
"ManageCustomers"
];
app.get("/oauth_button_path", function(req,res) {
res.send(
helpers.oauthHost() +
"/v2/authenticate?redirect_uri=" + conf.oauth_redirect +
"&client_id=" +
encodeURIComponent(conf.key) +
"&response_type=code&scope=" + scopes.join("|"));
});
app.get("/oauth", function(req, res) {
var code = req.query.code;
goGrabTokenWithCode(code, function(token, refresh, expire_time) {
helpers.save("access_token", token, function() {
res.redirect("/#work");
})
})
});
var context = {};
app.get("/initialSteps", function(req, res) {
context = {};
res.json(workflowReader.getInitialWorkflows());
})
app.get("/nextSteps", function(req, res) {
res.json(workflowReader.readStepsFor(req.query.fromStep))
})
app.get("/play", function(req, res) {
var workflow = workflowReader.getStepsForWorkflow(req.query.step);
var countToWatchFor = countRequestsToWatchFor(workflow);
logger.info("WAITING FOR "+ countToWatchFor + " REQUESTS")
var results = [];
flow(workflow, context, function(requestOptions, response) {
countToWatchFor--;
results.push({request: requestOptions, response: response});
if (countToWatchFor === 0) {
res.end(JSON.stringify(results));
}
})
})
function countRequestsToWatchFor(workflow) {
var count = 0;
workflow.forEach(function(e, i) {
logger.info(e)
if (e.expand) count += e.expand.length;
count++;
})
return count;
}
function goGrabTokenWithCode(code, func) {
var data = {
form: {
client_id: conf.key,
client_secret: conf.secret,
code: code,
grant_type: "authorization_code",
redirect_uri: conf.oauth_redirect
}
};
request.post(helpers.oauthHost() + "/v2/token", data, function (err, res, body) {
logger.info("request to server for token", data, "responded with", body)
var body = JSON.parse(body);
if (body.error) return logger.warn(body);
else {
for (var k in body) helpers.save(k, body[k]);
func(body.access_token, body.refresh_token, body.expires_in);
setInterval(refreshToken, body.expires_in/1 * 1000);
}
});
}
function refreshToken(func) {
var data = {
client_id: conf.key,
client_secret: conf.secret,
refresh_token: helpers.load("refresh_token"),
grant_type: "refresh_token"
};
helpers.post(helpers.oauthHost() + "/v2/token", data, function (err, res, body) {
logger.info("request to server for refreshed token", data, "responded with", body)
if (err) { func(err) ;return logger.warn(err); }
var body = JSON.parse(body);
if (body.error) { func(body); return logger.warn(body); }
else for (var k in body) helpers.save(k, body[k]);
func()
});
}
function startServer() {
logger.info("Starting server. at port", conf.port);
app.listen(conf.port);
var base = "http://localhost:"+conf.port+"/#";
helpers.save("init", {});
refreshToken(function(err) {
if (!err) open(base + "work");
else open(base + "start");
})
}
function initialize() {
startServer();
}
initialize();