Files
cloudron-box/src/routes/clients.js

125 lines
4.4 KiB
JavaScript
Raw Normal View History

'use strict';
exports = module.exports = {
add: add,
get: get,
del: del,
getAll: getAll,
2018-04-28 17:55:17 -07:00
addToken: addToken,
getTokens: getTokens,
delTokens: delTokens,
2016-06-07 15:34:27 +02:00
delToken: delToken
};
var assert = require('assert'),
2019-10-22 21:16:00 -07:00
BoxError = require('../boxerror.js'),
clients = require('../clients.js'),
2016-08-01 10:14:45 +02:00
constants = require('../constants.js'),
HttpError = require('connect-lastmile').HttpError,
2015-10-15 16:31:45 -07:00
HttpSuccess = require('connect-lastmile').HttpSuccess,
validUrl = require('valid-url');
function add(req, res, next) {
var data = req.body;
if (!data) return next(new HttpError(400, 'Cannot parse data field'));
if (typeof data.appId !== 'string' || !data.appId) return next(new HttpError(400, 'appId is required'));
if (typeof data.redirectURI !== 'string' || !data.redirectURI) return next(new HttpError(400, 'redirectURI is required'));
if (typeof data.scope !== 'string' || !data.scope) return next(new HttpError(400, 'scope is required'));
if (!validUrl.isWebUri(data.redirectURI)) return next(new HttpError(400, 'redirectURI must be a valid uri'));
2016-06-03 15:05:00 +02:00
clients.add(data.appId, clients.TYPE_EXTERNAL, data.redirectURI, data.scope, function (error, result) {
if (error) return next(BoxError.toHttpError(error));
2019-10-22 21:16:00 -07:00
next(new HttpSuccess(201, result));
});
}
function get(req, res, next) {
assert.strictEqual(typeof req.params.clientId, 'string');
clients.get(req.params.clientId, function (error, result) {
if (error) return next(BoxError.toHttpError(error));
2019-10-22 21:16:00 -07:00
next(new HttpSuccess(200, result));
});
}
function del(req, res, next) {
assert.strictEqual(typeof req.params.clientId, 'string');
clients.get(req.params.clientId, function (error, result) {
if (error) return next(BoxError.toHttpError(error));
// we do not allow to use the REST API to delete addon clients
if (result.type !== clients.TYPE_EXTERNAL) return next(new HttpError(405, 'Deleting app addon clients is not allowed.'));
clients.del(req.params.clientId, function (error, result) {
if (error) return next(BoxError.toHttpError(error));
2019-10-22 21:16:00 -07:00
next(new HttpSuccess(204, result));
});
});
}
function getAll(req, res, next) {
clients.getAll(function (error, result) {
if (error) return next(BoxError.toHttpError(error));
2019-10-22 21:16:00 -07:00
next(new HttpSuccess(200, { clients: result }));
});
}
2018-04-28 17:55:17 -07:00
function addToken(req, res, next) {
assert.strictEqual(typeof req.params.clientId, 'string');
assert.strictEqual(typeof req.user, 'object');
2018-04-28 20:41:38 -07:00
assert.strictEqual(typeof req.body, 'object');
2018-04-28 20:41:38 -07:00
var data = req.body;
var expiresAt = data.expiresAt ? parseInt(data.expiresAt, 10) : Date.now() + constants.DEFAULT_TOKEN_EXPIRATION;
if (isNaN(expiresAt) || expiresAt <= Date.now()) return next(new HttpError(400, 'expiresAt must be a timestamp in the future'));
2018-08-27 14:50:41 -07:00
if ('name' in req.body && typeof req.body.name !== 'string') return next(new HttpError(400, 'name must be a string'));
2018-08-27 14:50:41 -07:00
clients.addTokenByUserId(req.params.clientId, req.user.id, expiresAt, { name: req.body.name || '' }, function (error, result) {
if (error) return next(BoxError.toHttpError(error));
2019-10-22 21:16:00 -07:00
next(new HttpSuccess(201, { token: result }));
});
}
2018-04-28 17:55:17 -07:00
function getTokens(req, res, next) {
assert.strictEqual(typeof req.params.clientId, 'string');
assert.strictEqual(typeof req.user, 'object');
2018-04-28 17:55:17 -07:00
clients.getTokensByUserId(req.params.clientId, req.user.id, function (error, result) {
if (error) return next(BoxError.toHttpError(error));
result = result.map(clients.removeTokenPrivateFields);
next(new HttpSuccess(200, { tokens: result }));
});
}
2018-04-28 17:55:17 -07:00
function delTokens(req, res, next) {
assert.strictEqual(typeof req.params.clientId, 'string');
assert.strictEqual(typeof req.user, 'object');
2018-04-28 17:55:17 -07:00
clients.delTokensByUserId(req.params.clientId, req.user.id, function (error) {
if (error) return next(BoxError.toHttpError(error));
2019-10-22 21:16:00 -07:00
next(new HttpSuccess(204));
});
}
2016-06-07 15:34:27 +02:00
function delToken(req, res, next) {
assert.strictEqual(typeof req.params.clientId, 'string');
assert.strictEqual(typeof req.params.tokenId, 'string');
assert.strictEqual(typeof req.user, 'object');
clients.delToken(req.params.clientId, req.params.tokenId, function (error) {
if (error) return next(BoxError.toHttpError(error));
2016-06-07 15:34:27 +02:00
next(new HttpSuccess(204));
});
}