Switch to storing array

This commit is contained in:
Tom Moor committed 2025-01-27 21:22:54 -05:00
1 parent 75838bb311
commit 99bc586f34
6 files changed
+28 -20

No files matched your search

+2 -8
View File
@@ -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() {
+1 -1
View File
@@ -75,7 +75,7 @@ function ApiKeyNew({ onSubmit }: Props) {
await apiKeys.create({
name,
expiresAt: expiresAt?.toISOString(),
scope,
scope: scope ? scope.split(" ") : undefined,
});
toast.success(
t(
@@ -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 });
});
+16 -3
View File
@@ -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);
});
});
});
+6 -5
View File
@@ -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 === "*") &&
+2 -2
View File
@@ -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(),
}),
});