You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/api/utils.js

97 lines
2.5 KiB
JavaScript

'use strict';
const db = require('../database');
const user = require('../user');
const srcUtils = require('../utils');
const utils = module.exports;
// internal token management utilities only
utils.tokens = {};
utils.tokens.list = async () => {
// Validation handled at higher level
const tokens = await db.getSortedSetRange(`tokens:createtime`, 0, -1);
return await utils.tokens.get(tokens);
};
utils.tokens.get = async (tokens) => {
// Validation handled at higher level
if (!tokens) {
throw new Error('[[error:invalid-data]]');
}
let singular = false;
if (!Array.isArray(tokens)) {
tokens = [tokens];
singular = true;
}
const [tokenObjs, lastSeen] = await Promise.all([
db.getObjects(tokens.map(t => `token:${t}`)),
utils.tokens.getLastSeen(tokens),
]);
tokenObjs.forEach((tokenObj, idx) => {
tokenObj.token = tokens[idx];
tokenObj.lastSeen = lastSeen[idx];
tokenObj.lastSeenISO = new Date(lastSeen[idx]).toISOString();
tokenObj.timestampISO = new Date(parseInt(tokenObj.timestamp, 10)).toISOString();
});
return singular ? tokenObjs[0] : tokenObjs;
};
utils.tokens.generate = async ({ uid, description }) => {
if (parseInt(uid, 10) !== 0) {
const uidExists = await user.exists(uid);
if (!uidExists) {
throw new Error('[[error:no-user]]');
}
}
const token = srcUtils.generateUUID();
const timestamp = Date.now();
return utils.tokens.add({ token, uid, description, timestamp });
};
utils.tokens.add = async ({ token, uid, description = '', timestamp = Date.now() }) => {
if (!token || uid === undefined) {
throw new Error('[[error:invalid-data]]');
}
await Promise.all([
db.setObject(`token:${token}`, { uid, description, timestamp }),
db.sortedSetAdd(`tokens:createtime`, timestamp, token),
db.sortedSetAdd(`tokens:uid`, uid, token),
]);
return token;
};
utils.tokens.update = async (token, { uid, description }) => {
await Promise.all([
db.setObject(`token:${token}`, { uid, description }),
db.sortedSetAdd(`tokens:uid`, uid, token),
]);
return await utils.tokens.get(token);
};
utils.tokens.delete = async (token) => {
await Promise.all([
db.delete(`token:${token}`),
db.sortedSetRemove(`tokens:createtime`, token),
db.sortedSetRemove(`tokens:uid`, token),
db.sortedSetRemove(`tokens:lastSeen`, token),
]);
};
utils.tokens.log = async (token) => {
await db.sortedSetAdd('tokens:lastSeen', Date.now(), token);
};
utils.tokens.getLastSeen = async tokens => await db.sortedSetScores('tokens:lastSeen', tokens);