feat(api): API tokens for authentication

Create and managed simple API tokens for access to the API from external
tools.
This commit is contained in:
Julian Tölle 2023-02-19 16:16:34 +01:00
parent eda89716ef
commit 8f7eebb806
15 changed files with 614 additions and 154 deletions

View file

@ -0,0 +1,77 @@
import {
MigrationInterface,
QueryRunner,
Table,
TableIndex,
TableForeignKey,
} from "typeorm";
import { TableColumnOptions } from "typeorm/schema-builder/options/TableColumnOptions";
const primaryUUIDColumn: TableColumnOptions = {
name: "id",
type: "uuid",
isPrimary: true,
isGenerated: true,
generationStrategy: "uuid",
};
export class CreateApiTokensTable0000000000007 implements MigrationInterface {
async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.createTable(
new Table({
name: "api_token",
columns: [
primaryUUIDColumn,
{
name: "userId",
type: "uuid",
},
{
name: "description",
type: "varchar",
},
{
name: "token",
type: "varchar",
isUnique: true,
},
{
name: "createdAt",
type: "timestamp",
default: "CURRENT_TIMESTAMP",
},
{
name: "lastUsedAt",
type: "timestamp",
default: "CURRENT_TIMESTAMP",
},
{
name: "revokedAt",
type: "timestamp",
default: null,
isNullable: true,
},
],
indices: [
new TableIndex({
name: "IDX_API_TOKEN_USER_ID",
columnNames: ["userId"],
}),
],
foreignKeys: [
new TableForeignKey({
name: "FK_API_TOKEN_USER_ID",
columnNames: ["userId"],
referencedColumnNames: ["id"],
referencedTableName: "user",
}),
],
}),
true
);
}
async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropTable("api_token");
}
}