-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathjwt.ts
More file actions
24 lines (21 loc) · 772 Bytes
/
Copy pathjwt.ts
File metadata and controls
24 lines (21 loc) · 772 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { IJwtCredentialV3 } from '../../sync/streaming/AuthClient/types';
function toBase64Url(str: string) {
return Buffer.from(str).toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
}
export function makeJwtCredential(expInSeconds = 3600): IJwtCredentialV3 {
const now = Math.floor(Date.now() / 1000);
const header = toBase64Url(JSON.stringify({ alg: 'HS256' }));
const decodedToken = { iat: now, exp: now + expInSeconds, 'x-ably-capability': '{"ch":["subscribe"]}' };
const payload = toBase64Url(JSON.stringify(decodedToken));
return {
token: `${header}.${payload}.sig`,
decodedToken,
channels: { ch: ['subscribe'] },
config: {
streaming: {
enabled: true,
delay: 60,
}
}
};
}