| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- /* eslint-disable
- handle-callback-err,
- 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')
- const { db } = require('../../infrastructure/mongojs')
- 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) {
- callback = function(error, data) {}
- }
- 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')
- return db.tokens.insert(
- {
- use,
- token,
- data,
- createdAt,
- expiresAt
- },
- function(error) {
- if (error != null) {
- return callback(error)
- }
- return callback(null, token)
- }
- )
- },
- getValueFromTokenAndExpire(use, token, callback) {
- if (callback == null) {
- callback = function(error, data) {}
- }
- const now = new Date()
- return db.tokens.findAndModify(
- {
- query: {
- use,
- token,
- expiresAt: { $gt: now },
- usedAt: { $exists: false }
- },
- update: {
- $set: {
- usedAt: now
- }
- }
- },
- function(error, token) {
- if (error != null) {
- return callback(error)
- }
- if (token == null) {
- return callback(new Errors.NotFoundError('no token found'))
- }
- return callback(null, token.data)
- }
- )
- }
- }
|