stackpress
Version:
Incept is a content management framework.
69 lines (68 loc) • 2.1 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.authorize = authorize;
exports.unauthorized = unauthorized;
exports.validData = validData;
const helpers_1 = require("@stackpress/ingest/helpers");
const Exception_js_1 = __importDefault(require("../Exception.js"));
function authorize(req, res) {
const authorization = req.headers.get('authorization');
if (!authorization) {
unauthorized(res);
return false;
}
const [, token] = authorization.split(' ');
if (!token || !token.trim().length) {
unauthorized(res);
return false;
}
const [id, secret] = token.split(':');
if (!id || !id.trim().length) {
unauthorized(res);
return false;
}
if (req.method.toUpperCase() !== 'GET'
&& (!secret || !(secret === null || secret === void 0 ? void 0 : secret.trim().length))) {
unauthorized(res);
return false;
}
return {
token: token.trim(),
id: id.trim(),
secret: (secret === null || secret === void 0 ? void 0 : secret.trim()) || ''
};
}
;
function unauthorized(res) {
res.setError(Exception_js_1.default
.for('Unauthorized')
.withCode(401)
.toResponse());
}
;
function validData(assert, data) {
for (const [key, value] of Object.entries(assert)) {
if (typeof data[key] === 'undefined')
return false;
if (Array.isArray(value)) {
if (!Array.isArray(data[key]))
return false;
if (!value.every(item => data[key].includes(item)))
return false;
}
else if ((0, helpers_1.isObject)(value)) {
if (!(0, helpers_1.isObject)(data[key]))
return false;
if (!validData(value, data[key]))
return false;
}
else if (data[key] !== value) {
return false;
}
}
return true;
}
;