From 05025d209a58f3f41979e1d1fe943134ddc7e814 Mon Sep 17 00:00:00 2001 From: jusax23 Date: Sun, 12 Mar 2023 19:10:36 +0100 Subject: [PATCH] post remote tests and fixes and logs removed --- src/api/acts/login.ts | 3 --- src/api/post.ts | 24 ++++++++++-------- src/sys/selfTag.ts | 1 - tests/post.js | 4 +-- tests/tests/post.js | 59 +++++++++++++++++++++++++++++++++++++++++++ tests/tests/ws.js | 2 +- tests/ws.js | 2 +- 7 files changed, 77 insertions(+), 18 deletions(-) diff --git a/src/api/acts/login.ts b/src/api/acts/login.ts index caa2c98..2cc1e8f 100644 --- a/src/api/acts/login.ts +++ b/src/api/acts/login.ts @@ -167,11 +167,9 @@ export const remote1 = { try { let server = await outbagURLfromTag(data.server); var cert = await getRemote(server); - console.log(server, cert); var tagAcert = `${data.name}@${server.host}:${server.port}-${data.publicKey}`; if (!(await verify(tagAcert, data.sign, cert))) { client.suspect(); - console.log("verify"); aws("error", "signature"); return; } @@ -183,7 +181,6 @@ export const remote1 = { client.remoteKey = data.publicKey; aws("ok", client.challenge); } catch (e) { - console.log(e); client.suspect(); aws("error", "signature"); } diff --git a/src/api/post.ts b/src/api/post.ts index f4ac8d4..e068ee3 100644 --- a/src/api/post.ts +++ b/src/api/post.ts @@ -31,15 +31,18 @@ export const addPostMethods = (server: express.Express) => { } debug("POST", "reveived:", req.body); const aws = (state: string, data: any) => { + debug("POST", "send:", state, data); res.status(state == "error" ? 400 : 200); - res.json({data}); + res.json({ data }); }; let client: postClient | null = null; try { let auth = authorization.parse(req.headers["authorization"] ?? ""); if (auth.scheme == "outbagServer") { + debug("POST", "auth: outbag Server"); throw new Error("not implemented"); } else if (auth.token != null && typeof auth.token == "string") { + debug("POST", "auth: temp Token"); if (tempTokens[auth.token] != null) { client = tempTokens[auth.token]; } else { @@ -47,7 +50,15 @@ export const addPostMethods = (server: express.Express) => { aws("error", "token"); return; } - } else if (auth?.params?.name != null && auth?.params?.server != null && auth?.params?.accountKey != null && typeof auth?.params?.name == "string" && auth?.params?.server == "string" && typeof auth?.params?.accountKey == "string") { + } else if ( + auth?.params?.name != null + && auth?.params?.server != null + && auth?.params?.accountKey != null + && typeof auth?.params?.name == "string" + && typeof auth?.params?.server == "string" + && typeof auth?.params?.accountKey == "string" + ) { + debug("POST", "auth: credentials"); client = new postClient(req.ip); client.client.name = auth?.params?.name; let serverTag = auth?.params?.server; @@ -100,35 +111,30 @@ export class postClient { let { state, data, right, func } = act; if (!(state & this.client.state)) { aws("error", "wrongstate"); - debug("POST", "send:", "error", "wrongstate"); this.client.suspect(); return; } if (typeof json.data == "undefined") { aws("error", "data"); - debug("POST", "send:", "error", "data"); return; } if (data) { for (let d in data) { if (!checktype(json.data[d], data[d])) { - aws("error", "data"); debug("POST", "Data check error. Key: ", d, "; Type:", data[d], "; Value:", json.data[d]); - debug("POST", "send:", "error", "data"); + aws("error", "data"); return; } } } if (right && !(await this.client.checkRight(right))) { aws("error", "right"); - debug("POST", "send:", "error", "right"); this.client.suspect(); return; } var send = false; try { await func(this.client, json.data, (state, data = "") => { - debug("POST", "send:", state, data); aws(state, data); send = true; }); @@ -137,11 +143,9 @@ export class postClient { } if (!send) { - debug("POST", "send:", "error", "server"); aws("error", "server"); } } catch (error) { - debug("POST", "error:", error); aws("error", "server"); } } diff --git a/src/sys/selfTag.ts b/src/sys/selfTag.ts index 96f2787..209961d 100644 --- a/src/sys/selfTag.ts +++ b/src/sys/selfTag.ts @@ -28,7 +28,6 @@ export const generateTag = async () => { selfTag = serverHostPort; } else selfTag = mainServerHost; if (initselfTag.httpsURL != selfTag.httpsURL) { - console.log(initselfTag, initselfTag.httpsURL, selfTag.httpsURL); debug("Outbag", "Not matching Server host, port, path and expected server link."); return false; } diff --git a/tests/post.js b/tests/post.js index 7cbe8e5..8bacc9a 100644 --- a/tests/post.js +++ b/tests/post.js @@ -31,7 +31,7 @@ function shallowEqual(object1, object2) { return false; } for (let key of keys1) { - if (object1[key] != object2[key]) { + if (object1[key] != null && object1[key] != object2[key]) { return false; } } @@ -47,7 +47,7 @@ async function postTester(url) { headers: Object.assign({ "Content-Type": "application/json" }, header), - body: JSON.stringify({data}) + body: JSON.stringify({ data }) }); let resp = { diff --git a/tests/tests/post.js b/tests/tests/post.js index c0e9c48..70b3948 100644 --- a/tests/tests/post.js +++ b/tests/tests/post.js @@ -14,21 +14,80 @@ const list = [ server: "localhost:7224", accountKey }, "ok", ""); + await req({}, "signup", { name: name1, server: "localhost:7224", accountKey }, "error", "existence"); + await req({}, "signup", { name: name2, server: "localhost:7224", accountKey }, "ok", ""); + await req({}, "signup", { name: name2, server: "localhost:7224", accountKey }, "error", "config"); + }], ["remote", async (req) => { + let signature = await req({ "authorization": `Digest name=${name1} server=localhost:7224 accountKey=${accountKey}` }, + "createSignature", { + publicKey + }, "ok", null); + let falseSignature = await sign("lol", privateKey); + let token = (await req({}, "requestTempToken", {}, "ok", { token: null })).token; + + await req({ + "authorization": `Bearer ${token}` + }, "remote1", { + name: name1, + server: "localhost:7223", + publicKey, + sign: signature + }, "error", "signature"); + + await req({ + "authorization": `Bearer ${token}` + }, "remote1", { + name: name1, + server: "localhost:7224", + publicKey, + sign: falseSignature + }, "error", "signature"); + + let challenge = await req({ + "authorization": `Bearer ${token}` + }, "remote1", { + name: name1, + server: "localhost:7224", + publicKey, + sign: signature + }, "ok", null); + + await req({ + "authorization": `Bearer ${token}` + }, "remote1", { + name: name1, + server: "localhost:7224", + publicKey, + sign: signature + }, "error", "wrongstate"); + + await req({ + "authorization": `Bearer ${token}` + }, "remote2", { + sign: await sign(challenge + "lol", privateKey) + }, "error", "signature"); + + await req({ + "authorization": `Bearer ${token}` + }, "remote2", { + sign: await sign(challenge, privateKey) + }, "ok", ""); + }] ]; diff --git a/tests/tests/ws.js b/tests/tests/ws.js index f4f8b61..d7a07ad 100644 --- a/tests/tests/ws.js +++ b/tests/tests/ws.js @@ -53,7 +53,7 @@ const list = [ }, "error", "signature"); await req(h2, "remote1", { name: name1, - server: "localhost:7223", + server: "localhost:7224", publicKey, sign: falseSignature }, "error", "signature"); diff --git a/tests/ws.js b/tests/ws.js index 8c2da23..1ce1683 100644 --- a/tests/ws.js +++ b/tests/ws.js @@ -90,7 +90,7 @@ function shallowEqual(object1, object2) { return false; } for (let key of keys1) { - if (object1[key] != object2[key]) { + if (object1[key] != null && object1[key] != object2[key]) { return false; } }