2020-09-18 17:05:56 +03:00
|
|
|
const ghostBookshelf = require('./base');
|
|
|
|
const crypto = require('crypto');
|
2021-06-15 17:36:27 +03:00
|
|
|
const logging = require('@tryghost/logging');
|
2020-09-18 17:05:56 +03:00
|
|
|
|
|
|
|
const SingleUseToken = ghostBookshelf.Model.extend({
|
|
|
|
tableName: 'tokens',
|
|
|
|
|
|
|
|
defaults() {
|
|
|
|
return {
|
|
|
|
token: crypto
|
|
|
|
.randomBytes(192 / 8)
|
|
|
|
.toString('base64')
|
|
|
|
// base64url encoding means the tokens are URL safe
|
2020-10-20 09:19:20 +03:00
|
|
|
.replace(/\+/g, '-')
|
|
|
|
.replace(/\//g, '_')
|
2020-09-18 17:05:56 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
async findOne(data, unfilteredOptions = {}) {
|
|
|
|
const model = await ghostBookshelf.Model.findOne.call(this, data, unfilteredOptions);
|
|
|
|
|
|
|
|
if (model) {
|
2021-01-18 20:03:41 +03:00
|
|
|
setTimeout(async () => {
|
|
|
|
try {
|
|
|
|
await this.destroy(Object.assign({
|
|
|
|
destroyBy: {
|
|
|
|
id: model.id
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
...unfilteredOptions,
|
|
|
|
transacting: null
|
|
|
|
}));
|
|
|
|
} catch (err) {
|
|
|
|
logging.error(err);
|
2020-09-18 17:05:56 +03:00
|
|
|
}
|
2021-01-18 20:03:41 +03:00
|
|
|
}, 10000);
|
2020-09-18 17:05:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return model;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const SingleUseTokens = ghostBookshelf.Collection.extend({
|
|
|
|
model: SingleUseToken
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
SingleUseToken: ghostBookshelf.model('SingleUseToken', SingleUseToken),
|
|
|
|
SingleUseTokens: ghostBookshelf.collection('SingleUseTokens', SingleUseTokens)
|
|
|
|
};
|