mirror of
https://github.com/outline/outline.git
synced 2026-11-22 22:02:35 +03:00
Switch to storing array
This commit is contained in:
1 parent
75838bb311
commit
99bc586f34
6 files changed
+28
-20
No files matched your search
@@ -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() {
|
||||
|
||||
@@ -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 });
|
||||
});
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -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 === "*") &&
|
||||
|
||||
@@ -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(),
|
||||
}),
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user