unleash-server
Version:
Unleash is an enterprise ready feature flag service. It provides different strategies for handling feature flags.
118 lines • 3.77 kB
JavaScript
import metricsHelper from '../util/metrics-helper.js';
import { DB_TIME } from '../metric-events.js';
import NotFoundError from '../error/notfound-error.js';
const TABLE = 'reset_tokens';
const rowToResetToken = (row) => ({
userId: row.user_id,
token: row.reset_token,
expiresAt: row.expires_at,
createdAt: row.created_at,
createdBy: row.created_by,
usedAt: row.used_at,
});
export class ResetTokenStore {
constructor(db, eventBus, _getLogger) {
this.db = db;
this.timer = (action) => metricsHelper.wrapTimer(eventBus, DB_TIME, {
store: 'reset-tokens',
action,
});
}
async getActive(token) {
const stop = this.timer('getActive');
const row = await this.db(TABLE)
.where({ reset_token: token })
.where('expires_at', '>', new Date())
.first();
stop();
if (!row) {
throw new NotFoundError('Could not find an active token');
}
return rowToResetToken(row);
}
async getActiveTokens() {
const stop = this.timer('getActiveTokens');
const rows = await this.db(TABLE)
.whereNull('used_at')
.andWhere('expires_at', '>', new Date());
stop();
return rows.map(rowToResetToken);
}
async insert(newToken) {
const stop = this.timer('insert_token');
const [row] = await this.db(TABLE)
.insert(newToken)
.returning(['created_at']);
stop();
return {
userId: newToken.user_id,
token: newToken.reset_token,
expiresAt: newToken.expires_at,
createdAt: row.created_at,
createdBy: newToken.created_by,
};
}
async useToken(token) {
const stop = this.timer('use_token');
try {
await this.db(TABLE)
.update({ used_at: new Date() })
.where({ reset_token: token.token, user_id: token.userId });
return true;
}
catch (_e) {
return false;
}
finally {
stop();
}
}
async deleteFromQuery({ reset_token }) {
const stop = this.timer('deleteFromQuery');
await this.db(TABLE).where(reset_token).del();
stop();
}
async deleteAll() {
const stop = this.timer('deleteAll');
await this.db(TABLE).del();
stop();
}
async deleteExpired() {
const stop = this.timer('deleteExpired');
await this.db(TABLE).where('expires_at', '<', new Date()).del();
stop();
}
async expireExistingTokensForUser(user_id) {
const stop = this.timer('expireExistingTokensForUser');
await this.db(TABLE).where({ user_id }).update({
expires_at: new Date(),
});
stop();
}
async delete(reset_token) {
const stop = this.timer('delete');
await this.db(TABLE).where({ reset_token }).del();
stop();
}
destroy() { }
async exists(reset_token) {
const stop = this.timer('exists');
const result = await this.db.raw(`SELECT EXISTS (SELECT 1 FROM ${TABLE} WHERE reset_token = ?) AS present`, [reset_token]);
stop();
const { present } = result.rows[0];
return present;
}
async get(key) {
const stop = this.timer('get');
const row = await this.db(TABLE).where({ reset_token: key }).first();
stop();
return rowToResetToken(row);
}
async getAll() {
const stop = this.timer('getAll');
const rows = await this.db(TABLE).select();
stop();
return rows.map(rowToResetToken);
}
}
//# sourceMappingURL=reset-token-store.js.map