-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathclient.js
93 lines (81 loc) · 2.42 KB
/
client.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
const express = require("express")
const bodyParser = require("body-parser")
const axios = require("axios").default
const { randomString, timeout } = require("./utils")
const url = require('url');
const config = {
port: 9000,
clientId: "my-client",
clientSecret: "zETqHgl0d7ThysUqPnaFuLOmG1E=",
redirectUri: "http://localhost:9000/callback",
authorizationEndpoint: "http://localhost:9001/authorize",
tokenEndpoint: "http://localhost:9001/token",
userInfoEndpoint: "http://localhost:9002/user-info",
}
let state = ""
const app = express()
app.set("view engine", "ejs")
app.set("views", "assets/client")
app.use(timeout)
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
/*
Your code here
*/
app.get('/authorize', (req, res) => {
state = randomString();
const u = new URL(config.authorizationEndpoint);
u.searchParams.append('response_type', 'code');
u.searchParams.append('client_id', config.clientId);
u.searchParams.append('client_secret', config.clientSecret);
u.searchParams.append('redirect_uri', config.redirectUri);
u.searchParams.append('scope', 'permission:name permission:date_of_birth');
u.searchParams.append('state', state);
console.log(u.href);
res.redirect(u.href);
/*
let url = config.authorizationEndpoint + '/?' +
'response_type=code&' +
'client_id=' + encodeURIComponent(config.clientId) + '&' +
'client_secret=' + encodeURIComponent(config.clientSecret) + '&' +
'redirect_uri=' + encodeURIComponent(config.redirectUri) + '&' +
'scope=permission:name permission:date_of_birth&' +
'state=' + encodeURIComponent(state);
res.redirect(url);
*/
});
app.get('/callback', (req, res) => {
if (state !== req.query.state) {
res.status(403).end();
return;
}
axios({
method: 'POST',
url: config.tokenEndpoint,
auth: {username: config.clientId, password: config.clientSecret},
data: {code: req.query.code}
}).then(response => {
return axios({
method: 'GET',
url: config.userInfoEndpoint,
headers: {authorization: "bearer " +response.data.access_token}
}).then(response => {
res.render("welcome", {user: response.data});
});
});
});
const server = app.listen(config.port, "localhost", function () {
var host = server.address().address
var port = server.address().port
})
// for testing purposes
module.exports = {
app,
server,
getState() {
return state
},
setState(s) {
state = s
},
}