forked from auth0/jwt-handbook-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hs256.js
33 lines (27 loc) · 1.08 KB
/
hs256.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
import { encode as base64Encode } from './base64.js';
import sha256 from './sha256.js';
import hmac from './hmac.js';
import { stringToUtf8, uint32ArrayToUint8Array } from './utils.js';
function b64(data) {
if(data instanceof Uint8Array) {
return base64Encode(data).replace(/=/g, '');
} else {
return base64Encode(stringToUtf8(data)).replace(/=/g, '');
}
}
export default function jwtEncode(header, payload, secret) {
if(typeof header !== 'object' || typeof payload !== 'object') {
throw new Error('header and payload must be objects');
}
if(typeof secret !== 'string') {
throw new Error("secret must be a string");
}
header.alg = 'HS256';
const encHeader = b64(JSON.stringify(header));
const encPayload = b64(JSON.stringify(payload));
const jwtUnprotected = `${encHeader}.${encPayload}`;
const signature = b64(uint32ArrayToUint8Array(
hmac(sha256, 512, secret, stringToUtf8(jwtUnprotected), true)));
return `${jwtUnprotected}.${signature}`;
}
console.log(jwtEncode({}, {sub: "[email protected]"}, 'secret'));