@squiz/dx-common-lib 1.39.1-alpha.2 → 1.39.1-alpha.22

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 (31) hide show
  1. package/.npm/_logs/{2023-06-22T07_22_05_356Z-debug-0.log → 2023-06-28T23_31_45_936Z-debug-0.log} +16 -16
  2. package/lib/api-key-validation/ApiKeyValidationService.d.ts +8 -3
  3. package/lib/api-key-validation/CloudflareApiKeyService.d.ts +9 -4
  4. package/lib/api-key-validation/CloudflareApiKeyService.js +24 -12
  5. package/lib/api-key-validation/CloudflareApiKeyService.js.map +1 -1
  6. package/lib/api-key-validation/CloudflareApiKeyService.spec.js +26 -23
  7. package/lib/api-key-validation/CloudflareApiKeyService.spec.js.map +1 -1
  8. package/lib/api-key-validation/DevelopmentApiKeyService.d.ts +8 -3
  9. package/lib/api-key-validation/DevelopmentApiKeyService.js +14 -5
  10. package/lib/api-key-validation/DevelopmentApiKeyService.js.map +1 -1
  11. package/lib/api-key-validation/DevelopmentApiKeyService.spec.js +10 -6
  12. package/lib/api-key-validation/DevelopmentApiKeyService.spec.js.map +1 -1
  13. package/lib/index.d.ts +0 -1
  14. package/lib/index.js +0 -1
  15. package/lib/index.js.map +1 -1
  16. package/package.json +4 -4
  17. package/src/api-key-validation/ApiKeyValidationService.ts +9 -3
  18. package/src/api-key-validation/CloudflareApiKeyService.spec.ts +38 -24
  19. package/src/api-key-validation/CloudflareApiKeyService.ts +27 -13
  20. package/src/api-key-validation/DevelopmentApiKeyService.spec.ts +12 -6
  21. package/src/api-key-validation/DevelopmentApiKeyService.ts +16 -5
  22. package/src/index.ts +0 -1
  23. package/tsconfig.tsbuildinfo +1 -1
  24. package/lib/server-utils/apiKeyMiddleware.d.ts +0 -6
  25. package/lib/server-utils/apiKeyMiddleware.js +0 -23
  26. package/lib/server-utils/apiKeyMiddleware.js.map +0 -1
  27. package/lib/server-utils/apiKeyMiddleware.spec.d.ts +0 -1
  28. package/lib/server-utils/apiKeyMiddleware.spec.js +0 -40
  29. package/lib/server-utils/apiKeyMiddleware.spec.js.map +0 -1
  30. package/src/server-utils/apiKeyMiddleware.spec.ts +0 -51
  31. package/src/server-utils/apiKeyMiddleware.ts +0 -26
@@ -6,15 +6,16 @@ import {
6
6
  import { UnAuthenticatedRequestError } from '../error/UnAuthenticatedRequestError';
7
7
  import { ApiKeyValidationService } from './ApiKeyValidationService';
8
8
  import { getLogger, Logger } from '@squiz/dx-logger-lib';
9
+ import { IncomingMessage } from 'http';
9
10
 
10
11
  export interface CloudFlareKeys {
11
- keys: string[];
12
12
  'matrix-keys': string[];
13
+ 'inter-service-keys': string[];
13
14
  }
14
15
 
15
16
  let validKeys: CloudFlareKeys = {
16
- keys: [],
17
17
  'matrix-keys': [],
18
+ 'inter-service-keys': [],
18
19
  };
19
20
 
20
21
  let refreshInterval: ReturnType<typeof setInterval>;
@@ -38,20 +39,32 @@ export class CloudflareApiKeyService implements ApiKeyValidationService {
38
39
  this.logger = logger;
39
40
  }
40
41
 
41
- public keyIsValid(key: string): boolean {
42
- if (validKeys.keys.length == 0) {
42
+ /**
43
+ * @deprecated Suggest moving towards using JWT authentication.
44
+ */
45
+ public matrixKeyIsValid(key: string, request: IncomingMessage): boolean {
46
+ if (validKeys['matrix-keys'].length == 0) {
43
47
  throw new UnAuthenticatedRequestError('No api keys to check against');
44
48
  }
45
49
 
46
- return validKeys.keys.includes(key);
50
+ if (validKeys['matrix-keys'].includes(key)) {
51
+ this.logger.info('Request authenticated using legacy Matrix API key', {
52
+ url: request.url,
53
+ userAgent: request.headers?.['user-agent'],
54
+ });
55
+
56
+ return true;
57
+ }
58
+
59
+ return false;
47
60
  }
48
61
 
49
- public matrixKeyIsValid(key: string): boolean {
50
- if (validKeys['matrix-keys'].length == 0) {
51
- throw new UnAuthenticatedRequestError('No api keys to check against');
62
+ public interServiceKeyIsValid(key: string): boolean {
63
+ if (validKeys['inter-service-keys'].length == 0) {
64
+ throw new UnAuthenticatedRequestError('No inter service api keys to check against');
52
65
  }
53
66
 
54
- return validKeys['matrix-keys'].includes(key);
67
+ return validKeys['inter-service-keys'].includes(key);
55
68
  }
56
69
 
57
70
  protected async getValidApiKeys(): Promise<CloudFlareKeys> {
@@ -74,7 +87,7 @@ export class CloudflareApiKeyService implements ApiKeyValidationService {
74
87
  }
75
88
  const secret = JSON.parse(secretValue.SecretString);
76
89
 
77
- if (secret.keys) {
90
+ if (secret['matrix-keys'] && secret['inter-service-keys']) {
78
91
  return secret;
79
92
  }
80
93
 
@@ -88,7 +101,8 @@ export class CloudflareApiKeyService implements ApiKeyValidationService {
88
101
  this.logger.info('refreshing keys');
89
102
  validKeys = await this.getValidApiKeys();
90
103
 
91
- this.logger.info(`number of valid keys found: ${validKeys.keys.length}`);
104
+ this.logger.info(`found ${validKeys['matrix-keys'].length} valid matrix keys`);
105
+ this.logger.info(`found ${validKeys['inter-service-keys'].length} valid inter service keys`);
92
106
 
93
107
  if (!refreshInterval) {
94
108
  refreshInterval = setInterval(async () => {
@@ -98,7 +112,7 @@ export class CloudflareApiKeyService implements ApiKeyValidationService {
98
112
  }
99
113
 
100
114
  // temporary method, to be removed when DXP auth is properly implemented
101
- getMatrixKeys(): string[] {
102
- return validKeys['matrix-keys'];
115
+ public getInterServiceKeys(): string[] {
116
+ return validKeys['inter-service-keys'];
103
117
  }
104
118
  }
@@ -1,17 +1,23 @@
1
1
  import { DevelopmentApiKeyService } from './DevelopmentApiKeyService';
2
+ import { IncomingMessage } from 'http';
3
+ import { getLogger } from '@squiz/dx-logger-lib';
2
4
 
3
5
  describe('DevelopmentApiKeyService', () => {
4
- const service = new DevelopmentApiKeyService();
6
+ const service = new DevelopmentApiKeyService(getLogger({ name: 'test', silent: true }));
5
7
 
6
- describe('keyIsValid', () => {
8
+ describe('matrixKeyIsValid', () => {
7
9
  it('should always return true', () => {
8
- expect(service.keyIsValid()).toEqual(true);
10
+ expect(service.matrixKeyIsValid(undefined, {} as IncomingMessage)).toEqual(true);
9
11
  });
10
12
  });
11
13
 
12
- describe('matrixKeyIsValid', () => {
13
- it('should always return true', () => {
14
- expect(service.matrixKeyIsValid()).toEqual(true);
14
+ describe('interServiceKeyIsValid', () => {
15
+ it('should return true if key is "inter-service-api-key"', () => {
16
+ expect(service.interServiceKeyIsValid('inter-service-api-key')).toEqual(true);
17
+ });
18
+
19
+ it('should return false if key is not "inter-service-api-key"', () => {
20
+ expect(service.interServiceKeyIsValid('invalid-api-key')).toEqual(false);
15
21
  });
16
22
  });
17
23
  });
@@ -1,14 +1,25 @@
1
+ import { getLogger, Logger } from '@squiz/dx-logger-lib';
1
2
  import { ApiKeyValidationService } from './ApiKeyValidationService';
3
+ import { IncomingMessage } from 'http';
2
4
 
3
5
  export class DevelopmentApiKeyService implements ApiKeyValidationService {
4
- public keyIsValid(): boolean {
5
- return true;
6
+ private logger: Logger;
7
+
8
+ public constructor(logger?: Logger) {
9
+ this.logger = logger || getLogger({ name: 'DevelopmentApiKeyService' });
6
10
  }
7
- public matrixKeyIsValid(): boolean {
11
+ public matrixKeyIsValid(key: string | undefined, request: IncomingMessage): boolean {
12
+ this.logger.info('Request authenticated using legacy Matrix API key', {
13
+ url: request.url,
14
+ userAgent: request.headers?.['user-agent'],
15
+ });
8
16
  return true;
9
17
  }
18
+ public interServiceKeyIsValid(key: string | undefined): boolean {
19
+ return Boolean(key && this.getInterServiceKeys().includes(key));
20
+ }
10
21
  // temporary method, to be removed when DXP auth is properly implemented
11
- getMatrixKeys(): string[] {
12
- return ['matrix-api-key'];
22
+ public getInterServiceKeys(): string[] {
23
+ return ['inter-service-api-key'];
13
24
  }
14
25
  }
package/src/index.ts CHANGED
@@ -8,7 +8,6 @@ export * from './api-key-validation/ApiKeyValidationService';
8
8
  export * from './api-key-validation/CloudflareApiKeyService';
9
9
  export * from './api-key-validation/DevelopmentApiKeyService';
10
10
  export * from './api-key-validation/getApiKeyService';
11
- export * from './server-utils/apiKeyMiddleware';
12
11
  export * from './cache';
13
12
  export * from './formatted-text/formattedTextToHtmlString';
14
13
  export * from './json-order';