diff --git a/app/models/ApiKey.ts b/app/models/ApiKey.ts index b1dbb5bb43..fb73d2cf43 100644 --- a/app/models/ApiKey.ts +++ b/app/models/ApiKey.ts @@ -11,10 +11,10 @@ class ApiKey extends ParanoidModel { @observable name: string; - /** A space-separated list of scopes that this API key has access to */ + /** A list of scopes that this API key has access to. If empty, the key has full access. */ @Field @observable - scope?: string; + scope?: string[]; /** An optional datetime that the API key expires. */ @Field @@ -34,12 +34,6 @@ 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/app/scenes/ApiKeyNew/index.tsx b/app/scenes/ApiKeyNew/index.tsx index c5ef830a87..3c68f1f3ed 100644 --- a/app/scenes/ApiKeyNew/index.tsx +++ b/app/scenes/ApiKeyNew/index.tsx @@ -75,7 +75,7 @@ function ApiKeyNew({ onSubmit }: Props) { await apiKeys.create({ name, expiresAt: expiresAt?.toISOString(), - scope, + scope: scope ? scope.split(" ") : undefined, }); toast.success( t( diff --git a/server/migrations/20250125031823-add-api-key-scopes.js b/server/migrations/20250125031823-add-api-key-scopes.js index 176e03a4f7..f1f9a6d180 100644 --- a/server/migrations/20250125031823-add-api-key-scopes.js +++ b/server/migrations/20250125031823-add-api-key-scopes.js @@ -5,7 +5,7 @@ module.exports = { async up(queryInterface, Sequelize) { await queryInterface.sequelize.transaction(async transaction => { await queryInterface.addColumn("apiKeys", "scope", { - type: Sequelize.STRING, + type: Sequelize.ARRAY(Sequelize.STRING), allowNull: true, }, { transaction }); }); diff --git a/server/models/ApiKey.test.ts b/server/models/ApiKey.test.ts index 99e7e2a873..ced9587871 100644 --- a/server/models/ApiKey.test.ts +++ b/server/models/ApiKey.test.ts @@ -59,7 +59,7 @@ describe("#ApiKey", () => { it("should return false if no matching scope", async () => { const apiKey = await buildApiKey({ name: "Dev", - scope: "documents.info", + scope: ["documents.info"], }); expect(apiKey.canAccess("documents.info")).toBe(true); @@ -70,7 +70,7 @@ describe("#ApiKey", () => { it("should allow wildcard methods", async () => { const apiKey = await buildApiKey({ name: "Dev", - scope: "documents.*", + scope: ["documents.*"], }); expect(apiKey.canAccess("documents.info")).toBe(true); @@ -81,12 +81,25 @@ describe("#ApiKey", () => { it("should allow wildcard namespaces", async () => { const apiKey = await buildApiKey({ name: "Dev", - scope: "*.info", + scope: ["*.info"], }); expect(apiKey.canAccess("documents.info")).toBe(true); expect(apiKey.canAccess("documents.create")).toBe(false); expect(apiKey.canAccess("collections.create")).toBe(false); }); + + it("should allow multiple scopes", async () => { + const apiKey = await buildApiKey({ + name: "Dev", + scope: ["*.info", "collections.list"], + }); + + expect(apiKey.canAccess("shares.info")).toBe(true); + expect(apiKey.canAccess("documents.info")).toBe(true); + expect(apiKey.canAccess("collections.list")).toBe(true); + expect(apiKey.canAccess("documents.create")).toBe(false); + expect(apiKey.canAccess("collections.create")).toBe(false); + }); }); }); diff --git a/server/models/ApiKey.ts b/server/models/ApiKey.ts index 258767650f..3ac32b5367 100644 --- a/server/models/ApiKey.ts +++ b/server/models/ApiKey.ts @@ -42,9 +42,11 @@ class ApiKey extends ParanoidModel< name: string; /** A space-separated list of scopes that this API key has access to */ - @Matches(/[\.\w\s]*/) - @Column - scope: string | null; + @Matches(/[\.\w\s]*/, { + each: true, + }) + @Column(DataType.ARRAY(DataType.STRING)) + scope: string[] | null; /** @deprecated The plain text value of the API key, removed soon. */ @Unique @@ -174,9 +176,8 @@ class ApiKey extends ParanoidModel< const resource = path.replace("/", ""); const [namespace, method] = resource.split("."); - const scopes = this.scope.split(" "); - return scopes.some((scope) => { + return this.scope.some((scope) => { const [scopeNamespace, scopeMethod] = scope.split("."); return ( (namespace === scopeNamespace || scopeNamespace === "*") && diff --git a/server/routes/api/apiKeys/schema.ts b/server/routes/api/apiKeys/schema.ts index 59a287ae42..746de0f781 100644 --- a/server/routes/api/apiKeys/schema.ts +++ b/server/routes/api/apiKeys/schema.ts @@ -7,8 +7,8 @@ export const APIKeysCreateSchema = BaseSchema.extend({ name: z.string(), /** API Key expiry date */ expiresAt: z.coerce.date().optional(), - /** A space-separated list of scopes that this API key has access to */ - scope: z.string().optional(), + /** A list of scopes that this API key has access to */ + scope: z.array(z.string()).optional(), }), });