-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
113 lines (84 loc) · 3.19 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
// app.js
var fs = require('fs');
//Certificate for https
var https_options = {
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.crt'),
requestCert: false,
rejectUnauthorized: false
};
var express = require('express');
var app = express();
var server = require('https').createServer(https_options,app);
var io = require('socket.io')(server);
var request = require('request');
var port = process.env.PORT || 3000;
app.use(express.static(__dirname + '/public'));
app.get(['/', '/callback'], function (req, res, next) {
if (req.query.code !== undefined) {
var code = req.query.code;
res.cookie('authCode', code);
res.cookie('clientId', consoleClientID);
}
res.redirect('..');
});
server.listen(port);
var consoleClientID, consoleClientSecret, consoleAuthCode, consoleAccessToken, consoleAuthURL, consoleTokenURL,
consoleScopes;
io.on('connection', function (socket) {
socket.emit('welcome', {});
socket.emit('getCookie', {});
socket.on('getAuthCode', function (consoleCredentials) {
consoleClientID = consoleCredentials.clientID;
consoleAuthURL = consoleCredentials.authEndpoint;
consoleScopes = consoleCredentials.scope;
var url = consoleAuthURL + "?response_type=code&client_id=" + consoleClientID + "&scope=" + consoleScopes;
socket.emit('openUrl', {url: url});
});
socket.on('getAccessToken', function (tokenCredentials) {
consoleTokenURL = tokenCredentials.tokenEndpoint;
consoleClientID = tokenCredentials.clientID;
consoleClientSecret = tokenCredentials.clientSecret;
consoleAuthCode = tokenCredentials.authCode;
var grant_type = "authorization_code";
var accessTokenOptions = {
method: 'POST',
url: consoleTokenURL,
headers: {
'User-Agent': 'Mozilla/5.0'
},
formData: {
client_id: consoleClientID,
client_secret: consoleClientSecret,
code: consoleAuthCode,
grant_type: grant_type
}
};
try {
request(accessTokenOptions, function (error, response, body) {
if (error) {
throw error;
}
if (response.statusCode == 200) {
var token = JSON.parse(body).access_token;
var refresh = JSON.parse(body).refresh_token;
socket.emit('accessToken', {access: token, refresh: refresh, clientId:consoleClientID});
}
else {
socket.emit('message', {
status: 'fail',
text: "Something went wrong, please check the client secret, client id, authorization code and token endpoint URL",
id: 2
});
}
});
}
catch (err) {
socket.emit('message', {
status: 'fail',
text: "Something went wrong, please check the client secret, client id, authorization code and token endpoint URL",
id: 2
});
}
});
});