2018-01-23 20:06:30 +00:00
|
|
|
const randomstring = require('randomstring')
|
Updates (very important to read)
Client-side CSS & JS files will now be processed with Gulp.
Gulp tasks are configured in gulpfile.js file.
CSS files will be optimized with postcss-preset-env, which will
auto-add vendor prefixes and convert any parts necessary for browsers
compatibility.
Afterwards they will be minified with cssnano.
JS files will be optimized with bublé,
likewise for browsers compatibility.
Afterwards they will be minified with terser.
Unprocessed CSS & JS files will now be located at src directory, while
the processed results will be located at dist directory.
Due to bublé, the JS files should now be compatible up to IE 11
at the minimum.
Previously the safe would not work in IE 11 due to extensive usage of
template literals.
Due to that as well, JS files in src directory will now extensively use
arrow functions for my personal comfort (as they will be converted too).
The server will use the processed files at dist directory by default.
If you want to rebuild the files by your own, you can run "yarn build".
Gulp is a development dependency, so make sure you have installed all
development dependencies (e.i. NOT using "yarn install --production").
---
yarn lint -> gulp lint
yarn build -> gulp default
yarn watch -> gulp watch
yarn develop -> env NODE_ENV=development yarn watch
---
Fixed not being able to demote staff into normal users.
/api/token/verify will no longer respond with 401 HTTP error code,
unless an error occurred (which will be 500 HTTP error code).
Fixed /nojs route not displaying file's original name when a duplicate
is found on the server.
Removed is-breeze CSS class name, in favor of Bulma's is-info.
Removed custom styling from auth page, in favor of global styling.
Removed all usage of style HTML attribute in favor of CSS classes.
Renamed js/s/ to js/misc/.
Use loading spinners on dashboard's sidebar menus.
Disable all other sidebar menus when something is loading.
Changed title HTML attribute of disabled control buttons in
uploads & users list.
Hid checkboxes and WIP controls from users list.
Better error messages handling.
Especially homepage will now support CF's HTTP error codes.
Updated various icons.
Also, added fontello config file at public/libs/fontello/config.json.
This should let you edit them more easily with fontello.
Use Gatsby icon for my blog's link in homepage's footer.
A bunch of other improvements here & there.
2019-09-15 06:20:11 +00:00
|
|
|
const perms = require('./permissionController')
|
2018-04-13 16:20:57 +00:00
|
|
|
const utils = require('./utilsController')
|
2021-01-08 03:56:09 +00:00
|
|
|
const ClientError = require('./utils/ClientError')
|
|
|
|
const ServerError = require('./utils/ServerError')
|
2022-07-30 01:37:57 +00:00
|
|
|
const logger = require('./../logger')
|
2017-01-17 19:54:25 +00:00
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
const self = {
|
|
|
|
tokenLength: 64,
|
|
|
|
tokenMaxTries: 3,
|
2022-07-30 01:37:57 +00:00
|
|
|
|
|
|
|
onHold: new Set() // temporarily held random tokens
|
2019-09-08 01:56:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-30 01:37:57 +00:00
|
|
|
self.getUniqueToken = async res => {
|
2019-09-08 01:56:29 +00:00
|
|
|
for (let i = 0; i < self.tokenMaxTries; i++) {
|
|
|
|
const token = randomstring.generate(self.tokenLength)
|
2022-07-30 01:37:57 +00:00
|
|
|
|
|
|
|
if (self.onHold.has(token)) {
|
|
|
|
logger.debug(`Token ${utils.mask(token)} is currently held by another request (${i + 1}/${utils.idMaxTries}).`)
|
|
|
|
continue
|
|
|
|
}
|
2019-06-18 21:04:14 +00:00
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
// Put token on-hold (wait for it to be inserted to DB)
|
|
|
|
self.onHold.add(token)
|
2017-01-17 19:54:25 +00:00
|
|
|
|
2022-06-03 21:21:56 +00:00
|
|
|
const user = await utils.db.table('users')
|
2019-09-08 01:56:29 +00:00
|
|
|
.where('token', token)
|
|
|
|
.select('id')
|
|
|
|
.first()
|
|
|
|
if (user) {
|
|
|
|
self.onHold.delete(token)
|
2022-07-30 01:37:57 +00:00
|
|
|
logger.debug(`User with token ${utils.mask(token)} already exists (${i + 1}/${utils.idMaxTries}).`)
|
2019-09-08 01:56:29 +00:00
|
|
|
continue
|
2019-06-18 21:04:14 +00:00
|
|
|
}
|
2019-09-08 01:56:29 +00:00
|
|
|
|
2022-07-30 01:37:57 +00:00
|
|
|
// Unhold token once the Response has been sent
|
|
|
|
if (res) {
|
|
|
|
// Keep in an array for future-proofing
|
|
|
|
// if a single Request needs to generate multiple tokens
|
|
|
|
if (!res.locals.tokens) {
|
|
|
|
res.locals.tokens = []
|
|
|
|
res.once('finish', () => { self.unholdTokens(res) })
|
|
|
|
}
|
|
|
|
res.locals.tokens.push(token)
|
|
|
|
}
|
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
return token
|
|
|
|
}
|
|
|
|
|
2022-07-30 01:37:57 +00:00
|
|
|
throw new ServerError('Failed to allocate a unique token. Try again?')
|
|
|
|
}
|
|
|
|
|
|
|
|
self.unholdTokens = res => {
|
|
|
|
if (!res.locals.tokens) return
|
|
|
|
|
|
|
|
for (const token of res.locals.tokens) {
|
|
|
|
self.onHold.delete(token)
|
|
|
|
logger.debug(`Unheld token ${utils.mask(token)}.`)
|
|
|
|
}
|
|
|
|
|
|
|
|
delete res.locals.tokens
|
2019-06-18 21:04:14 +00:00
|
|
|
}
|
|
|
|
|
2022-07-10 12:46:25 +00:00
|
|
|
self.verify = async (req, res) => {
|
2022-07-24 23:12:55 +00:00
|
|
|
utils.assertRequestType(req, 'application/json')
|
2022-07-22 01:40:40 +00:00
|
|
|
|
2022-07-10 12:46:25 +00:00
|
|
|
// Parse POST body
|
|
|
|
req.body = await req.json()
|
2017-01-17 19:54:25 +00:00
|
|
|
|
2022-07-10 12:46:25 +00:00
|
|
|
const token = typeof req.body.token === 'string'
|
|
|
|
? req.body.token.trim()
|
|
|
|
: ''
|
2018-10-09 19:52:41 +00:00
|
|
|
|
2022-07-10 12:46:25 +00:00
|
|
|
if (!token) throw new ClientError('No token provided.', { statusCode: 403 })
|
2019-09-08 01:56:29 +00:00
|
|
|
|
2022-07-10 12:46:25 +00:00
|
|
|
const user = await utils.db.table('users')
|
|
|
|
.where('token', token)
|
|
|
|
.select('username', 'permission')
|
|
|
|
.first()
|
2019-09-08 01:56:29 +00:00
|
|
|
|
2022-07-10 12:46:25 +00:00
|
|
|
if (!user) {
|
|
|
|
throw new ClientError('Invalid token.', { statusCode: 403, code: 10001 })
|
|
|
|
}
|
2022-05-06 19:17:31 +00:00
|
|
|
|
2022-07-10 12:46:25 +00:00
|
|
|
const obj = {
|
|
|
|
success: true,
|
|
|
|
username: user.username,
|
|
|
|
permissions: perms.mapPermissions(user)
|
|
|
|
}
|
2021-01-08 03:56:09 +00:00
|
|
|
|
2022-07-10 12:46:25 +00:00
|
|
|
const group = perms.group(user)
|
|
|
|
if (group) {
|
|
|
|
obj.group = group
|
|
|
|
if (utils.retentions.enabled) {
|
|
|
|
obj.retentionPeriods = utils.retentions.periods[group]
|
|
|
|
obj.defaultRetentionPeriod = utils.retentions.default[group]
|
2021-01-08 03:56:09 +00:00
|
|
|
}
|
2022-07-10 12:46:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (utils.clientVersion) {
|
|
|
|
obj.version = utils.clientVersion
|
|
|
|
}
|
2021-01-08 03:56:09 +00:00
|
|
|
|
2022-07-10 12:46:25 +00:00
|
|
|
return res.json(obj)
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
2017-01-17 19:54:25 +00:00
|
|
|
|
2022-07-10 12:46:25 +00:00
|
|
|
self.list = async (req, res) => {
|
|
|
|
const user = await utils.authorize(req)
|
|
|
|
return res.json({ success: true, token: user.token })
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
2017-01-17 19:54:25 +00:00
|
|
|
|
2022-07-10 12:46:25 +00:00
|
|
|
self.change = async (req, res) => {
|
|
|
|
const user = await utils.authorize(req, 'token')
|
2021-01-08 03:56:09 +00:00
|
|
|
|
2022-07-30 01:37:57 +00:00
|
|
|
const newToken = await self.getUniqueToken(res)
|
2021-01-08 03:56:09 +00:00
|
|
|
|
2022-07-10 12:46:25 +00:00
|
|
|
await utils.db.table('users')
|
|
|
|
.where('token', user.token)
|
|
|
|
.update({
|
|
|
|
token: newToken,
|
|
|
|
timestamp: Math.floor(Date.now() / 1000)
|
|
|
|
})
|
2019-09-08 01:56:29 +00:00
|
|
|
|
2022-07-10 12:46:25 +00:00
|
|
|
return res.json({ success: true, token: newToken })
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
2017-01-18 07:51:42 +00:00
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
module.exports = self
|