-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
128 lines (110 loc) · 3.72 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
const fs = require('fs');
const path = require('path');
const axios = require('axios');
const querystring = require('querystring');
const session = require('express-session');
const express = require('express');
const app = express();
// Load configuration
const CONFIG = JSON.parse(fs.readFileSync('./config.json', {encoding: 'utf8'}));
// Set express middleware
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(express.static('public'))
app.use(session({
secret: 'secret session',
resave: false,
saveUninitialized: true
}));
axios.interceptors.request.use(request => {
console.log(JSON.stringify(request, null, 2))
return request
})
app.get('/config', function (req, res) {
res.json({
client_id: CONFIG.client_id,
oauth_url: CONFIG.oauth_url
});
});
app.get('/authorize', function (req, res) {
const url = new URL(CONFIG.oauth_url);
const clientId = CONFIG.client_id;
const redirectURL = CONFIG.redirect_url;
url.pathname = '/oauth2/authorize';
url.searchParams.set('response_type', 'code');
url.searchParams.set('scope', 'openid trading');
url.searchParams.set('client_id', clientId);
url.searchParams.set('redirect_uri', redirectURL);
console.log(url.toString())
res.redirect(url.toString());
});
app.get('/authorized', function (req, res) {
// Handle error responses
if (req.query.error) {
console.log(req.query.error);
res.json({error: req.query.error})
return;
}
// Exchange auth code for access token through back channel POST, authorization_grant flow
const tokenURL = CONFIG.oauth_url + '/oauth2/token';
let postBody = {
grant_type: 'authorization_code',
redirect_uri: CONFIG.redirect_url,
code: req.query.code,
client_id: CONFIG.client_id,
client_secret: CONFIG.client_secret
};
axios.request({
withCredentials: true,
method: 'POST',
url: tokenURL,
data: querystring.stringify(postBody)
}).then(function (result) {
console.log(result);
req.session.access_token = result.data.access_token;
req.session.refresh_token = result.data.refresh_token;
res.redirect('/');
}).catch(function (err) {
console.log("ERROR:: " + err);
res.redirect("/");
});
});
app.get('/logout', function (req, res) {
const url = new URL(CONFIG.oauth_url);
url.pathname = '/oauth2/logout';
url.searchParams.set('client_id', CONFIG.client_id);
url.searchParams.set('redirect_uri', CONFIG.redirect_logout_url);
url.searchParams.set('refresh_token', req.session.refresh_token);
res.redirect(url.toString());
});
app.get('/refresh', function (req, res) {
const tokenURL = CONFIG.oauth_url + '/oauth2/token';
let postBody = {
grant_type: 'refresh_token',
redirect_uri: CONFIG.redirect_url,
refresh_token: req.session.refresh_token,
client_id: CONFIG.client_id,
client_secret: CONFIG.client_secret
};
axios.request({
withCredentials: true,
method: 'POST',
url: tokenURL,
data: querystring.stringify(postBody)
}).then(function (result) {
console.log(result);
req.session.access_token = result.data.access_token;
req.session.refresh_token = result.data.refresh_token;
res.redirect('/');
}).catch(function (err) {
console.log("ERROR:: " + err);
res.redirect("/");
});
})
app.get('/', function (req, res) {
res.render('index', {access_token: req.session.access_token, refresh_token: req.session.refresh_token});
});
// Start server
app.listen(CONFIG.port, function () {
console.log(`Server started on port:${CONFIG.port}`)
});