2021-03-29 19:58:11 +02:00
|
|
|
import { EventEmitter } from 'events';
|
|
|
|
import { Knex } from 'knex';
|
2021-05-02 21:11:17 +02:00
|
|
|
import metricsHelper from '../util/metrics-helper';
|
2021-04-29 10:21:29 +02:00
|
|
|
import { DB_TIME } from '../metric-events';
|
2021-03-29 19:58:11 +02:00
|
|
|
import { Logger, LogProvider } from '../logger';
|
|
|
|
import NotFoundError from '../error/notfound-error';
|
2021-09-15 20:28:10 +02:00
|
|
|
import { IApiTokenStore } from '../types/stores/api-token-store';
|
2021-08-12 15:04:37 +02:00
|
|
|
import {
|
|
|
|
ApiTokenType,
|
|
|
|
IApiToken,
|
|
|
|
IApiTokenCreate,
|
2021-09-15 20:28:10 +02:00
|
|
|
} from '../types/models/api-token';
|
2021-03-29 19:58:11 +02:00
|
|
|
|
|
|
|
const TABLE = 'api_tokens';
|
|
|
|
|
2021-09-15 20:28:10 +02:00
|
|
|
const ALL = '*';
|
|
|
|
|
2021-03-29 19:58:11 +02:00
|
|
|
interface ITokenTable {
|
|
|
|
id: number;
|
|
|
|
secret: string;
|
|
|
|
username: string;
|
|
|
|
type: ApiTokenType;
|
|
|
|
expires_at?: Date;
|
|
|
|
created_at: Date;
|
|
|
|
seen_at?: Date;
|
2021-09-15 20:28:10 +02:00
|
|
|
environment: string;
|
|
|
|
project: string;
|
2021-03-29 19:58:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const toRow = (newToken: IApiTokenCreate) => ({
|
|
|
|
username: newToken.username,
|
|
|
|
secret: newToken.secret,
|
|
|
|
type: newToken.type,
|
2021-09-15 20:28:10 +02:00
|
|
|
project: newToken.project === ALL ? undefined : newToken.project,
|
|
|
|
environment:
|
|
|
|
newToken.environment === ALL ? undefined : newToken.environment,
|
2021-03-29 19:58:11 +02:00
|
|
|
expires_at: newToken.expiresAt,
|
|
|
|
});
|
|
|
|
|
|
|
|
const toToken = (row: ITokenTable): IApiToken => ({
|
|
|
|
secret: row.secret,
|
|
|
|
username: row.username,
|
|
|
|
type: row.type,
|
2021-09-15 20:28:10 +02:00
|
|
|
environment: row.environment ? row.environment : ALL,
|
|
|
|
project: row.project ? row.project : ALL,
|
2021-03-29 19:58:11 +02:00
|
|
|
expiresAt: row.expires_at,
|
|
|
|
createdAt: row.created_at,
|
|
|
|
});
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
export class ApiTokenStore implements IApiTokenStore {
|
2021-03-29 19:58:11 +02:00
|
|
|
private logger: Logger;
|
|
|
|
|
|
|
|
private timer: Function;
|
|
|
|
|
|
|
|
private db: Knex;
|
|
|
|
|
|
|
|
constructor(db: Knex, eventBus: EventEmitter, getLogger: LogProvider) {
|
|
|
|
this.db = db;
|
|
|
|
this.logger = getLogger('api-tokens.js');
|
|
|
|
this.timer = (action: string) =>
|
|
|
|
metricsHelper.wrapTimer(eventBus, DB_TIME, {
|
|
|
|
store: 'api-tokens',
|
|
|
|
action,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAll(): Promise<IApiToken[]> {
|
|
|
|
const stopTimer = this.timer('getAll');
|
|
|
|
const rows = await this.db<ITokenTable>(TABLE);
|
|
|
|
stopTimer();
|
|
|
|
return rows.map(toToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAllActive(): Promise<IApiToken[]> {
|
|
|
|
const stopTimer = this.timer('getAllActive');
|
|
|
|
const rows = await this.db<ITokenTable>(TABLE)
|
2021-09-02 10:05:31 +02:00
|
|
|
.where('expires_at', 'IS', null)
|
|
|
|
.orWhere('expires_at', '>', 'now()');
|
2021-03-29 19:58:11 +02:00
|
|
|
stopTimer();
|
|
|
|
return rows.map(toToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
async insert(newToken: IApiTokenCreate): Promise<IApiToken> {
|
|
|
|
const [row] = await this.db<ITokenTable>(TABLE).insert(
|
|
|
|
toRow(newToken),
|
|
|
|
['created_at'],
|
|
|
|
);
|
|
|
|
return { ...newToken, createdAt: row.created_at };
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
destroy(): void {}
|
|
|
|
|
|
|
|
async exists(secret: string): Promise<boolean> {
|
|
|
|
const result = await this.db.raw(
|
|
|
|
`SELECT EXISTS (SELECT 1 FROM ${TABLE} WHERE secret = ?) AS present`,
|
|
|
|
[secret],
|
|
|
|
);
|
|
|
|
const { present } = result.rows[0];
|
|
|
|
return present;
|
|
|
|
}
|
|
|
|
|
|
|
|
async get(key: string): Promise<IApiToken> {
|
|
|
|
const row = await this.db(TABLE).where('secret', key).first();
|
|
|
|
return toToken(row);
|
|
|
|
}
|
|
|
|
|
2021-03-29 19:58:11 +02:00
|
|
|
async delete(secret: string): Promise<void> {
|
2021-08-12 15:04:37 +02:00
|
|
|
return this.db<ITokenTable>(TABLE).where({ secret }).del();
|
2021-03-29 19:58:11 +02:00
|
|
|
}
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
async deleteAll(): Promise<void> {
|
|
|
|
return this.db<ITokenTable>(TABLE).del();
|
|
|
|
}
|
|
|
|
|
2021-03-29 19:58:11 +02:00
|
|
|
async setExpiry(secret: string, expiresAt: Date): Promise<IApiToken> {
|
|
|
|
const rows = await this.db<ITokenTable>(TABLE)
|
|
|
|
.update({ expires_at: expiresAt })
|
|
|
|
.where({ secret })
|
|
|
|
.returning('*');
|
|
|
|
if (rows.length > 0) {
|
|
|
|
return toToken(rows[0]);
|
|
|
|
}
|
|
|
|
throw new NotFoundError('Could not find api-token.');
|
|
|
|
}
|
|
|
|
|
|
|
|
async markSeenAt(secrets: string[]): Promise<void> {
|
|
|
|
const now = new Date();
|
|
|
|
try {
|
|
|
|
await this.db(TABLE)
|
|
|
|
.whereIn('secrets', secrets)
|
|
|
|
.update({ seen_at: now });
|
|
|
|
} catch (err) {
|
|
|
|
this.logger.error('Could not update lastSeen, error: ', err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|