From a95005776f0e654617b47fbc2d565c2cd4f0afeb Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Fri, 24 Jan 2025 22:45:32 -0500 Subject: [PATCH] scope storage --- app/models/ApiKey.ts | 13 ++++++++++++- .../20250125031823-add-api-key-scopes.js | 19 +++++++++++++++++++ server/models/ApiKey.ts | 9 +++++++++ server/presenters/apiKey.ts | 1 + 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 server/migrations/20250125031823-add-api-key-scopes.js diff --git a/app/models/ApiKey.ts b/app/models/ApiKey.ts index 6bb33992c2..b1dbb5bb43 100644 --- a/app/models/ApiKey.ts +++ b/app/models/ApiKey.ts @@ -6,11 +6,16 @@ import Field from "./decorators/Field"; class ApiKey extends ParanoidModel { static modelName = "ApiKey"; - /** The user chosen name of the API key. */ + /** The human-readable name of this API key */ @Field @observable name: string; + /** A space-separated list of scopes that this API key has access to */ + @Field + @observable + scope?: string; + /** An optional datetime that the API key expires. */ @Field @observable @@ -29,6 +34,12 @@ class ApiKey extends ParanoidModel { /** A preview of the last 4 characters of the API key. */ last4: string; + /** An array of scopes that this API key has access to. */ + @computed + get scopes() { + return this.scope?.split(" ") ?? []; + } + /** Whether the API key has an expiry in the past. */ @computed get isExpired() { diff --git a/server/migrations/20250125031823-add-api-key-scopes.js b/server/migrations/20250125031823-add-api-key-scopes.js new file mode 100644 index 0000000000..176e03a4f7 --- /dev/null +++ b/server/migrations/20250125031823-add-api-key-scopes.js @@ -0,0 +1,19 @@ +"use strict"; + +/** @type {import('sequelize-cli').Migration} */ +module.exports = { + async up(queryInterface, Sequelize) { + await queryInterface.sequelize.transaction(async transaction => { + await queryInterface.addColumn("apiKeys", "scope", { + type: Sequelize.STRING, + allowNull: true, + }, { transaction }); + }); + }, + + async down(queryInterface) { + await queryInterface.sequelize.transaction(async transaction => { + await queryInterface.removeColumn("apiKeys", "scope", { transaction }); + }); + }, +}; diff --git a/server/models/ApiKey.ts b/server/models/ApiKey.ts index a3159d20dc..e4755b855f 100644 --- a/server/models/ApiKey.ts +++ b/server/models/ApiKey.ts @@ -1,4 +1,5 @@ import crypto from "crypto"; +import { Matches } from "class-validator"; import { subMinutes } from "date-fns"; import randomstring from "randomstring"; import { InferAttributes, InferCreationAttributes, Op } from "sequelize"; @@ -31,6 +32,7 @@ class ApiKey extends ParanoidModel< static eventNamespace = "api_keys"; + /** The human-readable name of this API key */ @Length({ min: ApiKeyValidation.minNameLength, max: ApiKeyValidation.maxNameLength, @@ -39,6 +41,11 @@ class ApiKey extends ParanoidModel< @Column name: string; + /** A space-separated list of scopes that this API key has access to */ + @Matches(/[\.\w\s]*/) + @Column + scope: string | null; + /** @deprecated The plain text value of the API key, removed soon. */ @Unique @Column @@ -59,10 +66,12 @@ class ApiKey extends ParanoidModel< @SkipChangeset last4: string; + /** The date and time when this API key will expire */ @IsDate @Column expiresAt: Date | null; + /** The date and time when this API key was last used */ @IsDate @Column @SkipChangeset diff --git a/server/presenters/apiKey.ts b/server/presenters/apiKey.ts index 943cf78cb8..cb73f8754f 100644 --- a/server/presenters/apiKey.ts +++ b/server/presenters/apiKey.ts @@ -5,6 +5,7 @@ export default function presentApiKey(apiKey: ApiKey) { id: apiKey.id, userId: apiKey.userId, name: apiKey.name, + scope: apiKey.scope, value: apiKey.value, last4: apiKey.last4, createdAt: apiKey.createdAt,