From 0f640116ec552aa8e25c59d431e8ee1348b9d456 Mon Sep 17 00:00:00 2001 From: phondropoulos Date: Thu, 11 Jul 2024 15:51:11 +0300 Subject: [PATCH] Pagans unittest update (with auth) --- models/unittest/Pagansunittest.js | 69 +++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 12 deletions(-) diff --git a/models/unittest/Pagansunittest.js b/models/unittest/Pagansunittest.js index 8313da9..9b2a3e5 100644 --- a/models/unittest/Pagansunittest.js +++ b/models/unittest/Pagansunittest.js @@ -1,7 +1,7 @@ const assert = require('assert'); const openpgp = require('openpgp'); const Pagans = require('../Pagans'); - +const dayjs = require('dayjs'); const ut = { name: 'Pagans' }; @@ -38,6 +38,8 @@ ut.test = { // Apx const apx = {}; +apx.data = {}; +apx.data.headers = {}; apx.generateKey = async (alias, passphrase) => { try { @@ -75,19 +77,50 @@ apx.createIdentity = async (alias, passphrase) => { return { alias, privatekey, publickey }; } catch (error) { console.error(`Error creating identity for alias: ${alias}`, error); - throw error; + throw error; } }; -// Mock joinTribe -apx.joinTribe = async (alias, tribe) => { +apx.clearmsgSignature = async (privateKeyArmored, passphrase, message) => { try { - console.log(`Joining alias ${alias} to tribe ${tribe}`); - - console.log(`Alias ${alias} successfully joined tribe ${tribe}`); - return { status: 200, message: 'Success' }; // Mock success response + 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; } catch (error) { - console.error(`Error joining tribe for alias ${alias}: ${error.message}`); + 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); throw error; } }; @@ -109,15 +142,22 @@ ut.createIdentity = async (t) => { throw new Error(`Failed to generate keys for ${t.alias}`); } - // Join tribe - await apx.joinTribe(t.alias, ut.test.tribe); - return keys; } catch (error) { throw new Error(`Error creating identity for ${t.alias}: ${error.message}`); } }; +// 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}`); + } +}; + ut.run = async () => { console.log('Test Pagans Registration and Authentication'); @@ -125,6 +165,11 @@ ut.run = async () => { try { console.log(`Creating identity for ${t.alias}`); const identity = await ut.createIdentity(t); + + console.log(`Authenticating ${t.alias}`); + const headers = await ut.authenticate(t, identity.privatekey, t.passphrase); + console.log(`Headers for ${t.alias}:`, headers); + console.log(`All operations for ${t.alias} completed successfully.`); } catch (error) { console.error(`Error processing ${t.alias}: ${error.message}`);