Files
cloudron-box/src/tokens.js
T

59 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-02-06 16:57:33 +01:00
'use strict';
exports = module.exports = {
add: add,
2020-02-06 16:57:33 +01:00
// token client ids. we categorize them so we can have different restrictions based on the client
ID_WEBADMIN: 'cid-webadmin', // dashboard oauth
ID_SDK: 'cid-sdk', // created by user via dashboard
ID_CLI: 'cid-cli' // created via cli tool
};
let assert = require('assert'),
BoxError = require('./boxerror.js'),
hat = require('./hat.js'),
uuid = require('uuid'),
tokendb = require('./tokendb.js');
function validateTokenName(name) {
assert.strictEqual(typeof name, 'string');
if (name.length > 64) return new BoxError(BoxError.BAD_FIELD, 'name too long', { field: 'name' });
return null;
}
function add(clientId, userId, expiresAt, options, callback) {
2020-02-06 16:57:33 +01:00
assert.strictEqual(typeof clientId, 'string');
assert.strictEqual(typeof userId, 'string');
assert.strictEqual(typeof expiresAt, 'number');
assert.strictEqual(typeof options, 'object');
assert.strictEqual(typeof callback, 'function');
const name = options.name || '';
let error = validateTokenName(name);
if (error) return callback(error);
const token = {
id: 'tid-' + uuid.v4(),
accessToken: hat(8 * 32),
identifier: userId,
clientId: clientId,
expires: expiresAt,
scope: 'unused',
name: name
};
tokendb.add(token, function (error) {
if (error) return callback(error);
callback(null, {
accessToken: token.accessToken,
tokenScopes: 'unused',
identifier: userId,
clientId: clientId,
expires: expiresAt
});
});
}