Skip to content

Commit bd8bf1e

Browse files
Douhi, AlehDouhi, Aleh
authored andcommitted
Added ability encrypt and decrypt first level elements, including primitive strings using JWE
1 parent 5bf69ac commit bd8bf1e

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ Output:
239239
- [Performing JWE Decryption](#performing-jwe-decryption)
240240
- [Encrypting Entire Payloads](#encrypting-entire-payloads-jwe)
241241
- [Decrypting Entire Payloads](#decrypting-entire-payloads-jwe)
242+
- [First Level Field Encryption and Decryption](#encrypting-decrypting-first-level-field-jwe)
242243

243244
##### • Introduction <a name="jwe-introduction"></a>
244245

@@ -466,6 +467,86 @@ Output:
466467
}
467468
```
468469

470+
##### • First Level Field Encryption and Decryption <a name="encrypting-decrypting-first-level-field-jwe"></a>
471+
472+
To have encrypted results in the first level field or to decrypt the first level field, specify `encryptedValueFieldName` to be the same as `obj` (for encryption) or `element` (for decryption):
473+
474+
Example of configuration:
475+
476+
```js
477+
const config = {
478+
paths: [
479+
{
480+
path: "/resource1",
481+
toEncrypt: [
482+
{
483+
/* path to element to be encrypted in request json body */
484+
element: "sensitive",
485+
/* path to object where to store encryption fields in request json body */
486+
obj: "encryptedData",
487+
},
488+
],
489+
toDecrypt: [
490+
{
491+
/* path to element where to store decrypted fields in response object */
492+
element: "encryptedData",
493+
/* path to object with encryption fields */
494+
obj: "sensitive",
495+
},
496+
],
497+
},
498+
],
499+
mode: "JWE",
500+
encryptedValueFieldName: "encryptedData",
501+
encryptionCertificate: "./path/to/public.cert",
502+
privateKey: "./path/to/your/private.key",
503+
};
504+
```
505+
506+
Example of encryption:
507+
508+
```js
509+
const payload = {
510+
sensitive: "this is a secret!",
511+
notSensitive: "not a secret",
512+
};
513+
const jwe = new (require("mastercard-client-encryption").JweEncryption)(config);
514+
//
515+
let responsePayload = jwe.encrypt("/resource1", header, payload);
516+
```
517+
518+
Output:
519+
520+
```json
521+
{
522+
"encryptedData": "eyJraWQiOiI3NjFiMDAzYzFlYWRlM….Y+oPYKZEMTKyYcSIVEgtQw",
523+
"notSensitive": "not a secret"
524+
}
525+
```
526+
527+
Example of decryption:
528+
529+
```js
530+
const response = {};
531+
response.request = { url: "/resource1" };
532+
response.body =
533+
"{" +
534+
' "encryptedData": "eyJraWQiOiI3NjFiMDAzYzFlYWRlM….Y+oPYKZEMTKyYcSIVEgtQw",' +
535+
' "notSensitive": "not a secret"' +
536+
"}";
537+
const jwe = new (require("mastercard-client-encryption").JweEncryption)(config);
538+
let responsePayload = jwe.decrypt(response);
539+
```
540+
541+
Output:
542+
543+
```json
544+
{
545+
"sensitive": "this is a secret",
546+
"notSensitive": "not a secret"
547+
}
548+
```
549+
469550
### Integrating with OpenAPI Generator API Client Libraries <a name="integrating-with-openapi-generator-api-client-libraries"></a>
470551

471552
[OpenAPI Generator](https://github.com/OpenAPITools/openapi-generator) generates API client libraries from [OpenAPI Specs](https://github.com/OAI/OpenAPI-Specification).

0 commit comments

Comments
 (0)