-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·127 lines (116 loc) · 3.46 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
const express = require('express');
const {
join
} = require('path');
const app = express();
const jwt = require('express-jwt');
const jwks = require('jwks-rsa');
const port = process.env.PORT || 3000;
const request = require('request');
app.use(express.static(join(__dirname, 'public')));
const jwtCheck = jwt({
secret: jwks.expressJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: 'https://dev-nuxjd7sz.auth0.com/.well-known/jwks.json'
}),
audience: 'orders',
issuer: 'https://dev-nuxjd7sz.auth0.com/',
algorithms: ['RS256']
});
app.get('/orders', jwtCheck, (req, res) => {
res.end(
JSON.stringify([{
orderID: 1,
pizzas: 7,
toppings: 'onion'
}])
);
});
app.get('/auth_config.json', (req, res) => {
res.sendFile(join(__dirname, 'auth_config.json'));
});
app.get('/userProfileInfo', async (req, res) => {
const userID = req.query.user_id;
const googleAccessToken = await getGoogleAccessToken(userID);
const connectionCount = await getUserConnections(googleAccessToken);
const userGender = await getUserGender(googleAccessToken);
res.end(JSON.stringify({
connectionCount,
userGender
}));
});
app.get('/*', (_, res) => {
res.sendFile(join(__dirname, 'index.html'));
});
app.listen(port);
function getAuth0AccessToken() {
var options = {
method: 'POST',
url: 'https://dev-nuxjd7sz.auth0.com/oauth/token',
headers: {
'content-type': 'application/json'
},
body: '{"client_id":"Uwa9PrcnmAc0QyqJCr59o8FsQ41FlOQN","client_secret":"7cHWor0NiHlRf8HlMhQf3LTYgBmsu_DTjxxApSTHOHzsyeYLdfCmFnV37NrgAokB","audience":"https://dev-nuxjd7sz.auth0.com/api/v2/","grant_type":"client_credentials"}'
};
return new Promise((resolve) => {
request(options, function (error, response, body) {
if (error) throw new Error(error);
resolve(JSON.parse(body));
});
});
}
async function getGoogleAccessToken(userID) {
const creds = await getAuth0AccessToken();
const access_token = creds.access_token;
var options = {
method: 'GET',
url: `https://dev-nuxjd7sz.auth0.com/api/v2/users/${userID}`,
headers: {
authorization: `Bearer ${access_token}`
}
};
return new Promise(resolve => {
request(options, function (error, response, body) {
var googleAccessToken = JSON.parse(body).identities[0].access_token;
resolve(googleAccessToken);
});
});
}
async function getUserConnections(googleAccessToken) {
var options = {
method: 'GET',
url: `https://people.googleapis.com/v1/people/me/connections?requestMask.includeField=person.names`,
headers: {
authorization: `Bearer ${googleAccessToken}`
}
};
return new Promise(resolve => {
request(options, function (error, response, body) {
if (response && response.statusCode == 200) {
var googleConnections = JSON.parse(body);
if (googleConnections && googleConnections.connections) {
resolve(googleConnections.connections.length);
}
}
});
});
}
function getUserGender(googleAccessToken) {
var options = {
method: 'GET',
url: 'https://people.googleapis.com/v1/people/me?personFields=genders',
headers: {
authorization: `Bearer ${googleAccessToken}`
}
};
return new Promise(resolve => {
request(options, function (error, response, body) {
if (response && response.statusCode == 200) {
const data = JSON.parse(body);
resolve(data.genders[0].formattedValue);
}
});
})
}