mirror of
https://github.com/outline/outline.git
synced 2026-12-06 11:12:32 +03:00
Store scopes with full url
This commit is contained in:
1 parent
99bc586f34
commit
cfaa08403a
4 files changed
+49
-25
No files matched your search
@@ -51,55 +51,55 @@ describe("#ApiKey", () => {
|
||||
name: "Dev",
|
||||
});
|
||||
|
||||
expect(apiKey.canAccess("documents.info")).toBe(true);
|
||||
expect(apiKey.canAccess("collections.create")).toBe(true);
|
||||
expect(apiKey.canAccess("apiKeys.list")).toBe(true);
|
||||
expect(apiKey.canAccess("/api/documents.info")).toBe(true);
|
||||
expect(apiKey.canAccess("/api/collections.create")).toBe(true);
|
||||
expect(apiKey.canAccess("/api/apiKeys.list")).toBe(true);
|
||||
});
|
||||
|
||||
it("should return false if no matching scope", async () => {
|
||||
const apiKey = await buildApiKey({
|
||||
name: "Dev",
|
||||
scope: ["documents.info"],
|
||||
scope: ["/api/documents.info"],
|
||||
});
|
||||
|
||||
expect(apiKey.canAccess("documents.info")).toBe(true);
|
||||
expect(apiKey.canAccess("collections.create")).toBe(false);
|
||||
expect(apiKey.canAccess("apiKeys.list")).toBe(false);
|
||||
expect(apiKey.canAccess("/api/documents.info")).toBe(true);
|
||||
expect(apiKey.canAccess("/api/collections.create")).toBe(false);
|
||||
expect(apiKey.canAccess("/api/apiKeys.list")).toBe(false);
|
||||
});
|
||||
|
||||
it("should allow wildcard methods", async () => {
|
||||
const apiKey = await buildApiKey({
|
||||
name: "Dev",
|
||||
scope: ["documents.*"],
|
||||
scope: ["/api/documents.*"],
|
||||
});
|
||||
|
||||
expect(apiKey.canAccess("documents.info")).toBe(true);
|
||||
expect(apiKey.canAccess("documents.create")).toBe(true);
|
||||
expect(apiKey.canAccess("collections.create")).toBe(false);
|
||||
expect(apiKey.canAccess("/api/documents.info")).toBe(true);
|
||||
expect(apiKey.canAccess("/api/documents.create")).toBe(true);
|
||||
expect(apiKey.canAccess("/api/collections.create")).toBe(false);
|
||||
});
|
||||
|
||||
it("should allow wildcard namespaces", async () => {
|
||||
const apiKey = await buildApiKey({
|
||||
name: "Dev",
|
||||
scope: ["*.info"],
|
||||
scope: ["/api/*.info"],
|
||||
});
|
||||
|
||||
expect(apiKey.canAccess("documents.info")).toBe(true);
|
||||
expect(apiKey.canAccess("documents.create")).toBe(false);
|
||||
expect(apiKey.canAccess("collections.create")).toBe(false);
|
||||
expect(apiKey.canAccess("/api/documents.info")).toBe(true);
|
||||
expect(apiKey.canAccess("/api/documents.create")).toBe(false);
|
||||
expect(apiKey.canAccess("/api/collections.create")).toBe(false);
|
||||
});
|
||||
|
||||
it("should allow multiple scopes", async () => {
|
||||
const apiKey = await buildApiKey({
|
||||
name: "Dev",
|
||||
scope: ["*.info", "collections.list"],
|
||||
scope: ["/api/*.info", "/api/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);
|
||||
expect(apiKey.canAccess("/api/shares.info")).toBe(true);
|
||||
expect(apiKey.canAccess("/api/documents.info")).toBe(true);
|
||||
expect(apiKey.canAccess("/api/collections.list")).toBe(true);
|
||||
expect(apiKey.canAccess("/api/documents.create")).toBe(false);
|
||||
expect(apiKey.canAccess("/api/collections.create")).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -42,7 +42,7 @@ class ApiKey extends ParanoidModel<
|
||||
name: string;
|
||||
|
||||
/** A space-separated list of scopes that this API key has access to */
|
||||
@Matches(/[\.\w\s]*/, {
|
||||
@Matches(/[\/\.\w\s]*/, {
|
||||
each: true,
|
||||
})
|
||||
@Column(DataType.ARRAY(DataType.STRING))
|
||||
@@ -174,12 +174,15 @@ class ApiKey extends ParanoidModel<
|
||||
return true;
|
||||
}
|
||||
|
||||
const resource = path.replace("/", "");
|
||||
const resource = path.split("/").pop() ?? "";
|
||||
const [namespace, method] = resource.split(".");
|
||||
|
||||
return this.scope.some((scope) => {
|
||||
const [scopeNamespace, scopeMethod] = scope.split(".");
|
||||
const [scopeNamespace, scopeMethod] = scope
|
||||
.replace("/api/", "")
|
||||
.split(".");
|
||||
return (
|
||||
scope.startsWith("/api/") &&
|
||||
(namespace === scopeNamespace || scopeNamespace === "*") &&
|
||||
(method === scopeMethod || scopeMethod === "*")
|
||||
);
|
||||
|
||||
@@ -40,6 +40,27 @@ describe("#apiKeys.create", () => {
|
||||
expect(body.data.lastActiveAt).toBeNull();
|
||||
});
|
||||
|
||||
it("should allow creating an api key with scopes", async () => {
|
||||
const user = await buildUser();
|
||||
|
||||
const res = await server.post("/api/apiKeys.create", {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
name: "My API Key",
|
||||
scope: ["/api/documents.list", "*.info", "users.*"],
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.data.name).toEqual("My API Key");
|
||||
expect(body.data.scope).toEqual([
|
||||
"/api/documents.list",
|
||||
"/api/*.info",
|
||||
"/api/users.*",
|
||||
]);
|
||||
});
|
||||
|
||||
it("should require authentication", async () => {
|
||||
const res = await server.post("/api/apiKeys.create");
|
||||
expect(res.status).toEqual(401);
|
||||
|
||||
@@ -28,7 +28,7 @@ router.post(
|
||||
name,
|
||||
userId: user.id,
|
||||
expiresAt,
|
||||
scope,
|
||||
scope: scope?.map((s) => (s.startsWith("/api/") ? s : `/api/${s}`)),
|
||||
});
|
||||
|
||||
ctx.body = {
|
||||
|
||||
Reference in new issue
Block a user