2024-02-22 17:08:09 +07:00
|
|
|
"use strict";
|
2024-02-18 12:56:00 +07:00
|
|
|
const { validateEvent, verifyEvent } = require("nostr-tools");
|
2024-02-19 19:38:31 +07:00
|
|
|
const { authorized_keys, private_keys } = require(process.env.BOSTR_CONFIG_PATH || "./config");
|
2023-11-16 21:53:58 +07:00
|
|
|
|
2023-11-20 21:21:22 +07:00
|
|
|
module.exports = (authKey, data, ws, req) => {
|
2024-02-18 12:56:00 +07:00
|
|
|
if (!validateEvent(data) || !verifyEvent(data)) {
|
2023-11-16 21:53:58 +07:00
|
|
|
ws.send(JSON.stringify(["NOTICE", "error: invalid challenge response."]));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-03-27 23:15:16 +07:00
|
|
|
if ((authorized_keys?.length || Object.keys(private_keys).length) && !authorized_keys?.includes(data.pubkey) && !(private_keys && private_keys[data.pubkey])) {
|
2023-11-16 21:53:58 +07:00
|
|
|
ws.send(JSON.stringify(["OK", data.id, false, "unauthorized."]));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.kind != 22242) {
|
|
|
|
ws.send(JSON.stringify(["OK", data.id, false, "not kind 22242."]));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const tags = new Map(data.tags);
|
2024-02-17 19:35:33 +07:00
|
|
|
|
2023-11-16 21:53:58 +07:00
|
|
|
if (!tags.get("relay").includes(req.headers.host)) {
|
|
|
|
ws.send(JSON.stringify(["OK", data.id, false, "unmatched relay url."]));
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (tags.get("challenge") !== authKey) {
|
|
|
|
ws.send(JSON.stringify(["OK", data.id, false, "unmatched challenge string."]));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-11-20 21:21:22 +07:00
|
|
|
ws.send(JSON.stringify(["OK", data.id, true, `Hello ${data.pubkey}`]));
|
2023-11-16 21:53:58 +07:00
|
|
|
return true;
|
|
|
|
}
|