33 lines
762 B
JavaScript
33 lines
762 B
JavaScript
const path = require('path');
|
|
const { authNodesDir } = require('./paths');
|
|
const { readJsonIfExists, writeJson } = require('./fs');
|
|
|
|
function nodeTokenPath(nodeId) {
|
|
return path.join(authNodesDir, `${nodeId}.json`);
|
|
}
|
|
|
|
async function getNodeToken(nodeId) {
|
|
return readJsonIfExists(nodeTokenPath(nodeId));
|
|
}
|
|
|
|
async function verifyNodeToken(nodeId, token) {
|
|
const record = await getNodeToken(nodeId);
|
|
return Boolean(record && record.node_id === nodeId && record.token === token);
|
|
}
|
|
|
|
async function saveNodeToken(nodeId, token) {
|
|
await writeJson(nodeTokenPath(nodeId), {
|
|
node_id: nodeId,
|
|
token,
|
|
schema_version: 'v1',
|
|
updated_at: new Date().toISOString(),
|
|
});
|
|
}
|
|
|
|
module.exports = {
|
|
getNodeToken,
|
|
verifyNodeToken,
|
|
saveNodeToken,
|
|
};
|
|
|