@levrbet/shared 0.2.52 → 0.2.53
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/dist/server/services/crypto.service.d.ts +28 -0
- package/dist/server/services/crypto.service.js +28 -0
- package/dist/server/services/crypto.service.js.map +1 -1
- package/dist/server/services/hmac.service.d.ts +35 -4
- package/dist/server/services/hmac.service.js +35 -4
- package/dist/server/services/hmac.service.js.map +1 -1
- package/package.json +1 -1
|
@@ -6,6 +6,34 @@ interface SignRequestParams {
|
|
|
6
6
|
nonce: string;
|
|
7
7
|
secretKey: string;
|
|
8
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* Service for cryptographic operations including data key generation, request signing, and secret key management.
|
|
11
|
+
*
|
|
12
|
+
* @remarks
|
|
13
|
+
* This service provides three main functionalities:
|
|
14
|
+
* - Generates data keys using AWS KMS with AES-256 encryption
|
|
15
|
+
* - Signs HTTP requests using HMAC-SHA256 for API authentication
|
|
16
|
+
* - Retrieves and caches secret keys with Redis for performance optimization
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* Generate a new data key
|
|
21
|
+
* const { ciphertext, secretKey } = await cryptoService.generateDataKey();
|
|
22
|
+
*
|
|
23
|
+
* Sign an API request
|
|
24
|
+
* const signature = cryptoService.signRequest({
|
|
25
|
+
* method: 'POST',
|
|
26
|
+
* path: '/api/endpoint',
|
|
27
|
+
* ts: Date.now(),
|
|
28
|
+
* body: { data: 'value' },
|
|
29
|
+
* nonce: 'random-nonce',
|
|
30
|
+
* secretKey: 'base64-encoded-key'
|
|
31
|
+
* });
|
|
32
|
+
*
|
|
33
|
+
* Retrieve a secret key
|
|
34
|
+
* const key = await cryptoService.getSecretKey('api-key-id', 'ciphertext-base64');
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
9
37
|
export declare const cryptoService: {
|
|
10
38
|
generateDataKey(): Promise<{
|
|
11
39
|
ciphertext: string;
|
|
@@ -9,6 +9,34 @@ const crypto_1 = __importDefault(require("crypto"));
|
|
|
9
9
|
const lodash_1 = __importDefault(require("lodash"));
|
|
10
10
|
const config_1 = require("../config");
|
|
11
11
|
const CACHE_TTL = 5 * 60; // 5 minutes in seconds
|
|
12
|
+
/**
|
|
13
|
+
* Service for cryptographic operations including data key generation, request signing, and secret key management.
|
|
14
|
+
*
|
|
15
|
+
* @remarks
|
|
16
|
+
* This service provides three main functionalities:
|
|
17
|
+
* - Generates data keys using AWS KMS with AES-256 encryption
|
|
18
|
+
* - Signs HTTP requests using HMAC-SHA256 for API authentication
|
|
19
|
+
* - Retrieves and caches secret keys with Redis for performance optimization
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* Generate a new data key
|
|
24
|
+
* const { ciphertext, secretKey } = await cryptoService.generateDataKey();
|
|
25
|
+
*
|
|
26
|
+
* Sign an API request
|
|
27
|
+
* const signature = cryptoService.signRequest({
|
|
28
|
+
* method: 'POST',
|
|
29
|
+
* path: '/api/endpoint',
|
|
30
|
+
* ts: Date.now(),
|
|
31
|
+
* body: { data: 'value' },
|
|
32
|
+
* nonce: 'random-nonce',
|
|
33
|
+
* secretKey: 'base64-encoded-key'
|
|
34
|
+
* });
|
|
35
|
+
*
|
|
36
|
+
* Retrieve a secret key
|
|
37
|
+
* const key = await cryptoService.getSecretKey('api-key-id', 'ciphertext-base64');
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
12
40
|
exports.cryptoService = {
|
|
13
41
|
async generateDataKey() {
|
|
14
42
|
const { client, KeyId } = config_1.kmsClientManager;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crypto.service.js","sourceRoot":"","sources":["../../../src/server/services/crypto.service.ts"],"names":[],"mappings":";;;;;;AAAA,oDAA4E;AAC5E,oDAA2B;AAC3B,oDAAsB;AACtB,sCAAwE;AAWxE,MAAM,SAAS,GAAG,CAAC,GAAG,EAAE,CAAA,CAAC,uB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S,EAAE,SAAS,CAAC,CAAA;YAE5E,OAAO,SAAS,CAAA;QACpB,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACX,eAAM,CAAC,KAAK,CAAC,2BAA2B,EAAE,GAAG,CAAC,CAAA;YAC9C,MAAM,GAAG,CAAA;QACb,CAAC;IACL,CAAC;CACJ,CAAA"}
|
|
@@ -15,7 +15,38 @@ interface HmacAuthHeaders {
|
|
|
15
15
|
"x-request-nonce": string;
|
|
16
16
|
}
|
|
17
17
|
/**
|
|
18
|
-
*
|
|
18
|
+
* Service for generating and validating HMAC-based authentication headers.
|
|
19
|
+
*
|
|
20
|
+
* Provides methods to:
|
|
21
|
+
* - Generate HMAC authentication headers for API requests
|
|
22
|
+
* - Generate service-to-service authentication headers
|
|
23
|
+
* - Attach Axios request interceptors for automatic request signing
|
|
24
|
+
*
|
|
25
|
+
* @remarks
|
|
26
|
+
* This service must be initialized with `init()` before generating service auth headers,
|
|
27
|
+
* which loads `SERVICE_API_KEY_ID` and `SERVICE_API_KEY_SECRET` from environment variables.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* import { hmacService } from "./hmac.service"
|
|
32
|
+
*
|
|
33
|
+
* // Initialize service keys from environment
|
|
34
|
+
* hmacService.init()
|
|
35
|
+
*
|
|
36
|
+
* // Generate auth headers manually
|
|
37
|
+
* const headers = hmacService.generateAuthHeaders({
|
|
38
|
+
* apiKeyId: "my-api-key",
|
|
39
|
+
* secretKey: "my-secret",
|
|
40
|
+
* method: "POST",
|
|
41
|
+
* path: "/api/endpoint",
|
|
42
|
+
* body: { data: "example" }
|
|
43
|
+
* })
|
|
44
|
+
*
|
|
45
|
+
* // Or use with Axios interceptor
|
|
46
|
+
* const client = axios.create({ baseURL: "https://api.example.com" })
|
|
47
|
+
* hmacService.attachInterceptor(client)
|
|
48
|
+
* await client.post("/endpoint", { foo: "bar" }, { sign: true })
|
|
49
|
+
* ```
|
|
19
50
|
*/
|
|
20
51
|
declare class HmacService {
|
|
21
52
|
private initialized;
|
|
@@ -32,14 +63,14 @@ declare class HmacService {
|
|
|
32
63
|
* import axios from "axios"
|
|
33
64
|
* import { hmacService } from "@levr/shared/src/server/services/hmac.service"
|
|
34
65
|
*
|
|
35
|
-
*
|
|
66
|
+
* On startup (loads SERVICE_API_KEY_ID and SERVICE_API_KEY_SECRET from env)
|
|
36
67
|
* hmacService.init()
|
|
37
68
|
*
|
|
38
|
-
*
|
|
69
|
+
* Create axios client and attach the interceptor
|
|
39
70
|
* const client = axios.create({ baseURL: "https://api.my-service.com" })
|
|
40
71
|
* hmacService.attachInterceptor(client)
|
|
41
72
|
*
|
|
42
|
-
*
|
|
73
|
+
* Make a signed request by setting `sign: true`
|
|
43
74
|
* await client.post(
|
|
44
75
|
* "/v1/some/endpoint",
|
|
45
76
|
* { foo: "bar" },
|
|
@@ -9,7 +9,38 @@ const core_1 = require("../../core");
|
|
|
9
9
|
const dotenv_1 = require("../config/dotenv");
|
|
10
10
|
const crypto_service_1 = require("./crypto.service");
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
12
|
+
* Service for generating and validating HMAC-based authentication headers.
|
|
13
|
+
*
|
|
14
|
+
* Provides methods to:
|
|
15
|
+
* - Generate HMAC authentication headers for API requests
|
|
16
|
+
* - Generate service-to-service authentication headers
|
|
17
|
+
* - Attach Axios request interceptors for automatic request signing
|
|
18
|
+
*
|
|
19
|
+
* @remarks
|
|
20
|
+
* This service must be initialized with `init()` before generating service auth headers,
|
|
21
|
+
* which loads `SERVICE_API_KEY_ID` and `SERVICE_API_KEY_SECRET` from environment variables.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* import { hmacService } from "./hmac.service"
|
|
26
|
+
*
|
|
27
|
+
* // Initialize service keys from environment
|
|
28
|
+
* hmacService.init()
|
|
29
|
+
*
|
|
30
|
+
* // Generate auth headers manually
|
|
31
|
+
* const headers = hmacService.generateAuthHeaders({
|
|
32
|
+
* apiKeyId: "my-api-key",
|
|
33
|
+
* secretKey: "my-secret",
|
|
34
|
+
* method: "POST",
|
|
35
|
+
* path: "/api/endpoint",
|
|
36
|
+
* body: { data: "example" }
|
|
37
|
+
* })
|
|
38
|
+
*
|
|
39
|
+
* // Or use with Axios interceptor
|
|
40
|
+
* const client = axios.create({ baseURL: "https://api.example.com" })
|
|
41
|
+
* hmacService.attachInterceptor(client)
|
|
42
|
+
* await client.post("/endpoint", { foo: "bar" }, { sign: true })
|
|
43
|
+
* ```
|
|
13
44
|
*/
|
|
14
45
|
class HmacService {
|
|
15
46
|
constructor() {
|
|
@@ -57,14 +88,14 @@ class HmacService {
|
|
|
57
88
|
* import axios from "axios"
|
|
58
89
|
* import { hmacService } from "@levr/shared/src/server/services/hmac.service"
|
|
59
90
|
*
|
|
60
|
-
*
|
|
91
|
+
* On startup (loads SERVICE_API_KEY_ID and SERVICE_API_KEY_SECRET from env)
|
|
61
92
|
* hmacService.init()
|
|
62
93
|
*
|
|
63
|
-
*
|
|
94
|
+
* Create axios client and attach the interceptor
|
|
64
95
|
* const client = axios.create({ baseURL: "https://api.my-service.com" })
|
|
65
96
|
* hmacService.attachInterceptor(client)
|
|
66
97
|
*
|
|
67
|
-
*
|
|
98
|
+
* Make a signed request by setting `sign: true`
|
|
68
99
|
* await client.post(
|
|
69
100
|
* "/v1/some/endpoint",
|
|
70
101
|
* { foo: "bar" },
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hmac.service.js","sourceRoot":"","sources":["../../../src/server/services/hmac.service.ts"],"names":[],"mappings":";;;;;;AACA,oDAA2B;AAC3B,qCAA6C;AAC7C,6CAAoD;AACpD,qDAAgD;AAmBhD
|
|
1
|
+
{"version":3,"file":"hmac.service.js","sourceRoot":"","sources":["../../../src/server/services/hmac.service.ts"],"names":[],"mappings":";;;;;;AACA,oDAA2B;AAC3B,qCAA6C;AAC7C,6CAAoD;AACpD,qDAAgD;AAmBhD;;;;;;;;;;;;;;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