@aeriajs/builtins 0.0.277 → 0.0.279

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 (53) hide show
  1. package/dist/authentication.js +6 -11
  2. package/dist/collections/file/description.js +10 -14
  3. package/dist/collections/file/download.js +21 -25
  4. package/dist/collections/file/index.js +15 -18
  5. package/dist/collections/file/insert.js +8 -12
  6. package/dist/collections/file/remove.js +7 -11
  7. package/dist/collections/file/removeAll.js +5 -9
  8. package/dist/collections/index.js +4 -20
  9. package/dist/collections/log/index.js +5 -8
  10. package/dist/collections/resourceUsage/index.js +2 -5
  11. package/dist/collections/user/activate.js +31 -35
  12. package/dist/collections/user/authenticate.js +29 -33
  13. package/dist/collections/user/createAccount.js +25 -29
  14. package/dist/collections/user/description.js +5 -8
  15. package/dist/collections/user/editProfile.js +12 -16
  16. package/dist/collections/user/getActivationLink.js +21 -26
  17. package/dist/collections/user/getCurrentUser.js +12 -16
  18. package/dist/collections/user/getInfo.js +22 -26
  19. package/dist/collections/user/getRedefinePasswordLink.js +15 -19
  20. package/dist/collections/user/index.d.ts +10 -10
  21. package/dist/collections/user/index.js +39 -42
  22. package/dist/collections/user/insert.js +16 -20
  23. package/dist/collections/user/redefinePassword.js +31 -35
  24. package/dist/functions/describe.js +21 -25
  25. package/dist/functions/index.js +1 -17
  26. package/dist/index.d.ts +15 -15
  27. package/dist/index.js +11 -29
  28. package/package.json +11 -17
  29. package/dist/authentication.mjs +0 -58
  30. package/dist/collections/file/description.mjs +0 -75
  31. package/dist/collections/file/download.mjs +0 -115
  32. package/dist/collections/file/index.mjs +0 -59
  33. package/dist/collections/file/insert.mjs +0 -44
  34. package/dist/collections/file/remove.mjs +0 -21
  35. package/dist/collections/file/removeAll.mjs +0 -22
  36. package/dist/collections/index.mjs +0 -5
  37. package/dist/collections/log/index.mjs +0 -55
  38. package/dist/collections/resourceUsage/index.mjs +0 -39
  39. package/dist/collections/user/activate.mjs +0 -119
  40. package/dist/collections/user/authenticate.mjs +0 -165
  41. package/dist/collections/user/createAccount.mjs +0 -93
  42. package/dist/collections/user/description.mjs +0 -149
  43. package/dist/collections/user/editProfile.mjs +0 -52
  44. package/dist/collections/user/getActivationLink.mjs +0 -88
  45. package/dist/collections/user/getCurrentUser.mjs +0 -57
  46. package/dist/collections/user/getInfo.mjs +0 -85
  47. package/dist/collections/user/getRedefinePasswordLink.mjs +0 -63
  48. package/dist/collections/user/index.mjs +0 -71
  49. package/dist/collections/user/insert.mjs +0 -70
  50. package/dist/collections/user/redefinePassword.mjs +0 -110
  51. package/dist/functions/describe.mjs +0 -95
  52. package/dist/functions/index.mjs +0 -2
  53. package/dist/index.mjs +0 -21
@@ -1,63 +0,0 @@
1
- "use strict";
2
- import { Result, HTTPStatus, resultSchema, functionSchemas, endpointErrorSchema, defineContract } from "@aeriajs/types";
3
- import { RedefinePasswordError } from "./redefinePassword.mjs";
4
- import { getActivationToken } from "./getActivationLink.mjs";
5
- export const getRedefinePasswordLinkContract = defineContract({
6
- payload: {
7
- type: "object",
8
- required: ["userId"],
9
- properties: {
10
- userId: {
11
- type: "string",
12
- format: "objectid"
13
- },
14
- redirect: {
15
- type: "string"
16
- }
17
- }
18
- },
19
- response: [
20
- functionSchemas.getError(),
21
- endpointErrorSchema({
22
- httpStatus: [HTTPStatus.Forbidden],
23
- code: [RedefinePasswordError.UserNotActive]
24
- }),
25
- resultSchema({
26
- type: "object",
27
- properties: {
28
- url: {
29
- type: "string"
30
- }
31
- }
32
- })
33
- ]
34
- });
35
- export const getRedefinePasswordLink = async (payload, context) => {
36
- if (!context.config.webPublicUrl) {
37
- throw new Error("config.webPublicUrl is not set");
38
- }
39
- const { error, result: user } = await context.collections.user.functions.get({
40
- filters: {
41
- _id: payload.userId
42
- },
43
- project: ["active"]
44
- });
45
- if (error) {
46
- return Result.error(error);
47
- }
48
- if (!user.active) {
49
- return context.error(HTTPStatus.Forbidden, {
50
- code: RedefinePasswordError.UserNotActive
51
- });
52
- }
53
- const redefineToken = await getActivationToken(payload.userId.toString(), context);
54
- const url = new URL(`${context.config.webPublicUrl}/user/redefine-password`);
55
- url.searchParams.set("step", "password"), url.searchParams.set("u", payload.userId.toString());
56
- url.searchParams.set("t", redefineToken);
57
- if (payload.redirect) {
58
- url.searchParams.set("next", payload.redirect);
59
- }
60
- return Result.result({
61
- url: url.toString()
62
- });
63
- };
@@ -1,71 +0,0 @@
1
- "use strict";
2
- import { defineCollection, get, getAll, remove, upload, removeFile } from "@aeriajs/core";
3
- import { description } from "./description.mjs";
4
- import { authenticate, authenticateContract } from "./authenticate.mjs";
5
- import { activate, activateContract } from "./activate.mjs";
6
- import { insert } from "./insert.mjs";
7
- import { createAccount, createAccountContract } from "./createAccount.mjs";
8
- import { getInfo, getInfoContract } from "./getInfo.mjs";
9
- import { getCurrentUser, getCurrentUserContract } from "./getCurrentUser.mjs";
10
- import { getActivationLink, getActivationLinkContract } from "./getActivationLink.mjs";
11
- import { redefinePassword, redefinePasswordContract } from "./redefinePassword.mjs";
12
- import { getRedefinePasswordLink, getRedefinePasswordLinkContract } from "./getRedefinePasswordLink.mjs";
13
- import { editProfile, editProfileContract } from "./editProfile.mjs";
14
- const functions = {
15
- get,
16
- getAll,
17
- remove,
18
- upload,
19
- removeFile,
20
- insert,
21
- editProfile,
22
- authenticate,
23
- activate,
24
- createAccount,
25
- getInfo,
26
- getCurrentUser,
27
- getActivationLink,
28
- getRedefinePasswordLink,
29
- redefinePassword
30
- };
31
- const exposedFunctions = {
32
- get: true,
33
- getAll: ["root"],
34
- remove: ["root"],
35
- upload: true,
36
- removeFile: true,
37
- insert: ["root"],
38
- editProfile: true,
39
- authenticate: "unauthenticated",
40
- activate: [
41
- "unauthenticated",
42
- "root"
43
- ],
44
- createAccount: "unauthenticated",
45
- getInfo: "unauthenticated",
46
- getCurrentUser: true,
47
- getActivationLink: ["root"],
48
- getRedefinePasswordLink: ["root"],
49
- redefinePassword: [
50
- "unauthenticated",
51
- "root"
52
- ]
53
- };
54
- export const user = defineCollection({
55
- description,
56
- functions
57
- });
58
- Object.assign(user, {
59
- exposedFunctions,
60
- contracts: {
61
- activate: activateContract,
62
- authenticate: authenticateContract,
63
- createAccount: createAccountContract,
64
- editProfile: editProfileContract,
65
- getActivationLink: getActivationLinkContract,
66
- getCurrentUser: getCurrentUserContract,
67
- getInfo: getInfoContract,
68
- getRedefinePasswordLink: getRedefinePasswordLinkContract,
69
- redefinePassword: redefinePasswordContract
70
- }
71
- });
@@ -1,70 +0,0 @@
1
- "use strict";
2
- import { HTTPStatus, ACError } from "@aeriajs/types";
3
- import { arraysIntersect } from "@aeriajs/common";
4
- import { ObjectId, insert as originalInsert } from "@aeriajs/core";
5
- import * as bcrypt from "bcryptjs";
6
- const isRoleAllowed = (targetRole, context) => {
7
- if (!context.config.security.rolesHierarchy) {
8
- return true;
9
- }
10
- if (!context.token.authenticated) {
11
- throw new Error();
12
- }
13
- for (const role of context.token.roles) {
14
- if (role in context.config.security.rolesHierarchy) {
15
- const hierarchy = context.config.security.rolesHierarchy[role];
16
- if (!hierarchy) {
17
- continue;
18
- }
19
- if (hierarchy === true || hierarchy.includes(targetRole)) {
20
- return true;
21
- }
22
- }
23
- }
24
- return false;
25
- };
26
- export const insert = async (payload, context) => {
27
- if (!context.token.authenticated) {
28
- throw new Error();
29
- }
30
- if ("roles" in payload.what) {
31
- if (context.config.security.rolesHierarchy) {
32
- if (!arraysIntersect(context.token.roles, Object.keys(context.config.security.rolesHierarchy))) {
33
- return context.error(HTTPStatus.Forbidden, {
34
- code: ACError.AuthorizationError,
35
- message: "user is not allowed to edit other users roles"
36
- });
37
- }
38
- if (Array.isArray(payload.what.roles)) {
39
- const allowed = payload.what.roles.every((role) => isRoleAllowed(role, context));
40
- if (!allowed) {
41
- return context.error(HTTPStatus.Forbidden, {
42
- code: ACError.AuthorizationError,
43
- message: "tried to set unallowed roles"
44
- });
45
- }
46
- }
47
- }
48
- }
49
- if ("_id" in payload.what && typeof payload.what._id === "string") {
50
- const user = await context.collections.user.model.findOne({
51
- _id: new ObjectId(payload.what._id)
52
- });
53
- if (!user) {
54
- return context.error(HTTPStatus.NotFound, {
55
- code: ACError.ResourceNotFound
56
- });
57
- }
58
- const allowed = user.roles.every((role) => isRoleAllowed(role, context));
59
- if (!allowed) {
60
- return context.error(HTTPStatus.Forbidden, {
61
- code: ACError.AuthorizationError,
62
- message: "tried to edit an user with a role higher in the hierarchy"
63
- });
64
- }
65
- }
66
- if ("password" in payload.what && typeof payload.what.password === "string") {
67
- payload.what.password = await bcrypt.hash(payload.what.password, 10);
68
- }
69
- return originalInsert(payload, context);
70
- };
@@ -1,110 +0,0 @@
1
- "use strict";
2
- import { decodeToken, ObjectId } from "@aeriajs/core";
3
- import { Result, ACError, HTTPStatus, resultSchema, functionSchemas, endpointErrorSchema, defineContract } from "@aeriajs/types";
4
- import * as bcrypt from "bcryptjs";
5
- export const RedefinePasswordError = {
6
- UserNotFound: "USER_NOT_FOUND",
7
- UserNotActive: "USER_NOT_ACTIVE",
8
- InvalidLink: "INVALID_LINK",
9
- InvalidToken: "INVALID_TOKEN"
10
- };
11
- export const redefinePasswordContract = defineContract({
12
- payload: {
13
- type: "object",
14
- required: [],
15
- properties: {
16
- userId: {
17
- type: "string",
18
- format: "objectid"
19
- },
20
- password: {
21
- type: "string"
22
- },
23
- token: {
24
- type: "string"
25
- }
26
- }
27
- },
28
- response: [
29
- functionSchemas.getError(),
30
- endpointErrorSchema({
31
- httpStatus: [
32
- HTTPStatus.NotFound,
33
- HTTPStatus.Forbidden,
34
- HTTPStatus.Unauthorized,
35
- HTTPStatus.UnprocessableContent
36
- ],
37
- code: [
38
- ACError.MalformedInput,
39
- RedefinePasswordError.InvalidLink,
40
- RedefinePasswordError.InvalidToken,
41
- RedefinePasswordError.UserNotFound,
42
- RedefinePasswordError.UserNotActive
43
- ]
44
- }),
45
- resultSchema({
46
- type: "object",
47
- properties: {
48
- userId: {
49
- type: "string",
50
- format: "objectid"
51
- }
52
- }
53
- })
54
- ]
55
- });
56
- export const redefinePassword = async (payload, context) => {
57
- const {
58
- userId,
59
- token,
60
- password
61
- } = payload;
62
- if (!context.config.secret) {
63
- throw new Error("config.secret is not set");
64
- }
65
- if (!userId || !token) {
66
- return context.error(HTTPStatus.NotFound, {
67
- code: RedefinePasswordError.InvalidLink
68
- });
69
- }
70
- const user = await context.collection.model.findOne({
71
- _id: new ObjectId(userId)
72
- }, {
73
- projection: {
74
- password: 1,
75
- active: 1
76
- }
77
- });
78
- if (!user) {
79
- return context.error(HTTPStatus.NotFound, {
80
- code: RedefinePasswordError.UserNotFound
81
- });
82
- }
83
- if (!user.active) {
84
- return context.error(HTTPStatus.Forbidden, {
85
- code: RedefinePasswordError.UserNotActive
86
- });
87
- }
88
- const decoded = await decodeToken(token, context.config.secret);
89
- if (!decoded) {
90
- return context.error(HTTPStatus.Unauthorized, {
91
- code: RedefinePasswordError.InvalidToken
92
- });
93
- }
94
- if (!password) {
95
- return context.error(HTTPStatus.UnprocessableContent, {
96
- code: ACError.MalformedInput
97
- });
98
- }
99
- await context.collection.model.updateOne({
100
- _id: user._id
101
- }, {
102
- $set: {
103
- active: true,
104
- password: await bcrypt.hash(password, 10)
105
- }
106
- });
107
- return Result.result({
108
- userId: user._id
109
- });
110
- };
@@ -1,95 +0,0 @@
1
- "use strict";
2
- import { createContext, preloadDescription, getEndpoints } from "@aeriajs/core";
3
- import { getCollections, getAvailableRoles } from "@aeriajs/entrypoint";
4
- import { Result, ACError, HTTPStatus } from "@aeriajs/types";
5
- import { serialize, endpointError, isValidCollection } from "@aeriajs/common";
6
- import { validator } from "@aeriajs/validation";
7
- import { authenticate } from "../collections/user/authenticate.mjs";
8
- const [Payload, validatePayload] = validator({
9
- type: "object",
10
- required: [],
11
- additionalProperties: true,
12
- properties: {
13
- collections: {
14
- type: "array",
15
- items: {
16
- type: "string"
17
- }
18
- },
19
- noSerialize: {
20
- type: "boolean"
21
- },
22
- revalidate: {
23
- type: "boolean"
24
- },
25
- roles: {
26
- type: "boolean"
27
- },
28
- router: {
29
- type: "boolean"
30
- }
31
- }
32
- });
33
- export const describe = async (contextOrPayload) => {
34
- const result = {
35
- descriptions: {}
36
- };
37
- let props;
38
- if ("request" in contextOrPayload) {
39
- const { error, result: validatedPayload } = validatePayload(contextOrPayload.request.payload);
40
- if (error) {
41
- return endpointError({
42
- httpStatus: HTTPStatus.UnprocessableContent,
43
- code: ACError.MalformedInput,
44
- details: error
45
- });
46
- }
47
- props = validatedPayload;
48
- } else {
49
- props = contextOrPayload;
50
- }
51
- if ("request" in contextOrPayload && props.revalidate) {
52
- const { error, result: auth } = await authenticate({
53
- revalidate: true
54
- }, await createContext({
55
- collectionName: "user",
56
- parentContext: contextOrPayload
57
- }));
58
- if (error) {
59
- return Result.error(error);
60
- }
61
- result.auth = JSON.parse(JSON.stringify(auth));
62
- }
63
- const collections = await getCollections();
64
- const retrievedCollections = props.collections?.length ? Object.fromEntries(Object.entries(collections).filter(([key]) => props.collections.includes(key))) : collections;
65
- const descriptions = {};
66
- result.descriptions = descriptions;
67
- for (const collectionName in retrievedCollections) {
68
- const candidate = retrievedCollections[collectionName];
69
- const collection = typeof candidate === "function" ? candidate() : candidate;
70
- if (!isValidCollection(collection)) {
71
- throw new Error(`The "${collectionName}" symbol exported from the entrypoint doesn't seem like a valid collection. Make sure only collections are exported from the "import('.').collections".`);
72
- }
73
- const { description: rawDescription } = collection;
74
- const description = await preloadDescription(rawDescription);
75
- descriptions[description.$id] = description;
76
- }
77
- if (props.roles) {
78
- const userCandidate = collections.user;
79
- const userCollection = typeof userCandidate === "function" ? userCandidate() : userCandidate;
80
- const userRolesProperty = userCollection.description.properties.roles;
81
- const userRoles = "enum" in userRolesProperty.items ? userRolesProperty.items.enum : [];
82
- result.roles = Array.from(new Set(userRoles.concat(await getAvailableRoles())));
83
- if ("config" in contextOrPayload) {
84
- result.rolesHierarchy = contextOrPayload.config.security.rolesHierarchy;
85
- }
86
- }
87
- if (props.router) {
88
- result.router = await getEndpoints();
89
- }
90
- if (props.noSerialize || !("response" in contextOrPayload)) {
91
- return Result.result(result);
92
- }
93
- contextOrPayload.response.setHeader("content-type", "application/bson");
94
- return contextOrPayload.response.end(serialize(result));
95
- };
@@ -1,2 +0,0 @@
1
- "use strict";
2
- export * from "./describe.mjs";
package/dist/index.mjs DELETED
@@ -1,21 +0,0 @@
1
- "use strict";
2
- export * as builtinFunctions from "./functions/index.mjs";
3
- export * from "./collections/index.mjs";
4
- export * from "./authentication.mjs";
5
- export {
6
- insert as insertUser
7
- } from "./collections/user/insert.mjs";
8
- import {
9
- file,
10
- tempFile,
11
- log,
12
- resourceUsage,
13
- user
14
- } from "./collections/index.mjs";
15
- export const collections = {
16
- file,
17
- tempFile,
18
- log,
19
- resourceUsage,
20
- user
21
- };