-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
78 lines (66 loc) · 2.13 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
require('dotenv').config();
const { Issuer } = require('openid-client');
const express = require('express');
const session = require('express-session');
const app = express();
const port = 3000;
app.use(session({
secret: '123456',
resave: true,
saveUninitialized: true
}));
Issuer.defaultHttpOptions = { timeout: 15000 }
const issuer = new Issuer({
issuer: process.env.APPID_OAUTHSERVERURL,
authorization_endpoint: process.env.APPID_OAUTHSERVERURL+'/authorization',
token_endpoint: process.env.APPID_OAUTHSERVERURL+'/token',
userinfo_endpoint: process.env.APPID_OAUTHSERVERURL+'/userinfo',
jwks_uri: process.env.APPID_OAUTHSERVERURL+'/publickeys',
});
console.log('Issuer %s %O', issuer.issuer, issuer.metadata);
issuer.defaultHttpOptions = { timeout: 15000 }
const client = new issuer.Client({
client_id: process.env.APPID_CLIENTID,
client_secret: process.env.APPID_SECRET
});
let authorizationUrl = client.authorizationUrl({
redirect_uri: process.env.REDIRECT_URL_CALLBACK,
scope: 'openid email profile'
});
console.log(authorizationUrl)
app.get('/callback', (req, res) => {
client.authorizationCallback(process.env.REDIRECT_URL_CALLBACK,
req.query, { 'response_type': 'code' })
.then(function (tokenSet) {
client.userinfo(tokenSet.access_token)
.then(function (userinfo) {
res.redirect(process.env.REDIRECT_URL_WEB_APP +
'?name=' + userinfo.name +
'&email=' + userinfo.email +
'&id_token=' + tokenSet.id_token +
'&access_token=' + tokenSet.access_token
);
})
.catch(function (error) {
console.log(error);
res.redirect(process.env.REDIRECT_URL_WEB_APP);
})
})
.catch(function (error) {
console.log(error);
res.redirect(process.env.REDIRECT_URL_WEB_APP);
})
})
app.get('/loginurl', (req, res) => {
res.json({ loginUrl: authorizationUrl });
})
app.get('/login', (req, res) => {
res.redirect(authorizationUrl);
})
app.get('/health', (req, res) => {
res.send('up');
})
app.get('/', (req, res) => {
res.send('up');
})
app.listen(3000, () => console.log('Listening on port 3000'))