2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
2020-12-15 05:23:54 -05:00
|
|
|
node/handle-callback-err,
|
2019-05-29 05:21:06 -04:00
|
|
|
max-len,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
const Settings = require('settings-sharelatex')
|
|
|
|
const crypto = require('crypto')
|
|
|
|
const logger = require('logger-sharelatex')
|
2020-10-01 04:30:26 -04:00
|
|
|
const { db } = require('../../infrastructure/mongodb')
|
2019-05-29 05:21:06 -04:00
|
|
|
const Errors = require('../Errors/Errors')
|
|
|
|
|
|
|
|
const ONE_HOUR_IN_S = 60 * 60
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getNewToken(use, data, options, callback) {
|
|
|
|
// options is optional
|
|
|
|
if (options == null) {
|
|
|
|
options = {}
|
|
|
|
}
|
|
|
|
if (callback == null) {
|
2021-04-14 09:17:21 -04:00
|
|
|
callback = function (error, data) {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
if (typeof options === 'function') {
|
|
|
|
callback = options
|
|
|
|
options = {}
|
|
|
|
}
|
|
|
|
const expiresIn = options.expiresIn || ONE_HOUR_IN_S
|
|
|
|
const createdAt = new Date()
|
|
|
|
const expiresAt = new Date(createdAt.getTime() + expiresIn * 1000)
|
|
|
|
const token = crypto.randomBytes(32).toString('hex')
|
2020-10-01 04:30:26 -04:00
|
|
|
return db.tokens.insertOne(
|
2019-05-29 05:21:06 -04:00
|
|
|
{
|
|
|
|
use,
|
|
|
|
token,
|
|
|
|
data,
|
|
|
|
createdAt,
|
|
|
|
expiresAt
|
|
|
|
},
|
2021-04-14 09:17:21 -04:00
|
|
|
function (error) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
return callback(null, token)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
getValueFromTokenAndExpire(use, token, callback) {
|
|
|
|
if (callback == null) {
|
2021-04-14 09:17:21 -04:00
|
|
|
callback = function (error, data) {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
const now = new Date()
|
2020-10-01 04:30:26 -04:00
|
|
|
return db.tokens.findOneAndUpdate(
|
2019-05-29 05:21:06 -04:00
|
|
|
{
|
2020-10-01 04:30:26 -04:00
|
|
|
use,
|
|
|
|
token,
|
|
|
|
expiresAt: { $gt: now },
|
|
|
|
usedAt: { $exists: false }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$set: {
|
|
|
|
usedAt: now
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
},
|
2021-04-14 09:17:21 -04:00
|
|
|
function (error, result) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
2020-10-01 04:30:26 -04:00
|
|
|
const token = result.value
|
2019-05-29 05:21:06 -04:00
|
|
|
if (token == null) {
|
|
|
|
return callback(new Errors.NotFoundError('no token found'))
|
|
|
|
}
|
|
|
|
return callback(null, token.data)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|