Initial commit
This commit is contained in:
32
shared/auth.js
Normal file
32
shared/auth.js
Normal file
@@ -0,0 +1,32 @@
|
||||
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,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user