2022-08-13 21:52:45 +00:00
|
|
|
import { HttpError } from 'flaska'
|
2021-10-09 00:11:52 +00:00
|
|
|
import decode from '../jwt/decode.mjs'
|
2021-07-03 13:56:53 +00:00
|
|
|
import config from '../config.mjs'
|
2017-12-10 09:45:38 +00:00
|
|
|
|
2021-10-09 00:11:52 +00:00
|
|
|
export function verifyToken(ctx) {
|
2021-10-11 00:21:57 +00:00
|
|
|
let token = ctx.query.get('token')
|
|
|
|
if (!token) {
|
2022-08-13 21:52:45 +00:00
|
|
|
throw new HttpError(422, 'Token is missing in query')
|
2017-12-10 09:45:38 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 14:47:51 +00:00
|
|
|
let org = config.get('sites')
|
|
|
|
let sites = {}
|
|
|
|
for (let key in org) {
|
|
|
|
if (org.hasOwnProperty(key)) {
|
|
|
|
sites[key] = org[key].keys
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-11 00:21:57 +00:00
|
|
|
try {
|
2022-01-05 14:47:51 +00:00
|
|
|
let decoded = decode(token, sites, [])
|
2021-10-11 00:21:57 +00:00
|
|
|
return decoded.iss
|
|
|
|
} catch (err) {
|
|
|
|
ctx.log.error(err, 'Error decoding token: ' + token)
|
2022-08-13 21:52:45 +00:00
|
|
|
throw new HttpError(422, 'Token was invalid')
|
2021-10-11 00:21:57 +00:00
|
|
|
}
|
2017-12-10 09:45:38 +00:00
|
|
|
}
|
2022-01-05 14:47:51 +00:00
|
|
|
|
2023-11-15 13:21:22 +00:00
|
|
|
export function hasCors(site) {
|
|
|
|
let sites = config.get('sites')
|
|
|
|
return sites[site]?.cors === true
|
|
|
|
}
|
|
|
|
|
2023-11-15 08:51:48 +00:00
|
|
|
export function verifyCorsEnabled(ctx) {
|
|
|
|
let site
|
|
|
|
try {
|
|
|
|
site = verifyToken(ctx)
|
|
|
|
} catch (err) {
|
|
|
|
throw new HttpError(404)
|
|
|
|
}
|
2023-11-15 13:21:22 +00:00
|
|
|
|
|
|
|
if (!hasCors(site)) {
|
2023-11-15 08:51:48 +00:00
|
|
|
throw new HttpError(404)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 14:47:51 +00:00
|
|
|
export function throwIfNotPublic(site) {
|
|
|
|
let sites = config.get('sites')
|
|
|
|
if (!sites[site] || sites[site].public !== true) {
|
2022-08-13 21:52:45 +00:00
|
|
|
throw new HttpError(404, `Requested site ${site} did not exist`)
|
2022-01-05 14:47:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-06 09:51:43 +00:00
|
|
|
const validObjectOperations = [
|
|
|
|
'resize',
|
|
|
|
'extend',
|
|
|
|
'flatten',
|
|
|
|
]
|
|
|
|
|
|
|
|
const validNumberOperations = [
|
|
|
|
'blur',
|
|
|
|
'trim',
|
|
|
|
]
|
|
|
|
|
2022-01-05 14:47:51 +00:00
|
|
|
export function verifyBody(ctx) {
|
|
|
|
let keys = Object.keys(ctx.req.body)
|
|
|
|
|
|
|
|
for (let key of keys) {
|
2022-01-06 09:01:10 +00:00
|
|
|
if (key === 'filename' || key === 'path') {
|
2022-08-13 21:52:45 +00:00
|
|
|
throw new HttpError(422, 'Body item with name filename or path is not allowed')
|
2022-01-05 14:47:51 +00:00
|
|
|
}
|
|
|
|
let item = ctx.req.body[key]
|
|
|
|
|
2022-10-17 09:45:28 +00:00
|
|
|
if (item == null) continue
|
|
|
|
|
2022-01-05 14:47:51 +00:00
|
|
|
if (typeof(item) !== 'object'
|
|
|
|
|| Array.isArray(item)) {
|
2022-08-13 21:52:45 +00:00
|
|
|
throw new HttpError(422, `Body item ${key} was not valid`)
|
2022-01-05 14:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof(item.format) !== 'string'
|
|
|
|
|| !item.format
|
2022-01-06 09:51:43 +00:00
|
|
|
|| validObjectOperations.includes(item.format)
|
2022-01-05 14:47:51 +00:00
|
|
|
|| item.format === 'out') {
|
2022-08-13 21:52:45 +00:00
|
|
|
throw new HttpError(422, `Body item ${key} missing valid format`)
|
2022-01-05 14:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof(item[item.format]) !== 'object'
|
|
|
|
|| !item[item.format]
|
|
|
|
|| Array.isArray(item[item.format])) {
|
2022-08-13 21:52:45 +00:00
|
|
|
throw new HttpError(422, `Body item ${key} options for format ${item.format} was not valid`)
|
2022-01-05 14:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (item.out != null) {
|
|
|
|
if (typeof(item.out) !== 'string'
|
|
|
|
|| (item.out !== '' && item.out !== 'file' && item.out !== 'base64')
|
|
|
|
) {
|
2022-08-13 21:52:45 +00:00
|
|
|
throw new HttpError(422, `Body item ${key} key out was invalid`)
|
2022-01-05 14:47:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-06 09:51:43 +00:00
|
|
|
for (let operation of validObjectOperations) {
|
|
|
|
if (item[operation] != null) {
|
|
|
|
if (typeof(item[operation]) !== 'object'
|
|
|
|
|| Array.isArray(item[operation])) {
|
2022-08-13 21:52:45 +00:00
|
|
|
throw new HttpError(422, `Body item ${key} key ${operation} was invalid`)
|
2022-01-06 09:51:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let operation of validNumberOperations) {
|
|
|
|
if (item[operation] != null) {
|
|
|
|
if (typeof(item[operation]) !== 'number') {
|
2022-08-13 21:52:45 +00:00
|
|
|
throw new HttpError(422, `Body item ${key} key ${operation} was invalid`)
|
2022-01-06 09:51:43 +00:00
|
|
|
}
|
2022-01-05 14:47:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|