1
0
forked from apxtri/apxtri
apxtri/models/unittest/Pagansunittest.js

190 lines
5.1 KiB
JavaScript
Raw Normal View History

2024-07-09 21:11:40 +03:00
const assert = require('assert');
const openpgp = require('openpgp');
const Pagans = require('../Pagans');
2024-07-11 15:51:11 +03:00
const dayjs = require('dayjs');
2024-06-30 22:06:37 +02:00
2024-07-09 21:11:40 +03:00
const ut = { name: 'Pagans' };
// Data
ut.test = {
tribe: "smatchit",
pagans: [
{
alias: "unittestadminsmatchit",
passphrase: "adminsmatchitPass",
persons: { firstname: "toto", lastname: "titi", profils: ["anonymous"] },
testprofil: "adminrecruiter",
},
{
alias: "unittestseeker",
passphrase: "",
persons: { firstname: "toto", lastname: "titi", profils: ["anonymous"] },
testprofil: "seeker",
},
{
alias: "unittestrecruiter",
passphrase: "recruiterPass",
persons: { firstname: "toto", lastname: "titi", profils: ["anonymous"] },
testprofil: "recruiter",
},
{
alias: "unittestadminrecruiter",
passphrase: "adminrecruiterPass",
persons: { firstname: "toto", lastname: "titi", profils: ["anonymous"] },
testprofil: "adminrecruiter",
},
],
};
// Apx
2024-06-30 22:06:37 +02:00
const apx = {};
2024-07-11 15:51:11 +03:00
apx.data = {};
apx.data.headers = {};
2024-06-30 22:06:37 +02:00
apx.generateKey = async (alias, passphrase) => {
2024-07-03 12:13:08 +02:00
try {
console.log(`\nGenerating keys for alias: ${alias}`);
const pgpParams = {
type: "ecc",
curve: "curve25519",
userIDs: [{ name: alias }],
passphrase: passphrase,
format: "armored",
};
const key = await openpgp.generateKey(pgpParams);
console.log(`Keys generated successfully for alias: ${alias}`);
return {
alias,
passphrase,
privatekey: key.privateKey,
publickey: key.publicKey,
};
} catch (error) {
console.error(`Error generating keys for alias: ${alias}`, error);
return {};
}
2024-06-30 22:06:37 +02:00
};
apx.createIdentity = async (alias, passphrase) => {
2024-07-03 12:13:08 +02:00
try {
const { privatekey, publickey } = await apx.generateKey(alias, passphrase);
2024-07-09 21:11:40 +03:00
if (!privatekey || !publickey) {
throw new Error(`Failed to generate keys for ${alias}`);
}
2024-07-03 12:13:08 +02:00
console.log(`Identity created successfully for alias: ${alias}`);
return { alias, privatekey, publickey };
} catch (error) {
console.error(`Error creating identity for alias: ${alias}`, error);
2024-07-11 15:51:11 +03:00
throw error;
2024-07-03 12:13:08 +02:00
}
2024-06-30 22:06:37 +02:00
};
2024-07-11 15:51:11 +03:00
apx.clearmsgSignature = async (privateKeyArmored, passphrase, message) => {
2024-07-09 21:11:40 +03:00
try {
2024-07-11 15:51:11 +03:00
const privateKey = await openpgp.readPrivateKey({ armoredKey: privateKeyArmored });
let decryptedPrivateKey = privateKey;
if (!privateKey.isDecrypted()) {
decryptedPrivateKey = await openpgp.decryptKey({
privateKey,
passphrase
});
}
const signedMessage = await openpgp.sign({
message: await openpgp.createMessage({ text: message }),
signingKeys: decryptedPrivateKey
});
return signedMessage;
2024-07-09 21:11:40 +03:00
} catch (error) {
2024-07-11 15:51:11 +03:00
console.error('Error signing message:', error);
return null;
}
};
apx.authenticate = async (alias, passphrase, privatekey) => {
try {
apx.data.headers.xalias = alias;
apx.data.headers.xdays = dayjs().valueOf();
const msg = `${alias}_${apx.data.headers.xdays}`;
console.log("pvk", privatekey);
apx.data.headers.xhash = await apx.clearmsgSignature(privatekey, passphrase, msg);
if (!apx.data.headers.xhash) {
throw new Error('Failed to generate xhash for authentication');
}
console.log(`Authentication successful for alias: ${alias}`);
return apx.data.headers;
} catch (error) {
console.error(`Error authenticating alias: ${alias}`, error);
2024-07-09 21:11:40 +03:00
throw error;
}
2024-06-30 22:06:37 +02:00
};
2024-07-09 21:11:40 +03:00
ut.createIdentity = async (t) => {
try {
// Test if alias already exists
const getalias = await Pagans.getalias(t.alias);
if (getalias.status !== 404) {
console.log(`Alias ${t.alias} already exists. Deleting...`);
await Pagans.deletealias(t.alias);
console.log(`Deleted ${t.alias}.`);
2024-07-03 12:13:08 +02:00
}
2024-07-09 21:11:40 +03:00
// Generate keys
const keys = await apx.createIdentity(t.alias, t.passphrase);
if (!keys.privatekey || !keys.publickey) {
throw new Error(`Failed to generate keys for ${t.alias}`);
2024-07-03 12:13:08 +02:00
}
2024-06-30 22:06:37 +02:00
2024-07-09 21:11:40 +03:00
return keys;
} catch (error) {
throw new Error(`Error creating identity for ${t.alias}: ${error.message}`);
}
2024-06-30 22:06:37 +02:00
};
2024-07-11 15:51:11 +03:00
// Authentication process
ut.authenticate = async (t, privatekey, passphrase) => {
try {
const headers = await apx.authenticate(t.alias, passphrase, privatekey);
return headers;
} catch (error) {
throw new Error(`Error authenticating ${t.alias}: ${error.message}`);
}
};
2024-07-09 21:11:40 +03:00
ut.run = async () => {
console.log('Test Pagans Registration and Authentication');
2024-06-30 22:06:37 +02:00
2024-07-09 21:11:40 +03:00
for (const t of ut.test.pagans) {
try {
console.log(`Creating identity for ${t.alias}`);
const identity = await ut.createIdentity(t);
2024-07-11 15:51:11 +03:00
console.log(`Authenticating ${t.alias}`);
const headers = await ut.authenticate(t, identity.privatekey, t.passphrase);
console.log(`Headers for ${t.alias}:`, headers);
2024-07-09 21:11:40 +03:00
console.log(`All operations for ${t.alias} completed successfully.`);
} catch (error) {
console.error(`Error processing ${t.alias}: ${error.message}`);
2024-06-30 22:06:37 +02:00
}
2024-07-03 12:13:08 +02:00
}
2024-07-09 21:11:40 +03:00
console.log('All test cases ran successfully');
2024-06-30 22:06:37 +02:00
};
module.exports = ut;
2024-07-09 21:11:40 +03:00
2024-06-30 22:06:37 +02:00
if (require.main === module) {
2024-07-09 21:11:40 +03:00
ut.run().catch(err => {
console.error('Test case failed:', err);
2024-07-03 12:13:08 +02:00
});
2024-06-30 22:06:37 +02:00
}