@lobb-js/lobb-ext-auth 0.6.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +2 -1
- package/dist/onStartup.d.ts +1 -0
- package/dist/onStartup.js +18 -2
- package/extensions/auth/collections/collections.ts +6 -0
- package/extensions/auth/collections/users.ts +2 -2
- package/extensions/auth/config/extensionConfigSchema.ts +3 -0
- package/extensions/auth/meta/meta.ts +1 -0
- package/extensions/auth/studio/index.ts +2 -1
- package/extensions/auth/studio/onStartup.ts +25 -2
- package/extensions/auth/tests/configs/auth.ts +11 -0
- package/extensions/auth/tests/controllers/me.test.ts +2 -2
- package/extensions/auth/tests/middlewares/adminProtection.test.ts +59 -0
- package/extensions/auth/tests/middlewares/publicPreventBasic.test.ts +4 -4
- package/extensions/auth/workflows/baseWorkflow.ts +15 -2
- package/extensions/auth/workflows/utils.ts +6 -0
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Auth } from "./auth";
|
|
2
|
-
import { onStartup } from "./onStartup";
|
|
2
|
+
import { onStartup, onRouteChange } from "./onStartup";
|
|
3
3
|
import LoginPage from "./lib/components/pages/loginPage/index.svelte";
|
|
4
4
|
import UserSettings from "./lib/components/pages/userSettings/index.svelte";
|
|
5
5
|
import Settings from "./lib/components/pages/settings/index.svelte";
|
|
@@ -8,6 +8,7 @@ export default function extension(utils) {
|
|
|
8
8
|
return {
|
|
9
9
|
name: "auth",
|
|
10
10
|
onStartup: onStartup,
|
|
11
|
+
onRouteChange: onRouteChange,
|
|
11
12
|
components: {
|
|
12
13
|
"pages.login_page": LoginPage,
|
|
13
14
|
"pages.user_settings": UserSettings,
|
package/dist/onStartup.d.ts
CHANGED
package/dist/onStartup.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { Auth } from "./auth";
|
|
2
|
+
const loginPath = "/studio/extensions/auth/login_page";
|
|
3
|
+
function isPublicPath(path, publicPaths) {
|
|
4
|
+
return path === loginPath || publicPaths.some(p => path.startsWith(p));
|
|
5
|
+
}
|
|
2
6
|
export async function onStartup(utils) {
|
|
3
|
-
// logout if we got an Unauthorized response
|
|
4
7
|
const auth = new Auth(utils);
|
|
8
|
+
// Logout if we got an Unauthorized response
|
|
5
9
|
utils.lobb.onResponse(async (response) => {
|
|
6
10
|
if (response.status === 401) {
|
|
7
11
|
const body = await response.json();
|
|
@@ -12,10 +16,22 @@ export async function onStartup(utils) {
|
|
|
12
16
|
}
|
|
13
17
|
});
|
|
14
18
|
const session = auth.getSession();
|
|
15
|
-
// if user is logged in
|
|
16
19
|
if (session) {
|
|
17
20
|
utils.ctx.extensions.auth.session = session;
|
|
18
21
|
return;
|
|
19
22
|
}
|
|
23
|
+
const publicPaths = utils.ctx.meta?.extensions?.auth?.studio?.publicPaths ?? [];
|
|
24
|
+
if (isPublicPath(window.location.pathname, publicPaths)) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
20
27
|
await auth.logout();
|
|
21
28
|
}
|
|
29
|
+
export function onRouteChange(utils, path) {
|
|
30
|
+
const auth = new Auth(utils);
|
|
31
|
+
if (auth.getSession())
|
|
32
|
+
return;
|
|
33
|
+
const publicPaths = utils.ctx.meta?.extensions?.auth?.studio?.publicPaths ?? [];
|
|
34
|
+
if (!isPublicPath(path, publicPaths)) {
|
|
35
|
+
utils.location.navigate(`${loginPath}?redirect=${encodeURIComponent(path)}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -12,6 +12,12 @@ export function collections(
|
|
|
12
12
|
collectionsSchemas["auth_users"] = usersCollection;
|
|
13
13
|
collectionsSchemas["auth_sessions"] = sessionsCollection;
|
|
14
14
|
|
|
15
|
+
// Convert the role field to an enum based on configured roles
|
|
16
|
+
const roleNames = Object.keys(extensionConfig.roles ?? {}).filter(r => r !== "public");
|
|
17
|
+
if (roleNames.length > 0) {
|
|
18
|
+
(usersCollection.fields.role as any).enum = [{ value: "admin" }, ...roleNames.map(r => ({ value: r }))];
|
|
19
|
+
}
|
|
20
|
+
|
|
15
21
|
// adding the additional fields and indexes if the extend_users property exists
|
|
16
22
|
if (extensionConfig.extend_users) {
|
|
17
23
|
collectionsSchemas["auth_users"].indexes = {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { NormalCollectionConfig } from "@lobb-js/core";
|
|
2
2
|
import { hash } from "argon2";
|
|
3
3
|
|
|
4
|
-
export const usersCollection:
|
|
4
|
+
export const usersCollection: NormalCollectionConfig = {
|
|
5
5
|
indexes: {
|
|
6
6
|
auth_users_email_index: {
|
|
7
7
|
unique: true,
|
|
@@ -6,6 +6,7 @@ export async function meta(lobb: Lobb, extensionConfig: ExtensionConfig) {
|
|
|
6
6
|
const meta: any = {};
|
|
7
7
|
|
|
8
8
|
meta["dashboard_access_roles"] = config.dashboard_access_roles ?? ["admin"];
|
|
9
|
+
meta["studio"] = { publicPaths: config.studio?.publicPaths ?? [] };
|
|
9
10
|
|
|
10
11
|
return meta;
|
|
11
12
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Extension, ExtensionUtils } from "@lobb-js/studio";
|
|
2
2
|
import { Auth } from "./auth";
|
|
3
|
-
import { onStartup } from "./onStartup";
|
|
3
|
+
import { onStartup, onRouteChange } from "./onStartup";
|
|
4
4
|
import LoginPage from "./lib/components/pages/loginPage/index.svelte";
|
|
5
5
|
import UserSettings from "./lib/components/pages/userSettings/index.svelte";
|
|
6
6
|
import Settings from "./lib/components/pages/settings/index.svelte";
|
|
@@ -10,6 +10,7 @@ export default function extension(utils: ExtensionUtils): Extension {
|
|
|
10
10
|
return {
|
|
11
11
|
name: "auth",
|
|
12
12
|
onStartup: onStartup,
|
|
13
|
+
onRouteChange: onRouteChange,
|
|
13
14
|
components: {
|
|
14
15
|
"pages.login_page": LoginPage,
|
|
15
16
|
"pages.user_settings": UserSettings,
|
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
import type { ExtensionUtils } from "@lobb-js/studio";
|
|
2
2
|
import { Auth } from "./auth";
|
|
3
3
|
|
|
4
|
+
const loginPath = "/studio/extensions/auth/login_page";
|
|
5
|
+
|
|
6
|
+
function isPublicPath(path: string, publicPaths: string[]) {
|
|
7
|
+
return path === loginPath || publicPaths.some(p => path.startsWith(p));
|
|
8
|
+
}
|
|
9
|
+
|
|
4
10
|
export async function onStartup(utils: ExtensionUtils) {
|
|
5
|
-
// logout if we got an Unauthorized response
|
|
6
11
|
const auth = new Auth(utils);
|
|
12
|
+
|
|
13
|
+
// Logout if we got an Unauthorized response
|
|
7
14
|
utils.lobb.onResponse(async (response) => {
|
|
8
15
|
if (response.status === 401) {
|
|
9
16
|
const body = await response.json();
|
|
@@ -15,11 +22,27 @@ export async function onStartup(utils: ExtensionUtils) {
|
|
|
15
22
|
}
|
|
16
23
|
}
|
|
17
24
|
});
|
|
25
|
+
|
|
18
26
|
const session = auth.getSession();
|
|
19
|
-
// if user is logged in
|
|
20
27
|
if (session) {
|
|
21
28
|
utils.ctx.extensions.auth.session = session;
|
|
22
29
|
return;
|
|
23
30
|
}
|
|
31
|
+
|
|
32
|
+
const publicPaths: string[] = utils.ctx.meta?.extensions?.auth?.studio?.publicPaths ?? [];
|
|
33
|
+
if (isPublicPath(window.location.pathname, publicPaths)) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
|
|
24
37
|
await auth.logout()
|
|
25
38
|
}
|
|
39
|
+
|
|
40
|
+
export function onRouteChange(utils: ExtensionUtils, path: string) {
|
|
41
|
+
const auth = new Auth(utils);
|
|
42
|
+
if (auth.getSession()) return;
|
|
43
|
+
|
|
44
|
+
const publicPaths: string[] = utils.ctx.meta?.extensions?.auth?.studio?.publicPaths ?? [];
|
|
45
|
+
if (!isPublicPath(path, publicPaths)) {
|
|
46
|
+
utils.location.navigate(`${loginPath}?redirect=${encodeURIComponent(path)}`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -41,6 +41,17 @@ export const authConfig: Config = {
|
|
|
41
41
|
},
|
|
42
42
|
},
|
|
43
43
|
},
|
|
44
|
+
author: {
|
|
45
|
+
permissions: {
|
|
46
|
+
auth_users: {
|
|
47
|
+
read: {
|
|
48
|
+
filter: { id: ({ user }: any) => user?.id ?? null },
|
|
49
|
+
},
|
|
50
|
+
update: true,
|
|
51
|
+
delete: true,
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
},
|
|
44
55
|
},
|
|
45
56
|
}),
|
|
46
57
|
],
|
|
@@ -166,7 +166,7 @@ describe("Login", () => {
|
|
|
166
166
|
);
|
|
167
167
|
await response2.json();
|
|
168
168
|
|
|
169
|
-
expect(response2.status).toEqual(
|
|
169
|
+
expect(response2.status).not.toEqual(200);
|
|
170
170
|
});
|
|
171
171
|
|
|
172
172
|
it("should prevent the test user to mutate another user", async () => {
|
|
@@ -248,7 +248,7 @@ describe("Login", () => {
|
|
|
248
248
|
},
|
|
249
249
|
body: JSON.stringify({
|
|
250
250
|
data: {
|
|
251
|
-
role: "
|
|
251
|
+
role: "author",
|
|
252
252
|
},
|
|
253
253
|
}),
|
|
254
254
|
},
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { Lobb } from "@lobb-js/core";
|
|
2
|
+
import { afterAll, beforeAll, describe, it, expect } from "bun:test";
|
|
3
|
+
import { authNoRolesConfig } from "../configs/auth_no_roles.ts";
|
|
4
|
+
|
|
5
|
+
describe("Admin user protection", () => {
|
|
6
|
+
let lobb: Lobb;
|
|
7
|
+
let baseUrl: string;
|
|
8
|
+
const authHeader = new Headers();
|
|
9
|
+
|
|
10
|
+
beforeAll(async () => {
|
|
11
|
+
lobb = await Lobb.init(authNoRolesConfig);
|
|
12
|
+
baseUrl = `http://127.0.0.1:${lobb.webServer.port}`;
|
|
13
|
+
|
|
14
|
+
const response = await fetch(
|
|
15
|
+
`${baseUrl}/api/collections/auth_sessions`,
|
|
16
|
+
{
|
|
17
|
+
method: "POST",
|
|
18
|
+
headers: { "Content-Type": "application/json" },
|
|
19
|
+
body: JSON.stringify({
|
|
20
|
+
data: { email: "admin@test.com", password: "admin" },
|
|
21
|
+
}),
|
|
22
|
+
},
|
|
23
|
+
);
|
|
24
|
+
const result = await response.json();
|
|
25
|
+
authHeader.append("Authorization", `Bearer ${result.data.access_token.token}`);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
afterAll(async () => {
|
|
29
|
+
await lobb.close();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("should prevent deleting the admin user", async () => {
|
|
33
|
+
const response = await fetch(
|
|
34
|
+
`${baseUrl}/api/collections/auth_users/1`,
|
|
35
|
+
{ method: "DELETE", headers: authHeader },
|
|
36
|
+
);
|
|
37
|
+
const result = await response.json();
|
|
38
|
+
|
|
39
|
+
expect(response.status).toEqual(403);
|
|
40
|
+
expect(result.message).toEqual("The admin user cannot be deleted.");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("should prevent updating the admin user", async () => {
|
|
44
|
+
const response = await fetch(
|
|
45
|
+
`${baseUrl}/api/collections/auth_users/1`,
|
|
46
|
+
{
|
|
47
|
+
method: "PATCH",
|
|
48
|
+
headers: authHeader,
|
|
49
|
+
body: JSON.stringify({
|
|
50
|
+
data: { email: "hacked@example.com" },
|
|
51
|
+
}),
|
|
52
|
+
},
|
|
53
|
+
);
|
|
54
|
+
const result = await response.json();
|
|
55
|
+
|
|
56
|
+
expect(response.status).toEqual(403);
|
|
57
|
+
expect(result.message).toEqual("The admin user cannot be updated.");
|
|
58
|
+
});
|
|
59
|
+
});
|
|
@@ -38,7 +38,7 @@ describe("Prevent Guard For Public Users", () => {
|
|
|
38
38
|
|
|
39
39
|
expect(
|
|
40
40
|
result.message,
|
|
41
|
-
).toEqual("
|
|
41
|
+
).toEqual("You do not have permission to perform this action.");
|
|
42
42
|
expect(response.status).toEqual(403);
|
|
43
43
|
});
|
|
44
44
|
|
|
@@ -54,7 +54,7 @@ describe("Prevent Guard For Public Users", () => {
|
|
|
54
54
|
|
|
55
55
|
expect(
|
|
56
56
|
result.message,
|
|
57
|
-
).toEqual("
|
|
57
|
+
).toEqual("You do not have permission to perform this action.");
|
|
58
58
|
expect(response.status).toEqual(403);
|
|
59
59
|
});
|
|
60
60
|
|
|
@@ -81,7 +81,7 @@ describe("Prevent Guard For Public Users", () => {
|
|
|
81
81
|
).toEqual({
|
|
82
82
|
status: 403,
|
|
83
83
|
code: "FORBIDDEN",
|
|
84
|
-
message: "
|
|
84
|
+
message: "You do not have permission to perform this action.",
|
|
85
85
|
});
|
|
86
86
|
expect(response.status).toEqual(403);
|
|
87
87
|
});
|
|
@@ -100,7 +100,7 @@ describe("Prevent Guard For Public Users", () => {
|
|
|
100
100
|
result,
|
|
101
101
|
).toEqual({
|
|
102
102
|
code: "FORBIDDEN",
|
|
103
|
-
message: "
|
|
103
|
+
message: "You do not have permission to perform this action.",
|
|
104
104
|
status: 403,
|
|
105
105
|
});
|
|
106
106
|
expect(response.status).toEqual(403);
|
|
@@ -8,10 +8,9 @@ export function getBaseWorkflows(_extensionConfig: ExtensionConfig): Workflow[]
|
|
|
8
8
|
return [
|
|
9
9
|
{
|
|
10
10
|
name: "auth_preventAdminUserDeletion",
|
|
11
|
-
eventName: "core.
|
|
11
|
+
eventName: "core.service.preDeleteOne",
|
|
12
12
|
handler: async (input, ctx) => {
|
|
13
13
|
if (input.collectionName !== "auth_users") return;
|
|
14
|
-
if (input.triggeredBy !== "API") return;
|
|
15
14
|
|
|
16
15
|
if (Number(input.id) === 1) {
|
|
17
16
|
throw new ctx.LobbError({
|
|
@@ -21,6 +20,20 @@ export function getBaseWorkflows(_extensionConfig: ExtensionConfig): Workflow[]
|
|
|
21
20
|
}
|
|
22
21
|
},
|
|
23
22
|
},
|
|
23
|
+
{
|
|
24
|
+
name: "auth_preventAdminUserUpdate",
|
|
25
|
+
eventName: "core.service.preUpdateOne",
|
|
26
|
+
handler: async (input, ctx) => {
|
|
27
|
+
if (input.collectionName !== "auth_users") return;
|
|
28
|
+
|
|
29
|
+
if (Number(input.id) === 1) {
|
|
30
|
+
throw new ctx.LobbError({
|
|
31
|
+
code: "FORBIDDEN",
|
|
32
|
+
message: "The admin user cannot be updated.",
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
},
|
|
24
37
|
...baseWorkflows,
|
|
25
38
|
];
|
|
26
39
|
}
|
|
@@ -40,6 +40,12 @@ export function handlePolicy({
|
|
|
40
40
|
const currentRole = config.roles?.[role];
|
|
41
41
|
|
|
42
42
|
if (typeof currentRole === "undefined") {
|
|
43
|
+
if (role === "public") {
|
|
44
|
+
throw new ctx.LobbError({
|
|
45
|
+
code: "FORBIDDEN",
|
|
46
|
+
message: "You do not have permission to perform this action.",
|
|
47
|
+
});
|
|
48
|
+
}
|
|
43
49
|
throw new ctx.LobbError({
|
|
44
50
|
code: "FORBIDDEN",
|
|
45
51
|
message: "Your role is not recognized by the system.",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lobb-js/lobb-ext-auth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"license": "UNLICENSED",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"publishConfig": {
|
|
@@ -33,12 +33,12 @@
|
|
|
33
33
|
"package": "svelte-package --input extensions/auth/studio"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@lobb-js/core": "^0.
|
|
36
|
+
"@lobb-js/core": "^0.28.0",
|
|
37
37
|
"argon2": "^0.40.3",
|
|
38
38
|
"hono": "^4.7.0"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@lobb-js/studio": "^0.
|
|
41
|
+
"@lobb-js/studio": "^0.25.0",
|
|
42
42
|
"@lucide/svelte": "^0.563.1",
|
|
43
43
|
"@playwright/test": "^1.58.2",
|
|
44
44
|
"@sveltejs/adapter-node": "^5.5.4",
|