tokens: async'ify

This commit is contained in:
Girish Ramakrishnan
2021-06-04 09:28:40 -07:00
parent 593038907c
commit 7bee7b9ef8
16 changed files with 517 additions and 727 deletions

View File

@@ -4,31 +4,28 @@ exports = module.exports = {
verifyToken
};
var assert = require('assert'),
const assert = require('assert'),
BoxError = require('./boxerror.js'),
debug = require('debug')('box:accesscontrol'),
safe = require('safetydance'),
tokens = require('./tokens.js'),
users = require('./users.js');
users = require('./users.js'),
util = require('util');
const NOOP_CALLBACK = function (error) { if (error) debug(error); };
const userGet = util.promisify(users.get);
function verifyToken(accessToken, callback) {
async function verifyToken(accessToken) {
assert.strictEqual(typeof accessToken, 'string');
assert.strictEqual(typeof callback, 'function');
tokens.getByAccessToken(accessToken, function (error, token) {
if (error && error.reason === BoxError.NOT_FOUND) return callback(new BoxError(BoxError.INVALID_CREDENTIALS));
if (error) return callback(error);
const token = await tokens.getByAccessToken(accessToken);
if (!token) throw new BoxError(BoxError.INVALID_CREDENTIALS);
users.get(token.identifier, function (error, user) {
if (error && error.reason === BoxError.NOT_FOUND) return callback(new BoxError(BoxError.INVALID_CREDENTIALS));
if (error) return callback(error);
const [error, user] = await safe(userGet(token.identifier));
if (error && error.reason === BoxError.NOT_FOUND) throw new BoxError(BoxError.INVALID_CREDENTIALS);
if (error) throw error;
if (!user.active) return callback(new BoxError(BoxError.INVALID_CREDENTIALS));
if (!user.active) throw new BoxError(BoxError.INVALID_CREDENTIALS);
tokens.update(token.id, { lastUsedTime: new Date() }, NOOP_CALLBACK);
await safe(tokens.update(token.id, { lastUsedTime: new Date() })); // ignore any error
callback(null, user);
});
});
return user;
}