slowly figuring out JWTs and WebCrypto API (ref. #28)

merge-requests/9/merge
Michał 'rysiek' Woźniak 2022-01-12 03:54:36 +00:00
rodzic 580042b0c3
commit 68ad9b1fa6
1 zmienionych plików z 38 dodań i 2 usunięć

Wyświetl plik

@ -1,6 +1,28 @@
const { subtle } = require('crypto').webcrypto;
describe("plugin: signed-integrity", () => {
beforeEach(() => {
var keypair = null
async function generateECDSAKeypair() {
if (keypair == null) {
keypair = await subtle.generateKey({
name: "ECDSA",
namedCurve: "P-384"
},
true,
["sign", "verify"]
);
}
return keypair;
}
async function getArmouredKey(key) {
return JSON.stringify(await subtle.exportKey('jwk', key))
}
beforeEach(async () => {
global.nodeFetch = require('node-fetch')
global.Request = global.nodeFetch.Request
global.Response = global.nodeFetch.Response
@ -22,9 +44,23 @@ describe("plugin: signed-integrity", () => {
}
}
// debug
console.log(await getArmouredKey((await generateECDSAKeypair()).publicKey))
console.log(await getArmouredKey((await generateECDSAKeypair()).privateKey))
// ES384: ECDSA using P-384 and SHA-384
header = btoa('{"alg": "ES384"}').replace(/\//g, '_').replace(/\+/g, '-').replace(/=/g, '')
payload = btoa('{"integrity": "sha256-eiMrFuthzteJuj8fPwUMyNQMb2SMW7VITmmt2oAxGj0="}').replace(/\//g, '_').replace(/\+/g, '-').replace(/=/g, '')
signature = btoa('FIXME').replace(/\//g, '_').replace(/\+/g, '-').replace(/=/g, '')
signature = await subtle.sign(
{
name: "ECDSA",
hash: {name: "SHA-384"}
},
(await generateECDSAKeypair()).privateKey,
(header + '.' + payload)
)
signature = btoa(signature).replace(/\//g, '_').replace(/\+/g, '-').replace(/=/g, '')
global.resolvingFetch = jest.fn((url, init)=>{
var content = '{"test": "success"}'