@memberjunction/encryption 3.2.0 → 3.4.0
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/README.md +87 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -1
- package/package.json +6 -5
package/README.md
CHANGED
|
@@ -316,6 +316,93 @@ if (engine.IsEncrypted(someValue)) {
|
|
|
316
316
|
engine.ClearCaches();
|
|
317
317
|
```
|
|
318
318
|
|
|
319
|
+
## API Key Management
|
|
320
|
+
|
|
321
|
+
The `EncryptionEngine` provides secure API key management for authentication scenarios like MCP servers, external integrations, and programmatic access.
|
|
322
|
+
|
|
323
|
+
### Creating API Keys
|
|
324
|
+
|
|
325
|
+
```typescript
|
|
326
|
+
import { EncryptionEngine } from '@memberjunction/encryption';
|
|
327
|
+
|
|
328
|
+
const result = await EncryptionEngine.Instance.CreateAPIKey({
|
|
329
|
+
userId: 'user-guid-here',
|
|
330
|
+
label: 'MCP Server Integration',
|
|
331
|
+
description: 'Used for Claude Desktop MCP connections',
|
|
332
|
+
expiresAt: new Date('2025-12-31') // Optional - omit for non-expiring keys
|
|
333
|
+
}, contextUser);
|
|
334
|
+
|
|
335
|
+
if (result.success) {
|
|
336
|
+
// CRITICAL: Save this key immediately - it cannot be recovered!
|
|
337
|
+
console.log('Your API Key:', result.rawKey);
|
|
338
|
+
console.log('API Key ID:', result.apiKeyId);
|
|
339
|
+
} else {
|
|
340
|
+
console.error('Failed to create API key:', result.error);
|
|
341
|
+
}
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
**Key Format**: `mj_sk_[64 hex characters]` (70 characters total)
|
|
345
|
+
|
|
346
|
+
**Security**: Only the SHA-256 hash is stored in the database. The raw key is returned exactly once at creation time and cannot be recovered.
|
|
347
|
+
|
|
348
|
+
### Validating API Keys
|
|
349
|
+
|
|
350
|
+
```typescript
|
|
351
|
+
const validation = await EncryptionEngine.Instance.ValidateAPIKey(
|
|
352
|
+
request.headers['x-api-key'],
|
|
353
|
+
systemUser
|
|
354
|
+
);
|
|
355
|
+
|
|
356
|
+
if (validation.isValid) {
|
|
357
|
+
// Use validation.user for authorized operations
|
|
358
|
+
console.log('Authenticated user:', validation.user.Name);
|
|
359
|
+
console.log('API Key ID:', validation.apiKeyId);
|
|
360
|
+
} else {
|
|
361
|
+
throw new Error(validation.error);
|
|
362
|
+
}
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
The validation method:
|
|
366
|
+
- Checks key format
|
|
367
|
+
- Looks up the hash in the CredentialEngine cache (fast!)
|
|
368
|
+
- Verifies the key is active and not expired
|
|
369
|
+
- Loads the associated user from the database
|
|
370
|
+
- Updates `LastUsedAt` and logs usage
|
|
371
|
+
|
|
372
|
+
### Other API Key Methods
|
|
373
|
+
|
|
374
|
+
```typescript
|
|
375
|
+
// Generate a key without storing it (for custom storage scenarios)
|
|
376
|
+
const { raw, hash } = EncryptionEngine.Instance.GenerateAPIKey();
|
|
377
|
+
|
|
378
|
+
// Hash a key for manual comparison
|
|
379
|
+
const keyHash = EncryptionEngine.Instance.HashAPIKey(rawKey);
|
|
380
|
+
|
|
381
|
+
// Validate key format before processing
|
|
382
|
+
if (!EncryptionEngine.Instance.IsValidAPIKeyFormat(key)) {
|
|
383
|
+
throw new Error('Invalid API key format');
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// Revoke an API key (permanently disables it)
|
|
387
|
+
const revoked = await EncryptionEngine.Instance.RevokeAPIKey(apiKeyId, contextUser);
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
### API Key Database Schema
|
|
391
|
+
|
|
392
|
+
The API key system uses these tables (part of MemberJunction core):
|
|
393
|
+
|
|
394
|
+
**MJ: API Keys**
|
|
395
|
+
- `ID` - Unique identifier
|
|
396
|
+
- `Hash` - SHA-256 hash of the raw key
|
|
397
|
+
- `UserID` - Associated user (operations execute with this user's permissions)
|
|
398
|
+
- `Label` - Friendly name
|
|
399
|
+
- `Status` - `Active` or `Revoked`
|
|
400
|
+
- `ExpiresAt` - Optional expiration date
|
|
401
|
+
- `LastUsedAt` - Automatically updated on each use
|
|
402
|
+
|
|
403
|
+
**MJ: API Key Usage Logs**
|
|
404
|
+
- Tracks API key usage for analytics and security monitoring
|
|
405
|
+
|
|
319
406
|
## Encrypted Value Format
|
|
320
407
|
|
|
321
408
|
Encrypted values are stored as self-describing strings:
|
package/dist/index.d.ts
CHANGED
|
@@ -51,6 +51,11 @@
|
|
|
51
51
|
* - Random IVs for each encryption operation
|
|
52
52
|
* - Cached key material has configurable TTL
|
|
53
53
|
*
|
|
54
|
+
* ## API Key Management
|
|
55
|
+
*
|
|
56
|
+
* API key functionality has been moved to the `@memberjunction/api-keys` package.
|
|
57
|
+
* Use that package for API key generation, validation, and scope-based authorization.
|
|
58
|
+
*
|
|
54
59
|
* @module @memberjunction/encryption
|
|
55
60
|
* @packageDocumentation
|
|
56
61
|
*/
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4DG;AAGH,OAAO,EACH,yBAAyB,EACzB,mBAAmB,EACnB,gBAAgB,EAChB,eAAe,EACf,eAAe,EACf,2BAA2B,EAC3B,2BAA2B,EAC9B,MAAM,cAAc,CAAC;AAGtB,OAAO,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAC;AAGpE,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAGtD,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EAAE,mBAAmB,EAAE,MAAM,iCAAiC,CAAC;AAGtE,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EAAE,sBAAsB,EAAE,MAAM,oCAAoC,CAAC;AAG5E,OAAO,EAAE,yBAAyB,EAAE,MAAM,qCAAqC,CAAC;AAChF,OAAO,EAAE,2BAA2B,EAAE,MAAM,uCAAuC,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -52,6 +52,11 @@
|
|
|
52
52
|
* - Random IVs for each encryption operation
|
|
53
53
|
* - Cached key material has configurable TTL
|
|
54
54
|
*
|
|
55
|
+
* ## API Key Management
|
|
56
|
+
*
|
|
57
|
+
* API key functionality has been moved to the `@memberjunction/api-keys` package.
|
|
58
|
+
* Use that package for API key generation, validation, and scope-based authorization.
|
|
59
|
+
*
|
|
55
60
|
* @module @memberjunction/encryption
|
|
56
61
|
* @packageDocumentation
|
|
57
62
|
*/
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4DG;;;AAaH,sCAAsC;AACtC,qEAAoE;AAA3D,kIAAA,uBAAuB,OAAA;AAEhC,yBAAyB;AACzB,uDAAsD;AAA7C,oHAAA,gBAAgB,OAAA;AAEzB,gCAAgC;AAChC,+DAA8D;AAArD,kHAAA,eAAe,OAAA;AACxB,uEAAsE;AAA7D,0HAAA,mBAAmB,OAAA;AAE5B,6DAA6D;AAC7D,+DAA8D;AAArD,kHAAA,eAAe,OAAA;AACxB,6EAA4E;AAAnE,gIAAA,sBAAsB,OAAA;AAE/B,gDAAgD;AAChD,iFAAgF;AAAvE,sIAAA,yBAAyB,OAAA;AAClC,qFAAoF;AAA3E,0IAAA,2BAA2B,OAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@memberjunction/encryption",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.4.0",
|
|
4
4
|
"description": "MemberJunction: Field-level encryption engine with pluggable key sources. Server-side only - provides AES-256-GCM/CBC encryption with environment variable, config file, AWS KMS, and Azure Key Vault key sources.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -23,10 +23,11 @@
|
|
|
23
23
|
"typescript": "^5.4.5"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@memberjunction/global": "3.
|
|
27
|
-
"@memberjunction/core": "3.
|
|
28
|
-
"@memberjunction/core-entities": "3.
|
|
29
|
-
"@memberjunction/
|
|
26
|
+
"@memberjunction/global": "3.4.0",
|
|
27
|
+
"@memberjunction/core": "3.4.0",
|
|
28
|
+
"@memberjunction/core-entities": "3.4.0",
|
|
29
|
+
"@memberjunction/credentials": "3.4.0",
|
|
30
|
+
"@memberjunction/actions-base": "3.4.0",
|
|
30
31
|
"cosmiconfig": "^9.0.0"
|
|
31
32
|
},
|
|
32
33
|
"optionalDependencies": {
|