@kravc/dos 1.11.11 → 1.11.13

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kravc/dos",
3
- "version": "1.11.11",
3
+ "version": "1.11.13",
4
4
  "description": "Convention-based, easy-to-use library for building API-driven serverless services.",
5
5
  "keywords": [
6
6
  "Service",
@@ -12,10 +12,11 @@ const IndexProfiles = require('examples/IndexProfiles')
12
12
 
13
13
  const test = require('src/test')
14
14
  const {
15
- errors,
16
15
  Create,
17
16
  Service,
18
17
  Component,
18
+ InvalidParametersError,
19
+ UnprocessibleConditionError,
19
20
  } = require('src')
20
21
 
21
22
  const testSchema = new Schema({
@@ -145,7 +146,7 @@ describe('Service', () => {
145
146
  it('returns "InvalidParametersError / 400" if invalid parameters', async () => {
146
147
  class InvalidIndexProfiles extends IndexProfiles {
147
148
  action() {
148
- throw new errors.InvalidParametersError()
149
+ throw new InvalidParametersError()
149
150
  }
150
151
  }
151
152
 
@@ -166,7 +167,7 @@ describe('Service', () => {
166
167
  it('returns "UnprocessibleConditionError / 422" if unprocessible condition', async () => {
167
168
  class InvalidIndexProfiles extends IndexProfiles {
168
169
  action() {
169
- throw new errors.UnprocessibleConditionError()
170
+ throw new UnprocessibleConditionError()
170
171
  }
171
172
  }
172
173
 
package/src/index.d.ts CHANGED
@@ -166,13 +166,14 @@ export declare class JwtAuthorization {
166
166
  cookieName?: string;
167
167
  normalizePayload?: Function;
168
168
  tokenVerificationMethod?: Function;
169
- accessVerificationMethod?: Function;
169
+ accessVerificationMethod?: (context: Context, payload: Record<string, unknown>) => [boolean, string?];
170
170
  }): Record<string, any>
171
171
  };
172
172
 
173
173
  export declare class SystemAuthorization {
174
174
  static createRequirement(options?: {
175
175
  name?: string;
176
+ accessVerificationMethod?: (context: Context) => [boolean, string?];
176
177
  }): Record<string, any>
177
178
  };
178
179
 
@@ -282,3 +283,33 @@ export declare function execute(service: Service, extraContext?: Record<string,
282
283
  errorName: string
283
284
  ) => Promise<OperationError>;
284
285
  }
286
+
287
+ export declare class CommonError extends Error {
288
+ constructor(code: string, message: string);
289
+ get code(): string;
290
+ get isCommonError(): boolean;
291
+ };
292
+
293
+ export declare class UnauthorizedError extends CommonError {
294
+ constructor(message: string);
295
+ };
296
+
297
+ export declare class AccessDeniedError extends CommonError {
298
+ constructor(message: string);
299
+ };
300
+
301
+ export declare class InvalidParametersError extends CommonError {
302
+ constructor(message: string)
303
+ };
304
+
305
+ export declare class UnprocessibleConditionError extends CommonError {
306
+ constructor(message: string)
307
+ };
308
+
309
+ export declare class DocumentExistsError extends CommonError {
310
+ constructor(Document: { name: string }, parameters: Record<string, unknown>);
311
+ };
312
+
313
+ export declare class DocumentNotFoundError extends CommonError {
314
+ constructor(Document: { name: string }, parameters: Record<string, unknown>);
315
+ };
package/src/index.js CHANGED
@@ -3,22 +3,28 @@
3
3
  const { wait, execute, createAccessToken } = require('./test')
4
4
 
5
5
  module.exports = {
6
- Document: require('./Document'),
7
- Operation: require('./Operation'),
8
- Service: require('./Service'),
9
- Read: require('./operations/Read'),
10
- Index: require('./operations/Index'),
11
- Create: require('./operations/Create'),
12
- Update: require('./operations/Update'),
13
- Delete: require('./operations/Delete'),
14
- Component: require('./Component'),
15
- errors: require('./errors'),
16
- handler: require('./helpers/handler'),
17
- security: require('./security'),
18
- getOrFail: require('./helpers/getOrFail'),
19
- verifyToken: require('./security/verifyToken'),
20
- JwtAuthorization: require('./security/JwtAuthorization'),
21
- SystemAuthorization: require('./security/SystemAuthorization'),
6
+ Document: require('./Document'),
7
+ Operation: require('./Operation'),
8
+ Service: require('./Service'),
9
+ Read: require('./operations/Read'),
10
+ Index: require('./operations/Index'),
11
+ Create: require('./operations/Create'),
12
+ Update: require('./operations/Update'),
13
+ Delete: require('./operations/Delete'),
14
+ Component: require('./Component'),
15
+ handler: require('./helpers/handler'),
16
+ getOrFail: require('./helpers/getOrFail'),
17
+ security: require('./security'),
18
+ verifyToken: require('./security/verifyToken'),
19
+ JwtAuthorization: require('./security/JwtAuthorization'),
20
+ SystemAuthorization: require('./security/SystemAuthorization'),
21
+ CommonError: require('./errors/CommonError'),
22
+ UnauthorizedError: require('./errors/UnauthorizedError'),
23
+ AccessDeniedError: require('./errors/AccessDeniedError'),
24
+ DocumentExistsError: require('./errors/DocumentExistsError'),
25
+ DocumentNotFoundError: require('./errors/DocumentNotFoundError'),
26
+ InvalidParametersError: require('./errors/InvalidParametersError'),
27
+ UnprocessibleConditionError: require('./errors/UnprocessibleConditionError'),
22
28
  wait,
23
29
  execute,
24
30
  createAccessToken,
@@ -3,10 +3,24 @@
3
3
  const { get } = require('lodash')
4
4
  const AccessDeniedError = require('../errors/AccessDeniedError')
5
5
 
6
+ const SYSTEM_NAME = 'System'
7
+
8
+ const verifySystemAccess = (context) => {
9
+ const { headers } = context
10
+ const isExternalRequest = Object.keys(headers).length > 0
11
+
12
+ if (!isExternalRequest) {
13
+ return [ true ]
14
+ }
15
+
16
+ return [ false ]
17
+ }
18
+
6
19
  class SystemAuthorization {
7
20
  static createRequirement(options = {}) {
8
21
  const name = get(options, 'name', 'authorization')
9
- const requirementName = 'System'
22
+
23
+ const requirementName = SYSTEM_NAME
10
24
 
11
25
  return {
12
26
  [requirementName]: {
@@ -20,7 +34,8 @@ class SystemAuthorization {
20
34
  type: 'apiKey',
21
35
  name
22
36
  },
23
- klass: this
37
+ klass: this,
38
+ ...options
24
39
  }
25
40
  }
26
41
  }
@@ -35,12 +50,17 @@ class SystemAuthorization {
35
50
  }
36
51
  }
37
52
 
53
+ constructor({
54
+ accessVerificationMethod = verifySystemAccess,
55
+ }) {
56
+ this._verifyAccess = accessVerificationMethod
57
+ }
58
+
38
59
  async verify(context) {
39
- const { headers } = context
40
- const isExternalRequest = Object.keys(headers).length > 0
60
+ const [ isAccessOk, accessErrorMessage ] = await this._verifyAccess(context)
41
61
 
42
- if (isExternalRequest) {
43
- const error = new AccessDeniedError()
62
+ if (!isAccessOk) {
63
+ const error = new AccessDeniedError(accessErrorMessage)
44
64
  return { isAuthorized: false, error }
45
65
  }
46
66