@lobb-js/lobb-ext-auth 0.12.0 → 0.14.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.
Files changed (51) hide show
  1. package/dist/auth.js +3 -3
  2. package/dist/lib/components/pages/loginPage/index.svelte +1 -1
  3. package/dist/lib/components/pages/settings/pages/activityFeed.svelte +1 -1
  4. package/dist/lib/components/pages/settings/pages/rolesAndPermissions.svelte +1 -1
  5. package/dist/lib/components/pages/settings/pages/users.svelte +1 -1
  6. package/extensions/auth/database/init.ts +3 -3
  7. package/extensions/auth/index.ts +2 -0
  8. package/extensions/auth/openapi.ts +3 -3
  9. package/extensions/auth/studio/auth.ts +3 -3
  10. package/extensions/auth/studio/lib/components/pages/loginPage/index.svelte +1 -1
  11. package/extensions/auth/studio/lib/components/pages/settings/pages/activityFeed.svelte +1 -1
  12. package/extensions/auth/studio/lib/components/pages/settings/pages/rolesAndPermissions.svelte +1 -1
  13. package/extensions/auth/studio/lib/components/pages/settings/pages/users.svelte +1 -1
  14. package/extensions/auth/workflows/actionController.ts +34 -0
  15. package/extensions/auth/workflows/actions.ts +89 -0
  16. package/extensions/auth/workflows/baseWorkflow.ts +13 -120
  17. package/extensions/auth/workflows/index.ts +2 -0
  18. package/extensions/auth/workflows/sharesWorkflows.ts +10 -9
  19. package/package.json +5 -5
  20. package/extensions/auth/tests/collections/extend_users_collection.test.ts +0 -61
  21. package/extensions/auth/tests/collections/shares.test.ts +0 -657
  22. package/extensions/auth/tests/configs/auth.ts +0 -101
  23. package/extensions/auth/tests/configs/auth_no_roles.ts +0 -65
  24. package/extensions/auth/tests/configs/auth_public_full_access.ts +0 -69
  25. package/extensions/auth/tests/configs/auth_with_admin_extra_fields.ts +0 -53
  26. package/extensions/auth/tests/configs/auth_with_different_admin_creds.ts +0 -81
  27. package/extensions/auth/tests/configs/auth_with_extend_users.ts +0 -79
  28. package/extensions/auth/tests/configs/auth_with_refresh_token.ts +0 -86
  29. package/extensions/auth/tests/configs/auth_with_short_access_token_only.ts +0 -95
  30. package/extensions/auth/tests/configs/auth_with_short_time_refresh_token.ts +0 -86
  31. package/extensions/auth/tests/configs/social_blog.ts +0 -146
  32. package/extensions/auth/tests/controllers/change_password.test.ts +0 -113
  33. package/extensions/auth/tests/controllers/dashboardAccessRoles.test.ts +0 -29
  34. package/extensions/auth/tests/controllers/login.test.ts +0 -101
  35. package/extensions/auth/tests/controllers/logout.test.ts +0 -89
  36. package/extensions/auth/tests/controllers/me.test.ts +0 -376
  37. package/extensions/auth/tests/controllers/register.test.ts +0 -45
  38. package/extensions/auth/tests/database/adminExtraFields.test.ts +0 -50
  39. package/extensions/auth/tests/database/db.test.ts +0 -64
  40. package/extensions/auth/tests/database/differentAdminCreds.test.ts +0 -51
  41. package/extensions/auth/tests/middlewares/adminAuthGuard.test.ts +0 -157
  42. package/extensions/auth/tests/middlewares/adminProtection.test.ts +0 -59
  43. package/extensions/auth/tests/middlewares/publicAllowBasic.test.ts +0 -137
  44. package/extensions/auth/tests/middlewares/publicPreventBasic.test.ts +0 -108
  45. package/extensions/auth/tests/permissions.test.ts +0 -127
  46. package/extensions/auth/tests/socialBlog.test.ts +0 -253
  47. package/extensions/auth/tests/utils/addArticles.ts +0 -22
  48. package/extensions/auth/tests/utils/addSocialBlogArticles.ts +0 -52
  49. package/extensions/auth/tests/utils/data/articles.ts +0 -65
  50. package/extensions/auth/tests/utils/data/socialBlogArticles.ts +0 -56
  51. package/extensions/auth/tests/workflows/shareIntersection.test.ts +0 -158
@@ -1,376 +0,0 @@
1
- import { Lobb } from "@lobb-js/core";
2
- import { afterAll, beforeAll, describe, it, expect } from "bun:test";
3
- import { authConfig } from "../configs/auth.ts";
4
-
5
- describe("Login", () => {
6
- let lobb: Lobb;
7
- let baseUrl: string;
8
- let adminUserId: number;
9
-
10
- beforeAll(async () => {
11
- lobb = await Lobb.init(authConfig);
12
- baseUrl = `http://127.0.0.1:${lobb.webServer.port}`;
13
- adminUserId = (await lobb.collectionService.findAll({
14
- collectionName: "auth_users",
15
- params: {
16
- filter: {
17
- email: "admin@test.com",
18
- },
19
- },
20
- })).data[0].id;
21
- });
22
-
23
- afterAll(async () => {
24
- await lobb.collectionService.deleteMany({
25
- collectionName: "auth_sessions",
26
- });
27
- await lobb.collectionService.deleteMany({
28
- collectionName: "auth_users",
29
- });
30
- await lobb.close();
31
- });
32
-
33
- it("should return the information current user", async () => {
34
- const response = await fetch(
35
- `${baseUrl}/api/collections/auth_sessions`,
36
- {
37
- method: "POST",
38
- headers: {
39
- "Content-Type": "application/json",
40
- },
41
- body: JSON.stringify({
42
- data: {
43
- email: "admin@test.com",
44
- password: "admin",
45
- },
46
- }),
47
- },
48
- );
49
- const result = await response.json();
50
- const sessionToken = result.data.access_token.token;
51
-
52
- const headers = new Headers();
53
- headers.append(
54
- "Authorization",
55
- `Bearer ${sessionToken}`,
56
- );
57
-
58
- const response2 = await fetch(
59
- `${baseUrl}/api/collections/auth_users/me`,
60
- {
61
- method: "GET",
62
- headers: headers,
63
- },
64
- );
65
- const data2 = await response2.json();
66
-
67
- expect(data2.data).toMatchObject({
68
- email: "admin@test.com",
69
- role: "admin",
70
- });
71
- });
72
-
73
- it("should return correct response for a public user", async () => {
74
- const response = await fetch(
75
- `${baseUrl}/api/collections/auth_users/me`,
76
- );
77
- const data = await response.json();
78
-
79
- expect(data).toMatchObject({
80
- code: "BAD_REQUEST",
81
- message: "You need to provide an access token.",
82
- status: 400,
83
- });
84
- });
85
-
86
- it("should allow the user to get his information", async () => {
87
- // create a new test user
88
- const response = await fetch(
89
- `${baseUrl}/api/collections/auth_users`,
90
- {
91
- method: "POST",
92
- headers: {
93
- "Content-Type": "application/json",
94
- },
95
- body: JSON.stringify({
96
- data: {
97
- email: "test@example.com",
98
- password: "test_password",
99
- role: "author",
100
- },
101
- }),
102
- },
103
- );
104
- const result = await response.json();
105
-
106
- // login with that user
107
- const loginResponse = await fetch(
108
- `${baseUrl}/api/collections/auth_sessions`,
109
- {
110
- method: "POST",
111
- headers: {
112
- "Content-Type": "application/json",
113
- },
114
- body: JSON.stringify({
115
- data: {
116
- email: "test@example.com",
117
- password: "test_password",
118
- },
119
- }),
120
- },
121
- );
122
- const loginResult = await loginResponse.json();
123
-
124
- // test if the user can get his data
125
- const sessionToken = loginResult.data.access_token.token;
126
- const response2 = await fetch(
127
- `${baseUrl}/api/collections/auth_users/me`,
128
- {
129
- headers: {
130
- "Authorization": `Bearer ${sessionToken}`,
131
- },
132
- },
133
- );
134
- const data = await response2.json();
135
-
136
- expect(response2.status).toEqual(200);
137
- });
138
-
139
- it("should prevent the test user from accessing the admin user", async () => {
140
- // login with that user
141
- const loginResponse = await fetch(
142
- `${baseUrl}/api/collections/auth_sessions`,
143
- {
144
- method: "POST",
145
- headers: {
146
- "Content-Type": "application/json",
147
- },
148
- body: JSON.stringify({
149
- data: {
150
- email: "test@example.com",
151
- password: "test_password",
152
- },
153
- }),
154
- },
155
- );
156
- const loginResult = await loginResponse.json();
157
-
158
- const sessionToken = loginResult.data.access_token.token;
159
- const response2 = await fetch(
160
- `${baseUrl}/api/collections/auth_users/${adminUserId}`,
161
- {
162
- headers: {
163
- "Authorization": `Bearer ${sessionToken}`,
164
- },
165
- },
166
- );
167
- await response2.json();
168
-
169
- expect(response2.status).not.toEqual(200);
170
- });
171
-
172
- it("should prevent the test user to mutate another user", async () => {
173
- // login with that user
174
- const loginResponse = await fetch(
175
- `${baseUrl}/api/collections/auth_sessions`,
176
- {
177
- method: "POST",
178
- headers: {
179
- "Content-Type": "application/json",
180
- },
181
- body: JSON.stringify({
182
- data: {
183
- email: "test@example.com",
184
- password: "test_password",
185
- },
186
- }),
187
- },
188
- );
189
- const loginResult = await loginResponse.json();
190
-
191
- const sessionToken = loginResult.data.access_token.token;
192
- const response2 = await fetch(
193
- `${baseUrl}/api/collections/auth_users/${adminUserId}`,
194
- {
195
- method: "PATCH",
196
- headers: {
197
- "Authorization": `Bearer ${sessionToken}`,
198
- },
199
- body: JSON.stringify({
200
- data: {
201
- password: "test_password",
202
- },
203
- }),
204
- },
205
- );
206
- await response2.json();
207
- expect(response2.status).toEqual(403);
208
-
209
- const response3 = await fetch(
210
- `${baseUrl}/api/collections/auth_users/${adminUserId}?force`,
211
- {
212
- method: "DELETE",
213
- headers: {
214
- "Authorization": `Bearer ${sessionToken}`,
215
- },
216
- },
217
- );
218
- await response3.json();
219
- expect(response3.status).toEqual(403);
220
- });
221
-
222
- it("should allow current test user to update or delete his user", async () => {
223
- // login with that user
224
- const loginResponse = await fetch(
225
- `${baseUrl}/api/collections/auth_sessions`,
226
- {
227
- method: "POST",
228
- headers: {
229
- "Content-Type": "application/json",
230
- },
231
- body: JSON.stringify({
232
- data: {
233
- email: "test@example.com",
234
- password: "test_password",
235
- },
236
- }),
237
- },
238
- );
239
- const loginResult = await loginResponse.json();
240
-
241
- const sessionToken = loginResult.data.access_token.token;
242
- const response2 = await fetch(
243
- `${baseUrl}/api/collections/auth_users/me`,
244
- {
245
- method: "PATCH",
246
- headers: {
247
- "Authorization": `Bearer ${sessionToken}`,
248
- },
249
- body: JSON.stringify({
250
- data: {
251
- role: "author",
252
- },
253
- }),
254
- },
255
- );
256
- await response2.json();
257
- expect(response2.status).toEqual(200);
258
-
259
- const response3 = await fetch(
260
- `${baseUrl}/api/collections/auth_users/me?force`,
261
- {
262
- method: "DELETE",
263
- headers: {
264
- "Authorization": `Bearer ${sessionToken}`,
265
- },
266
- },
267
- );
268
- const result = await response3.json();
269
-
270
- expect(response3.status).toEqual(200);
271
- });
272
-
273
- describe("permissions in the /me response", () => {
274
- async function login(email: string, password: string): Promise<string> {
275
- const res = await fetch(`${baseUrl}/api/collections/auth_sessions`, {
276
- method: "POST",
277
- headers: { "Content-Type": "application/json" },
278
- body: JSON.stringify({ data: { email, password } }),
279
- });
280
- const body = await res.json();
281
- return body.data.access_token.token;
282
- }
283
-
284
- async function createUser(email: string, password: string, role: string) {
285
- await fetch(`${baseUrl}/api/collections/auth_users`, {
286
- method: "POST",
287
- headers: { "Content-Type": "application/json" },
288
- body: JSON.stringify({ data: { email, password, role } }),
289
- });
290
- }
291
-
292
- it("should attach the role's permissions as a sibling of data for the current user via /me", async () => {
293
- await createUser("perm_author@example.com", "pw", "author");
294
- const token = await login("perm_author@example.com", "pw");
295
-
296
- const res = await fetch(`${baseUrl}/api/collections/auth_users/me`, {
297
- headers: { "Authorization": `Bearer ${token}` },
298
- });
299
- const body = await res.json();
300
-
301
- expect(res.status).toEqual(200);
302
- expect(body.data.permissions).toBeUndefined();
303
- // author role config has auth_users.read/update/delete — function-typed
304
- // filters get stripped to plain JSON before being returned.
305
- expect(body.permissions).toEqual({
306
- auth_users: {
307
- read: { filter: {} },
308
- update: true,
309
- delete: true,
310
- },
311
- articles: {
312
- read: {
313
- filter: {},
314
- fields: { id: true, title: true, body: true },
315
- },
316
- create: { fields: { title: true, body: true } },
317
- },
318
- auth_shares: {
319
- create: true,
320
- },
321
- });
322
- });
323
-
324
- it("should not attach permissions when reading another user's row", async () => {
325
- // login as admin so we can read other users
326
- const adminToken = await login("admin@test.com", "admin");
327
-
328
- // fetch a non-admin user's row by id
329
- const otherUser = (await lobb.collectionService.findAll({
330
- collectionName: "auth_users",
331
- params: { filter: { email: "perm_author@example.com" } },
332
- })).data[0];
333
-
334
- const res = await fetch(
335
- `${baseUrl}/api/collections/auth_users/${otherUser.id}`,
336
- { headers: { "Authorization": `Bearer ${adminToken}` } },
337
- );
338
- const body = await res.json();
339
-
340
- expect(res.status).toEqual(200);
341
- expect(body.permissions).toBeUndefined();
342
- });
343
-
344
- it("should not attach permissions when the current user reads their own row by id (not via /me)", async () => {
345
- const token = await login("perm_author@example.com", "pw");
346
- const ownUser = (await lobb.collectionService.findAll({
347
- collectionName: "auth_users",
348
- params: { filter: { email: "perm_author@example.com" } },
349
- })).data[0];
350
-
351
- const res = await fetch(
352
- `${baseUrl}/api/collections/auth_users/${ownUser.id}`,
353
- { headers: { "Authorization": `Bearer ${token}` } },
354
- );
355
- const body = await res.json();
356
-
357
- expect(res.status).toEqual(200);
358
- expect(body.permissions).toBeUndefined();
359
- });
360
-
361
- it("should allow a user whose role has no auth_users.read permission to fetch /me", async () => {
362
- // The "reader" role in authConfig has only articles.read — no auth_users
363
- // perm at all. /me should still work because the user is reading their
364
- // own row, but historically the internal findOne→findAll re-checked the
365
- // policy on preFindAll and threw 403 for roles lacking auth_users.read.
366
- await createUser("no_auth_perm@example.com", "pw", "reader");
367
- const token = await login("no_auth_perm@example.com", "pw");
368
-
369
- const res = await fetch(`${baseUrl}/api/collections/auth_users/me`, {
370
- headers: { "Authorization": `Bearer ${token}` },
371
- });
372
-
373
- expect(res.status).toEqual(200);
374
- });
375
- });
376
- });
@@ -1,45 +0,0 @@
1
- import { Lobb } from "@lobb-js/core";
2
- import { afterAll, beforeAll, describe, it, expect } from "bun:test";
3
- import { authConfig } from "../configs/auth.ts";
4
-
5
- describe("Register", () => {
6
- let lobb: Lobb;
7
- let baseUrl: string;
8
-
9
- beforeAll(async () => {
10
- lobb = await Lobb.init(authConfig);
11
- baseUrl = `http://127.0.0.1:${lobb.webServer.port}`;
12
- await lobb.collectionService.deleteMany({
13
- collectionName: "auth_sessions",
14
- });
15
- await lobb.collectionService.deleteMany({
16
- collectionName: "auth_users",
17
- });
18
- });
19
-
20
- afterAll(async () => {
21
- await lobb.close();
22
- });
23
-
24
- it("should create a new user", async () => {
25
- const response = await fetch(
26
- `${baseUrl}/api/collections/auth_users`,
27
- {
28
- method: "POST",
29
- headers: {
30
- "Content-Type": "application/json",
31
- },
32
- body: JSON.stringify({
33
- data: {
34
- email: "test@example.com",
35
- password: "test_password",
36
- role: "author",
37
- },
38
- }),
39
- },
40
- );
41
- await response.json();
42
-
43
- expect(response.status).toEqual(201);
44
- });
45
- });
@@ -1,50 +0,0 @@
1
- import { Lobb } from "@lobb-js/core";
2
- import { afterAll, beforeAll, describe, it, expect } from "bun:test";
3
- import { authWithAdminExtraFieldsConfig } from "../configs/auth_with_admin_extra_fields.ts";
4
-
5
- describe("AUTH admin extra fields", () => {
6
- let lobb: Lobb;
7
-
8
- beforeAll(async () => {
9
- lobb = await Lobb.init(authWithAdminExtraFieldsConfig);
10
- });
11
-
12
- afterAll(async () => {
13
- await lobb.close();
14
- });
15
-
16
- it("should create the admin user with extra fields on first init", async () => {
17
- const result = await lobb.collectionService.findAll({
18
- collectionName: "auth_users",
19
- params: { filter: { role: "admin" } },
20
- });
21
-
22
- const admin = result.data[0];
23
- expect(admin).toBeDefined();
24
- expect(admin.email).toEqual("admin@test.com");
25
- expect(admin.role).toEqual("admin");
26
- expect(admin.name).toEqual("Super Admin");
27
- expect(admin.username).toEqual("superadmin");
28
- });
29
-
30
- it("should always set role to admin regardless of extra fields", async () => {
31
- const result = await lobb.collectionService.findAll({
32
- collectionName: "auth_users",
33
- params: { filter: { role: "admin" } },
34
- });
35
-
36
- expect(result.data[0].role).toEqual("admin");
37
- });
38
-
39
- it("should not create a second admin user on re-init", async () => {
40
- const lobb2 = await Lobb.init(authWithAdminExtraFieldsConfig);
41
-
42
- const result = await lobb2.collectionService.findAll({
43
- collectionName: "auth_users",
44
- params: { filter: { role: "admin" } },
45
- });
46
-
47
- expect(result.data.length).toEqual(1);
48
- await lobb2.close();
49
- });
50
- });
@@ -1,64 +0,0 @@
1
- import { Lobb } from "@lobb-js/core";
2
- import { afterAll, beforeAll, describe, it, expect } from "bun:test";
3
- import { authConfig } from "../configs/auth.ts";
4
-
5
- describe("AUTH", () => {
6
- let lobb: Lobb;
7
- let baseUrl: string;
8
-
9
- beforeAll(async () => {
10
- lobb = await Lobb.init(authConfig);
11
- baseUrl = `http://127.0.0.1:${lobb.webServer.port}`;
12
- await lobb.collectionService.deleteMany({
13
- collectionName: "auth_sessions",
14
- });
15
- });
16
-
17
- afterAll(async () => {
18
- await lobb.collectionService.deleteMany({
19
- collectionName: "auth_sessions",
20
- });
21
- await lobb.collectionService.deleteMany({
22
- collectionName: "auth_users",
23
- });
24
- await lobb.close();
25
- });
26
-
27
- it("should get the users", async () => {
28
- const result = await lobb.collectionService.findAll({
29
- collectionName: "auth_users",
30
- });
31
-
32
- expect(result.data.length).toBeTruthy();
33
- });
34
-
35
- it("should get the admin user", async () => {
36
- const result = await lobb.collectionService.findAll({
37
- collectionName: "auth_users",
38
- });
39
-
40
- expect(result.data[0].email).toEqual("admin@test.com");
41
- expect(result.data[0].role).toEqual("admin");
42
- });
43
-
44
- it("should make sure the admin password in the config is the same one in the database", async () => {
45
- const response = await fetch(
46
- `${baseUrl}/api/collections/auth_sessions`,
47
- {
48
- method: "POST",
49
- headers: {
50
- "Content-Type": "application/json",
51
- },
52
- body: JSON.stringify({
53
- data: {
54
- email: "admin@test.com",
55
- password: "admin",
56
- },
57
- }),
58
- },
59
- );
60
-
61
- await response.json();
62
- expect(response.status).toEqual(200);
63
- });
64
- });
@@ -1,51 +0,0 @@
1
- import { Lobb } from "@lobb-js/core";
2
- import { afterAll, beforeAll, describe, it, expect } from "bun:test";
3
- import { authWithDifferentAdminCredsConfig } from "../configs/auth_with_different_admin_creds.ts";
4
-
5
- describe("AUTH with different admin creds", () => {
6
- let lobb: Lobb;
7
- let baseUrl: string;
8
-
9
- beforeAll(async () => {
10
- lobb = await Lobb.init(authWithDifferentAdminCredsConfig);
11
- baseUrl = `http://127.0.0.1:${lobb.webServer.port}`;
12
- });
13
-
14
- afterAll(async () => {
15
- await lobb.close();
16
- });
17
-
18
- it("should make sure the admin user's email is changed in the db if its changed in the config", async () => {
19
- const response = await fetch(
20
- `${baseUrl}/api/collections/auth_users`,
21
- {
22
- method: "GET",
23
- },
24
- );
25
- const json = await response.json();
26
-
27
- expect(json.data[0].email).toEqual("admin@wow.yey");
28
- expect(json.data[0].role).toEqual("admin");
29
- });
30
-
31
- it("it should make sure the admin user's password is changed in the db if it's changed in the config", async () => {
32
- const response = await fetch(
33
- `${baseUrl}/api/collections/auth_sessions`,
34
- {
35
- method: "POST",
36
- headers: {
37
- "Content-Type": "application/json",
38
- },
39
- body: JSON.stringify({
40
- data: {
41
- email: "admin@wow.yey",
42
- password: "123456",
43
- },
44
- }),
45
- },
46
- );
47
- await response.json();
48
-
49
- expect(response.status).toEqual(200);
50
- });
51
- });