-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
53 lines (44 loc) · 1.17 KB
/
index.js
File metadata and controls
53 lines (44 loc) · 1.17 KB
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
const oauth2 = require('simple-oauth2');
const express = require('express');
const appUrl = process.env.APP_URL;
const oAuthUrl = process.env.OAUTH2_URL;
const clientId = process.env.OAUTH2_CLIENT_ID;
const clientSecret = process.env.OAUTH2_CLIENT_SECRET;
const credentials = {
client: {
id: clientId,
secret: clientSecret
},
auth: {
tokenHost: oAuthUrl,
tokenPath: '/oauth2/token',
authorizePath: '/oauth2/auth'
}
};
const app = express();
const client = oauth2.create(credentials);
app.get('/', (req, res) => {
// Authorization oauth2 URI
const authorizationUri = client.authorizationCode.authorizeURL({
redirect_uri: `${appUrl}/callback`,
state: '123456789',
scope: 'openid offline'
});
res.redirect(authorizationUri);
});
app.get('/callback', async (req, res) => {
const tokenConfig = {
code: req.query.code,
redirect_uri: `${appUrl}/callback`,
scope: 'openid offline'
};
try {
const result = await client.authorizationCode.getToken(tokenConfig);
const accessToken = client.accessToken.create(result);
res.send(accessToken);
} catch (e) {
console.error(e);
res.status(500);
}
});
app.listen(3000);