mirror of
https://github.com/outline/outline.git
synced 2026-11-27 16:08:58 +03:00
scope validation
This commit is contained in:
1 parent
581502f7e2
commit
2136be9327
2 files changed
+79
-3
No files matched your search
@@ -0,0 +1,75 @@
|
||||
import { v4 } from "uuid";
|
||||
import { Scope } from "@shared/types";
|
||||
import { OAuthInterface } from "./OAuthInterface";
|
||||
|
||||
describe("OAuthInterface", () => {
|
||||
const user = {
|
||||
id: v4(),
|
||||
};
|
||||
const client = {
|
||||
id: v4(),
|
||||
grants: ["authorization_code", "refresh_token"],
|
||||
};
|
||||
|
||||
describe("#validateScope", () => {
|
||||
it("should return empty array for empty scope", async () => {
|
||||
const result = await OAuthInterface.validateScope(user, client, []);
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it("should return empty array for empty scope", async () => {
|
||||
const result = await OAuthInterface.validateScope(
|
||||
user,
|
||||
client,
|
||||
undefined
|
||||
);
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it("should allow valid global scopes", async () => {
|
||||
const scope = [Scope.Read, Scope.Write];
|
||||
const result = await OAuthInterface.validateScope(user, client, scope);
|
||||
expect(result).toEqual(scope);
|
||||
});
|
||||
|
||||
it("should allow route scopes", async () => {
|
||||
const scope = [
|
||||
"/api/documents.info",
|
||||
"/api/documents.create",
|
||||
"/api/documents.update",
|
||||
"/api/documents.delete",
|
||||
];
|
||||
const result = await OAuthInterface.validateScope(user, client, scope);
|
||||
expect(result).toEqual(scope);
|
||||
});
|
||||
|
||||
it("should allow scopes with colon and valid prefix", async () => {
|
||||
const scope = [
|
||||
"documents:read",
|
||||
"documents:write",
|
||||
"collections:read",
|
||||
"collections:write",
|
||||
];
|
||||
const result = await OAuthInterface.validateScope(user, client, scope);
|
||||
expect(result).toEqual(scope);
|
||||
});
|
||||
|
||||
it("should reject invalid route scopes", async () => {
|
||||
const scope = ["invalid.scope.periods"];
|
||||
const result = await OAuthInterface.validateScope(user, client, scope);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it("should reject invalid access scopes", async () => {
|
||||
const scope = ["documents:invalid"];
|
||||
const result = await OAuthInterface.validateScope(user, client, scope);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it("should reject malformed access scopes", async () => {
|
||||
const scope = ["documents::read"];
|
||||
const result = await OAuthInterface.validateScope(user, client, scope);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -3,6 +3,7 @@ import {
|
||||
AuthorizationCodeModel,
|
||||
} from "@node-oauth/oauth2-server";
|
||||
import rs from "randomstring";
|
||||
import { Required } from "utility-types";
|
||||
import { Scope } from "@shared/types";
|
||||
import {
|
||||
OAuthClient,
|
||||
@@ -16,7 +17,7 @@ interface Config {
|
||||
}
|
||||
|
||||
export const OAuthInterface: RefreshTokenModel &
|
||||
AuthorizationCodeModel &
|
||||
Required<AuthorizationCodeModel, "validateScope"> &
|
||||
Config = {
|
||||
grants: ["authorization_code", "refresh_token"],
|
||||
|
||||
@@ -219,7 +220,7 @@ export const OAuthInterface: RefreshTokenModel &
|
||||
* @returns The scopes if valid, false otherwise.
|
||||
*/
|
||||
async validateScope(user, client, scope) {
|
||||
if (!scope) {
|
||||
if (!scope?.length) {
|
||||
return [];
|
||||
}
|
||||
|
||||
@@ -240,7 +241,7 @@ export const OAuthInterface: RefreshTokenModel &
|
||||
|
||||
if (
|
||||
colonCount === 1 &&
|
||||
validAccessScopes.includes(s.split(":")[0] as Scope)
|
||||
validAccessScopes.includes(s.split(":")[1] as Scope)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user