-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathserver.ts
62 lines (52 loc) · 1.81 KB
/
server.ts
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
import express from 'express';
import path from 'path';
import dotenv from 'dotenv';
import cors from 'cors';
import crypto from 'crypto';
dotenv.config({ path: path.resolve(__dirname, '../../../.env.local') });
const app = express();
const port = 3001;
app.use(
cors({
origin: ['http://localhost:3000', 'http://localhost:5173'],
methods: ['GET', 'POST'],
}),
);
function base64urlEncode(str: string) {
return str.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
}
app.get('/generate-jwt', async (_req, res) => {
console.log('1 - /generate-jwt endpoint called');
const ablyApiKey = process.env.VITE_PUBLIC_ABLY_KEY || '';
const [apiKeyName, apiKeySecret] = ablyApiKey.split(':');
try {
if (ablyApiKey === '') {
throw new Error('VITE_PUBLIC_ABLY_KEY is not set');
}
const header = {
typ: 'JWT',
alg: 'HS256',
kid: apiKeyName,
};
const currentTime = Math.round(Date.now() / 1000);
const claims = {
iat: currentTime /* current time in seconds */,
exp: currentTime + 3600 /* time of expiration in seconds */,
'x-ably-capability': '{"*":["*"]}',
};
const base64Header = base64urlEncode(btoa(JSON.stringify(header)));
const base64Claims = base64urlEncode(btoa(JSON.stringify(claims)));
/* Apply the hash specified in the header */
const hmac = crypto.createHmac('sha256', apiKeySecret);
hmac.update(base64Header + '.' + base64Claims);
const signature = base64urlEncode(hmac.digest('base64'));
const ablyJwt = base64Header + '.' + base64Claims + '.' + signature;
console.log('2 - JWT generated: ', ablyJwt);
res.json(ablyJwt);
} catch (error) {
res.status(500).json({ error: 'Failed to generate token' });
}
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});