fix authentication + public key linked to socket.io

This commit is contained in:
2025-02-16 23:59:05 +01:00
parent 156a39f7cb
commit 1816cd3cf1
7 changed files with 73 additions and 19 deletions

View File

@ -29,9 +29,45 @@ describe('authentication module', () => {
const result = await authentication.verifySignature(encodedData, signature, publicKeys);
expect(result).toBe(pemKey);
let fakeKey = `-----BEGIN PUBLIC KEY-----MCowBQYDK2VwAyEAmrBLT6lyiFh/eUticsIFNY6AkjXuQPqj0Qvb99pCJJk=-----END PUBLIC KEY-----`
let fakeKey = `-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEAmrBLT6lyiFh/eUticsIFNY6AkjXuQPqj0Qvb99pCJJk=\n-----END PUBLIC KEY-----`
const result2 = await authentication.verifySignature(encodedData, signature, [fakeKey,]);
expect(result2).toBe(null);
});
it('encoding and decoding test', async () => {
const testkey = '-----BEGIN PRIVATE KEY-----\nMC4CAQAwBQYDK2VwBCIEILXfcMirlUiI/o/r+8rK7ZIP0fyAMynxYzJXCI6BJHPk\n-----END PRIVATE KEY-----';
const base64 = testkey.replace(`-----BEGIN PRIVATE KEY-----`, '').replace(`-----END PRIVATE KEY-----`, '').trim();
const derBuffer = Buffer.from(base64, 'base64');
let privkey = await crypto.subtle.importKey(
"pkcs8",
derBuffer,
{
name: "Ed25519",
},
true,
["sign"],
);
const msg2 = '12f4b99e3784ac2e8b95427533a3dbc4';
const encoder2 = new TextEncoder();
const encodedData2 = encoder2.encode(msg2);
const signature2 = await crypto.subtle.sign(
{
name: "Ed25519",
},
privkey,
encodedData2,
);
const signatureBase64 = Buffer.from(signature2).toString('base64');
const sig = Buffer.from(signatureBase64, 'base64');
let pemKey2 = '-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEALrj4A3Vftz5TgWXEHi5KG+HD+uQLGB3bGc4TprDi9kE=\n-----END PUBLIC KEY-----'
const publicKeys2 = [pemKey2,];
const result3 = await authentication.verifySignature(encodedData2, sig, publicKeys2);
expect(result3).toBe(pemKey2);
});
});
});