-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
125 lines (98 loc) · 3.16 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
const querystring = require('querystring');
const http = require('http');
const uuidv4 = require('uuid/v4');
const url = require('url');
const axios = require('axios');
require('dotenv').config()
const sessionState = uuidv4();
const config = process.env
const params = {
client_id: config.clientId,
redirect_uri: config.redirectUri,
scope: config.oauthScope,
response_type: 'code',
state: sessionState,
};
const queryString = querystring.stringify(params);
const apiAuthUrl = `${config.authUrl}?${queryString}`;
console.log(`Please open the following URL in your browser: \n${apiAuthUrl}`);
const retrieveTokens = async authorizationCode => {
const requestBody = {
client_id: config.clientId,
client_secret: config.clientSecret,
redirect_uri: config.redirectUri,
code: authorizationCode,
grant_type: 'authorization_code',
};
const response = await axios.post(config.tokenUrl, querystring.stringify(requestBody), {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
});
return {
accessToken: response.data.access_token,
refreshToken: response.data.refresh_token,
};
};
const refreshTokens = async refreshToken => {
const requestBody = {
client_id: config.clientId,
client_secret: config.clientSecret,
refresh_token: refreshToken,
grant_type: 'refresh_token',
};
const response = await axios.post(config.tokenUrl, querystring.stringify(requestBody), {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
});
return {
accessToken: response.data.access_token,
refreshToken: response.data.refresh_token,
};
};
const userInfo = async accessToken => {
const options = {
headers: {
Authorization: `Bearer ${accessToken}`,
},
};
const response = await axios.get(config.testApiEndpoint, options);
return response.data;
};
const handleRequest = async (request, response) => {
const requestUrl = url.parse(request.url);
if (requestUrl.pathname !== '/oauth') {
response.end();
return;
}
const queryParameter = querystring.parse(requestUrl.query);
const authorizationCode = queryParameter.code;
const receivedState = queryParameter.state;
if (receivedState !== sessionState) {
console.log('State in the callback does not match the state in the original request.');
response.end();
return;
}
// Get access token
console.log('Getting tokens...');
const tokens = await retrieveTokens(authorizationCode);
console.log('Received new tokens: \n', tokens);
// Get user information
console.log('Getting user information...');
const userInformation = await userInfo(tokens.accessToken);
console.log(userInformation);
// Refresh tokens
console.log('Refreshing tokens...');
const refreshedTokens = await refreshTokens(tokens.refreshToken);
console.log('Received new tokens: \n', tokens);
// Get user information using the refreshed accessToken
console.log('Getting user information...');
const userInformationWithRefreshedToken = await userInfo(refreshedTokens.accessToken);
console.log(userInformationWithRefreshedToken);
response.end();
};
const server = http.createServer(handleRequest);
server.listen(config.port, () => {
console.log('Server listening on: http://localhost:%s', config.port);
});