-
Notifications
You must be signed in to change notification settings - Fork 2
/
jwt.js
32 lines (28 loc) · 1.13 KB
/
jwt.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
base64 = x => btoa(String.fromCharCode.apply(null, new Uint8Array(x)))
.replace(/\+/g, '-').replace(/\//g, '_').replace(/={1,2}$/,'');
var UTF8 = {
_encoder: new TextEncoder("utf-8"),
encode(x) { return this._encoder.encode(x); },
_decoder: new TextDecoder("utf-8"),
decode(x) { return this._decoder.decode(x); }
};
jenc = x => base64(UTF8.encode(JSON.stringify(x)));
var jwtHeader = {
"typ": "JWT",
"alg": "ES256"
};
console.log('header', JSON.stringify(jwtHeader));
var jwtBody = {
"aud": "https://push.example.net",
"exp": Math.floor(Date.now() / 1000) + 60*60*24,
"sub": "mailto:push@example.com"
};
console.log('header', JSON.stringify(jwtBody));
crypto.subtle.generateKey({name: 'ECDSA', namedCurve: 'P-256'}, false, ['sign'])
.then(k => {
crypto.subtle.exportKey('raw', k.publicKey)
.then(p => console.log('Crypto-Key: p256ecdsa=' + base64(p)));
crypto.subtle.sign({name: 'ECDSA', hash: 'SHA-256'}, k.privateKey,
UTF8.encode(jenc(jwtHeader) + '.' + jenc(jwtBody)))
.then(sig => console.log('Authorization: WebPush ' + jenc(jwtHeader) + '.' + jenc(jwtBody) + '.' + base64(sig)));
});