2015-07-20 00:09:47 -07:00
|
|
|
/* jslint node: true */
|
|
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2021-03-15 12:47:57 -07:00
|
|
|
get,
|
|
|
|
|
getByAccessToken,
|
|
|
|
|
delByAccessToken,
|
|
|
|
|
add,
|
|
|
|
|
del,
|
|
|
|
|
getByIdentifier,
|
|
|
|
|
delExpired,
|
|
|
|
|
update,
|
2015-07-20 00:09:47 -07:00
|
|
|
|
|
|
|
|
_clear: clear
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var assert = require('assert'),
|
2019-10-24 11:13:48 -07:00
|
|
|
BoxError = require('./boxerror.js'),
|
|
|
|
|
database = require('./database.js');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2021-03-15 12:47:57 -07:00
|
|
|
var TOKENS_FIELDS = [ 'id', 'accessToken', 'identifier', 'clientId', 'scope', 'expires', 'name', 'lastUsedTime' ].join(',');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2019-02-15 13:57:18 -08:00
|
|
|
function getByAccessToken(accessToken, callback) {
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof accessToken, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
database.query('SELECT ' + TOKENS_FIELDS + ' FROM tokens WHERE accessToken = ? AND expires > ?', [ accessToken, Date.now() ], function (error, result) {
|
2019-10-24 11:13:48 -07:00
|
|
|
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
|
2019-10-24 20:48:38 -07:00
|
|
|
if (result.length === 0) return callback(new BoxError(BoxError.NOT_FOUND, 'Token not found'));
|
2015-07-20 00:09:47 -07:00
|
|
|
|
|
|
|
|
callback(null, result[0]);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-04 14:35:25 +01:00
|
|
|
function delByAccessToken(accessToken, callback) {
|
|
|
|
|
assert.strictEqual(typeof accessToken, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
database.query('DELETE FROM tokens WHERE accessToken = ?', [ accessToken ], 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);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-15 13:57:18 -08:00
|
|
|
function get(id, callback) {
|
|
|
|
|
assert.strictEqual(typeof id, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
database.query('SELECT ' + TOKENS_FIELDS + ' FROM tokens WHERE id = ?', [ id ], function (error, result) {
|
2019-10-24 11:13:48 -07:00
|
|
|
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
|
2019-10-24 20:48:38 -07:00
|
|
|
if (result.length === 0) return callback(new BoxError(BoxError.NOT_FOUND, 'Token not found'));
|
2019-02-15 13:57:18 -08:00
|
|
|
|
|
|
|
|
callback(null, result[0]);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function add(token, callback) {
|
|
|
|
|
assert.strictEqual(typeof token, 'object');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
let { id, accessToken, identifier, clientId, expires, scope, name } = token;
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof accessToken, 'string');
|
|
|
|
|
assert.strictEqual(typeof identifier, 'string');
|
|
|
|
|
assert(typeof clientId === 'string' || clientId === null);
|
|
|
|
|
assert.strictEqual(typeof expires, 'number');
|
|
|
|
|
assert.strictEqual(typeof scope, 'string');
|
2018-08-27 14:50:41 -07:00
|
|
|
assert.strictEqual(typeof name, 'string');
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2019-02-15 13:57:18 -08:00
|
|
|
database.query('INSERT INTO tokens (id, accessToken, identifier, clientId, expires, scope, name) VALUES (?, ?, ?, ?, ?, ?, ?)',
|
|
|
|
|
[ id, accessToken, identifier, clientId, expires, scope, name ], function (error, result) {
|
2019-10-24 11:13:48 -07:00
|
|
|
if (error && error.code === 'ER_DUP_ENTRY') return callback(new BoxError(BoxError.ALREADY_EXISTS));
|
|
|
|
|
if (error || result.affectedRows !== 1) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2018-04-30 22:26:34 -07:00
|
|
|
callback(null);
|
|
|
|
|
});
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
2021-03-15 12:47:57 -07:00
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-15 13:57:18 -08:00
|
|
|
function del(id, callback) {
|
|
|
|
|
assert.strictEqual(typeof id, 'string');
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2019-02-15 13:57:18 -08:00
|
|
|
database.query('DELETE FROM tokens WHERE id = ?', [ id ], function (error, result) {
|
2019-10-24 11:13:48 -07:00
|
|
|
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
|
2019-10-24 20:48:38 -07:00
|
|
|
if (result.affectedRows !== 1) return callback(new BoxError(BoxError.NOT_FOUND, 'Token not found'));
|
2015-07-20 00:09:47 -07:00
|
|
|
|
|
|
|
|
callback(error);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getByIdentifier(identifier, callback) {
|
|
|
|
|
assert.strictEqual(typeof identifier, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
database.query('SELECT ' + TOKENS_FIELDS + ' FROM tokens WHERE identifier = ?', [ identifier ], function (error, results) {
|
2019-10-24 11:13:48 -07:00
|
|
|
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
|
2015-07-20 00:09:47 -07:00
|
|
|
|
|
|
|
|
callback(null, results);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function delExpired(callback) {
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
database.query('DELETE FROM tokens WHERE expires <= ?', [ Date.now() ], function (error, result) {
|
2019-10-24 11:13:48 -07:00
|
|
|
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
|
2015-07-20 00:09:47 -07:00
|
|
|
return callback(null, result.affectedRows);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function clear(callback) {
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
database.query('DELETE FROM tokens', function (error) {
|
2019-10-24 11:13:48 -07:00
|
|
|
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
|
2015-07-20 00:09:47 -07:00
|
|
|
|
|
|
|
|
return callback(null);
|
|
|
|
|
});
|
|
|
|
|
}
|