tokens: add lastUsedTime

This commit is contained in:
Girish Ramakrishnan
2021-03-15 12:47:57 -07:00
parent 750f313c6a
commit 176388111c
15 changed files with 222 additions and 81 deletions
+30 -8
View File
@@ -3,13 +3,14 @@
'use strict';
exports = module.exports = {
get: get,
getByAccessToken: getByAccessToken,
delByAccessToken: delByAccessToken,
add: add,
del: del,
getByIdentifier: getByIdentifier,
delExpired: delExpired,
get,
getByAccessToken,
delByAccessToken,
add,
del,
getByIdentifier,
delExpired,
update,
_clear: clear
};
@@ -18,7 +19,7 @@ var assert = require('assert'),
BoxError = require('./boxerror.js'),
database = require('./database.js');
var TOKENS_FIELDS = [ 'id', 'accessToken', 'identifier', 'clientId', 'scope', 'expires', 'name' ].join(',');
var TOKENS_FIELDS = [ 'id', 'accessToken', 'identifier', 'clientId', 'scope', 'expires', 'name', 'lastUsedTime' ].join(',');
function getByAccessToken(accessToken, callback) {
assert.strictEqual(typeof accessToken, 'string');
@@ -79,6 +80,27 @@ function add(token, callback) {
});
}
function update(id, values, callback) {
assert.strictEqual(typeof id, 'string');
assert.strictEqual(typeof values, 'object');
assert.strictEqual(typeof callback, 'function');
let args = [ ];
let fields = [ ];
for (let k in values) {
fields.push(k + ' = ?');
args.push(values[k]);
}
args.push(id);
database.query('UPDATE tokens SET ' + fields.join(', ') + ' WHERE id = ?', args, function (error, result) {
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
if (result.affectedRows !== 1) return callback(new BoxError(BoxError.NOT_FOUND, 'Token not found'));
return callback(null);
});
}
function del(id, callback) {
assert.strictEqual(typeof id, 'string');
assert.strictEqual(typeof callback, 'function');