-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
115 lines (94 loc) · 3.45 KB
/
Copy pathindex.js
File metadata and controls
115 lines (94 loc) · 3.45 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
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
import express from 'express';
import randomstring from 'randomstring';
import * as jose from 'jose';
import fetch from 'node-fetch';
const app = express();
const port = parseInt(process.env.PORT, 10) || 8080;
const AUTH_HOST = process.env.AUTH_HOST || 'https://auth.eks.codebrick.io';
const CLIENT_ID = process.env.CLIENT_ID || 'client-1';
const CLIENT_SECRET = process.env.CLIENT_SECRET || 'client-1-secret';
const REDIRECT_URI = process.env.REDIRECT_URI || `http://localhost:${port}/oauth_callback`;
const JWKS = jose.createRemoteJWKSet(new URL(AUTH_HOST + '/.well-known/jwks.json'));
class SillyStateStore {
constructor() {
this.states = {};
}
get(state) {
return this.states[state];
}
delete(state) {
delete this.states[state];
}
set(state) {
this.states[state] = true;
}
}
const stateStore = new SillyStateStore();
app.set('view engine', 'pug');
app.get('/', (req, res) => {
res.render('index', {
auth_host: AUTH_HOST
})
});
app.listen(port, () => {
console.log(`example client is running on port ${port}`);
});
app.get('/signin', (req, res) => {
// Generate 'state' and store it. 'state' is used for preventing cross-site request forgery.
const state = randomstring.generate();
stateStore.set(state);
// Request authorization by redirecting the user to the authorization endpoint.
res.redirect(`${AUTH_HOST}/auth?response_type=code&client_id=${CLIENT_ID}&redirect_uri=${REDIRECT_URI}&scope=openid+profile&state=${state}`);
});
app.get('/oauth_callback', async (req, res) => {
const code = req.query.code;
const state = req.query.state;
// Check 'state' is same as you sent.
const storedState = stateStore.get(state);
if (!storedState) {
res.render('error', {error: 'Invalid state'});
return;
}
stateStore.delete(state);
// Exchange code for tokens.
let json
try {
const response = await fetch(`${AUTH_HOST}/oauth/token`, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': `Basic ${Buffer.from(`${CLIENT_ID}:${CLIENT_SECRET}`).toString('base64')}`,
},
body: `grant_type=authorization_code&code=${code}&redirect_uri=${REDIRECT_URI}`
});
// You'll get access_token and id_token. id_token contains user's identity information.
json = await response.json();
console.log(json);
} catch (e) {
console.error('failed to exchange code:' + e);
res.render('error', {error: e.toString()});
return;
}
// Verify id_token before using it.
let idTokenPayload
try {
const { payload } = await jose.jwtVerify(json.id_token, JWKS, {
issuer: 'https://accounts.eks.codebrick.io',
audience: CLIENT_ID,
});
idTokenPayload = payload
} catch (e) {
console.error('failed to verify id_token:' + e);
res.render('error', {error: e.toString()});
return;
}
console.log(idTokenPayload);
// Now using payload of id_token, you can identify the user.
// 'sub', which is abbreviation of subject, is the user's identifier.
// Create a session or proceed to additional sign-up step if the user visits the site for the first time.
res.render('success', {
response: json,
token_payload: idTokenPayload,
signout_url: `${AUTH_HOST}/signout`,
});
});