@aeriajs/security 0.0.164 → 0.0.166

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.
@@ -1,4 +1,2 @@
1
- import type { Context, Collection, GetReturnType, GetAllReturnType, CountReturnType, CollectionReadPayload } from '@aeriajs/types';
2
- export declare const applyReadMiddlewares: <TContext extends Context>(payload: CollectionReadPayload, context: TContext & {
3
- collection: Collection;
4
- }, fn: (p: typeof payload, context: Context) => Promise<GetReturnType<unknown> | GetAllReturnType<unknown> | CountReturnType>) => Promise<import("@aeriajs/types").Result.Either<any, any>>;
1
+ import type { Context, GetReturnType, GetAllReturnType, CountReturnType, CollectionReadPayload } from '@aeriajs/types';
2
+ export declare const applyReadMiddlewares: <TContext extends Context>(payload: CollectionReadPayload, context: TContext, fn: (p: typeof payload, context: Context) => Promise<GetReturnType<unknown> | GetAllReturnType<unknown> | CountReturnType>) => Promise<import("@aeriajs/types").Result.Either<any, any>>;
@@ -3,14 +3,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.applyReadMiddlewares = void 0;
4
4
  const iterableMiddlewares_js_1 = require("./iterableMiddlewares.js");
5
5
  const applyReadMiddlewares = (payload, context, fn) => {
6
- if (context.collection.middlewares) {
7
- if (Array.isArray(context.collection.middlewares)) {
8
- const readMiddlewares = context.collection.middlewares.map((middleware) => middleware.beforeRead).filter((fn) => !!fn);
6
+ const { middlewares } = context.collection;
7
+ if (middlewares) {
8
+ if (Array.isArray(middlewares)) {
9
+ const readMiddlewares = middlewares.map((middleware) => middleware.beforeRead).filter((fn) => !!fn);
9
10
  const start = (0, iterableMiddlewares_js_1.iterableMiddlewares)(readMiddlewares, fn);
10
11
  return start(payload, context);
11
12
  }
12
- if (context.collection.middlewares.beforeRead) {
13
- return context.collection.middlewares.beforeRead(payload, context, fn);
13
+ if (middlewares.beforeRead) {
14
+ return middlewares.beforeRead(payload, context, fn);
14
15
  }
15
16
  }
16
17
  return fn(payload, context);
@@ -1,17 +1,18 @@
1
1
  "use strict";
2
2
  import { iterableMiddlewares } from "./iterableMiddlewares.mjs";
3
3
  export const applyReadMiddlewares = (payload, context, fn) => {
4
- if (context.collection.middlewares) {
5
- if (Array.isArray(context.collection.middlewares)) {
6
- const readMiddlewares = context.collection.middlewares.map((middleware) => middleware.beforeRead).filter((fn2) => !!fn2);
4
+ const { middlewares } = context.collection;
5
+ if (middlewares) {
6
+ if (Array.isArray(middlewares)) {
7
+ const readMiddlewares = middlewares.map((middleware) => middleware.beforeRead).filter((fn2) => !!fn2);
7
8
  const start = iterableMiddlewares(
8
9
  readMiddlewares,
9
10
  fn
10
11
  );
11
12
  return start(payload, context);
12
13
  }
13
- if (context.collection.middlewares.beforeRead) {
14
- return context.collection.middlewares.beforeRead(payload, context, fn);
14
+ if (middlewares.beforeRead) {
15
+ return middlewares.beforeRead(payload, context, fn);
15
16
  }
16
17
  }
17
18
  return fn(payload, context);
@@ -1,4 +1,2 @@
1
- import type { Context, Collection, CollectionWritePayload, InsertReturnType } from '@aeriajs/types';
2
- export declare const applyWriteMiddlewares: <TDocument, TContext extends Context>(payload: CollectionWritePayload, context: TContext & {
3
- collection: Collection;
4
- }, fn: (p: typeof payload, context: Context) => Promise<InsertReturnType<TDocument>>) => Promise<import("@aeriajs/types").Result.Either<any, any>>;
1
+ import type { Context, CollectionWritePayload, InsertReturnType } from '@aeriajs/types';
2
+ export declare const applyWriteMiddlewares: <TDocument, TContext extends Context>(payload: CollectionWritePayload, context: TContext, fn: (p: typeof payload, context: Context) => Promise<InsertReturnType<TDocument>>) => Promise<import("@aeriajs/types").Result.Either<any, any>>;
@@ -3,14 +3,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.applyWriteMiddlewares = void 0;
4
4
  const iterableMiddlewares_js_1 = require("./iterableMiddlewares.js");
5
5
  const applyWriteMiddlewares = (payload, context, fn) => {
6
- if (context.collection.middlewares) {
7
- if (Array.isArray(context.collection.middlewares)) {
8
- const writeMiddlewares = context.collection.middlewares.map((middleware) => middleware.beforeWrite).filter((fn) => !!fn);
6
+ const { middlewares } = context.collection;
7
+ if (middlewares) {
8
+ if (Array.isArray(middlewares)) {
9
+ const writeMiddlewares = middlewares.map((middleware) => middleware.beforeWrite).filter((fn) => !!fn);
9
10
  const start = (0, iterableMiddlewares_js_1.iterableMiddlewares)(writeMiddlewares, fn);
10
11
  return start(payload, context);
11
12
  }
12
- if (context.collection.middlewares.beforeWrite) {
13
- return context.collection.middlewares.beforeWrite(payload, context, fn);
13
+ if (middlewares.beforeWrite) {
14
+ return middlewares.beforeWrite(payload, context, fn);
14
15
  }
15
16
  }
16
17
  return fn(payload, context);
@@ -1,17 +1,18 @@
1
1
  "use strict";
2
2
  import { iterableMiddlewares } from "./iterableMiddlewares.mjs";
3
3
  export const applyWriteMiddlewares = (payload, context, fn) => {
4
- if (context.collection.middlewares) {
5
- if (Array.isArray(context.collection.middlewares)) {
6
- const writeMiddlewares = context.collection.middlewares.map((middleware) => middleware.beforeWrite).filter((fn2) => !!fn2);
4
+ const { middlewares } = context.collection;
5
+ if (middlewares) {
6
+ if (Array.isArray(middlewares)) {
7
+ const writeMiddlewares = middlewares.map((middleware) => middleware.beforeWrite).filter((fn2) => !!fn2);
7
8
  const start = iterableMiddlewares(
8
9
  writeMiddlewares,
9
10
  fn
10
11
  );
11
12
  return start(payload, context);
12
13
  }
13
- if (context.collection.middlewares.beforeWrite) {
14
- return context.collection.middlewares.beforeWrite(payload, context, fn);
14
+ if (middlewares.beforeWrite) {
15
+ return middlewares.beforeWrite(payload, context, fn);
15
16
  }
16
17
  }
17
18
  return fn(payload, context);
@@ -26,6 +26,12 @@ const checkImmutability = async (docId, props, context) => {
26
26
  };
27
27
  const checkImmutabilityRead = async (props, context, next) => {
28
28
  const { payload: originalPayload } = props.result;
29
+ if (Array.isArray(originalPayload.filters)) {
30
+ return props;
31
+ }
32
+ if (!(originalPayload.filters._id instanceof mongodb_1.ObjectId)) {
33
+ throw new Error;
34
+ }
29
35
  const { result: payload, error } = await checkImmutability(originalPayload.filters._id, props.result, context);
30
36
  if (error) {
31
37
  return types_1.Result.error(error);
@@ -22,6 +22,12 @@ const checkImmutability = async (docId, props, context) => {
22
22
  };
23
23
  export const checkImmutabilityRead = async (props, context, next) => {
24
24
  const { payload: originalPayload } = props.result;
25
+ if (Array.isArray(originalPayload.filters)) {
26
+ return props;
27
+ }
28
+ if (!(originalPayload.filters._id instanceof ObjectId)) {
29
+ throw new Error();
30
+ }
25
31
  const { result: payload, error } = await checkImmutability(originalPayload.filters._id, props.result, context);
26
32
  if (error) {
27
33
  return Result.error(error);
@@ -5,6 +5,9 @@ const types_1 = require("@aeriajs/types");
5
5
  const checkOwnershipRead = async (props, context, next) => {
6
6
  const { token, description } = context;
7
7
  const { payload } = props.result;
8
+ if (Array.isArray(payload.filters)) {
9
+ return props;
10
+ }
8
11
  if (token.authenticated && description.owned && description.owned !== 'on-write') {
9
12
  if (!token.roles.includes('root')) {
10
13
  payload.filters.owner = token.sub;
@@ -3,6 +3,9 @@ import { Result, ACError } from "@aeriajs/types";
3
3
  export const checkOwnershipRead = async (props, context, next) => {
4
4
  const { token, description } = context;
5
5
  const { payload } = props.result;
6
+ if (Array.isArray(payload.filters)) {
7
+ return props;
8
+ }
6
9
  if (token.authenticated && description.owned && description.owned !== "on-write") {
7
10
  if (!token.roles.includes("root")) {
8
11
  payload.filters.owner = token.sub;
@@ -1,12 +1,66 @@
1
1
  import type { RouteContext, RateLimitingParams } from '@aeriajs/types';
2
2
  import { Result, HTTPStatus, RateLimitingError } from '@aeriajs/types';
3
- export declare const getOrCreateUsageEntry: (params: RateLimitingParams, context: RouteContext) => Promise<import("@aeriajs/types").WithId<Omit<import("@aeriajs/types").PackReferences<import("@aeriajs/types").SchemaWithId<import("@aeriajs/types").Description>>, "_id">>>;
4
- export declare const limitRate: (params: RateLimitingParams, context: RouteContext) => Promise<{
5
- readonly _tag: "Result";
6
- readonly error: undefined;
7
- readonly result: any;
8
- } | Result.Error<{
3
+ export declare const getOrCreateUsageEntry: (params: RateLimitingParams, context: RouteContext) => Promise<import("mongodb").WithId<Omit<import("@aeriajs/types").PackReferences<import("@aeriajs/types").SchemaWithId<{
4
+ readonly $id: "resourceUsage";
5
+ readonly icon: "wrench";
6
+ readonly required: readonly ["usage"];
7
+ readonly properties: {
8
+ readonly user: {
9
+ readonly $ref: "user";
10
+ };
11
+ readonly address: {
12
+ readonly type: "string";
13
+ };
14
+ readonly usage: {
15
+ readonly type: "object";
16
+ readonly additionalProperties: {
17
+ readonly type: "object";
18
+ readonly properties: {
19
+ readonly hits: {
20
+ readonly type: "integer";
21
+ };
22
+ readonly points: {
23
+ readonly type: "integer";
24
+ };
25
+ readonly last_reach: {
26
+ readonly type: "string";
27
+ readonly format: "date-time";
28
+ };
29
+ readonly last_maximum_reach: {
30
+ readonly type: "string";
31
+ readonly format: "date-time";
32
+ };
33
+ };
34
+ };
35
+ };
36
+ };
37
+ }>>, "_id">>>;
38
+ export declare const limitRate: (params: RateLimitingParams, context: RouteContext) => Promise<Result.Error<{
9
39
  readonly code: RateLimitingError.LimitReached;
10
40
  } & {
11
41
  httpStatus: HTTPStatus.TooManyRequests;
12
- }>>;
42
+ }> | {
43
+ readonly _tag: "Result";
44
+ readonly error: undefined;
45
+ readonly result: import("@aeriajs/types").PackReferences<{} & Omit<Readonly<import("@aeriajs/types").FilterReadonlyProperties<{
46
+ readonly hits: {
47
+ readonly type: "integer";
48
+ };
49
+ readonly points: {
50
+ readonly type: "integer";
51
+ };
52
+ readonly last_reach: {
53
+ readonly type: "string";
54
+ readonly format: "date-time";
55
+ };
56
+ readonly last_maximum_reach: {
57
+ readonly type: "string";
58
+ readonly format: "date-time";
59
+ };
60
+ }>> & {
61
+ hits: number;
62
+ points: number;
63
+ last_reach: Date;
64
+ last_maximum_reach: Date;
65
+ }, never>>;
66
+ }>;
@@ -62,7 +62,7 @@ const limitRate = async (params, context) => {
62
62
  }, {
63
63
  returnDocument: 'after',
64
64
  });
65
- if (!newEntry) {
65
+ if (!newEntry || !newEntry.usage[resourceName]) {
66
66
  throw new Error();
67
67
  }
68
68
  return types_1.Result.result(newEntry.usage[resourceName]);
@@ -67,7 +67,7 @@ export const limitRate = async (params, context) => {
67
67
  returnDocument: "after"
68
68
  }
69
69
  );
70
- if (!newEntry) {
70
+ if (!newEntry || !newEntry.usage[resourceName]) {
71
71
  throw new Error();
72
72
  }
73
73
  return Result.result(newEntry.usage[resourceName]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriajs/security",
3
- "version": "0.0.164",
3
+ "version": "0.0.166",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -28,9 +28,9 @@
28
28
  "mongodb": "^6.5.0"
29
29
  },
30
30
  "peerDependencies": {
31
- "@aeriajs/core": "^0.0.164",
32
- "@aeriajs/common": "^0.0.100",
33
- "@aeriajs/types": "^0.0.86",
31
+ "@aeriajs/core": "^0.0.166",
32
+ "@aeriajs/common": "^0.0.102",
33
+ "@aeriajs/types": "^0.0.87",
34
34
  "mongodb": "^6.5.0"
35
35
  },
36
36
  "scripts": {