mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-28 05:37:34 +03:00
812e4b682f
no-issue This is a model for the tokens table, which handles the single use aspect by customising the `findOne` method to automatically destroy the model after reading from it
46 lines
1.3 KiB
JavaScript
46 lines
1.3 KiB
JavaScript
const ghostBookshelf = require('./base');
|
|
const crypto = require('crypto');
|
|
|
|
const SingleUseToken = ghostBookshelf.Model.extend({
|
|
tableName: 'tokens',
|
|
|
|
defaults() {
|
|
return {
|
|
token: crypto
|
|
.randomBytes(192 / 8)
|
|
.toString('base64')
|
|
// base64url encoding means the tokens are URL safe
|
|
.replace('+', '-')
|
|
.replace('/', '_')
|
|
};
|
|
}
|
|
}, {
|
|
async findOne(data, unfilteredOptions = {}) {
|
|
if (!unfilteredOptions.transacting) {
|
|
return ghostBookshelf.transaction((transacting) => {
|
|
return this.findOne(data, Object.assign({transacting}, unfilteredOptions));
|
|
});
|
|
}
|
|
const model = await ghostBookshelf.Model.findOne.call(this, data, unfilteredOptions);
|
|
|
|
if (model) {
|
|
await this.destroy(Object.assign({
|
|
destroyBy: {
|
|
id: model.id
|
|
}
|
|
}, unfilteredOptions));
|
|
}
|
|
|
|
return model;
|
|
}
|
|
});
|
|
|
|
const SingleUseTokens = ghostBookshelf.Collection.extend({
|
|
model: SingleUseToken
|
|
});
|
|
|
|
module.exports = {
|
|
SingleUseToken: ghostBookshelf.model('SingleUseToken', SingleUseToken),
|
|
SingleUseTokens: ghostBookshelf.collection('SingleUseTokens', SingleUseTokens)
|
|
};
|