Skip to content
This repository was archived by the owner on Feb 28, 2024. It is now read-only.

Commit 1cd724c

Browse files
committed
updating readme
1 parent 69c05d7 commit 1cd724c

File tree

1 file changed

+213
-1
lines changed

1 file changed

+213
-1
lines changed

README.md

Lines changed: 213 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* [Loading the Encryption Certificate](#loading-the-encryption-certificate)
1818
* [Loading the Decryption Key](#loading-the-decryption-key)
1919
* [Performing Field Level Encryption and Decryption](#performing-field-level-encryption-and-decryption)
20+
* [Performing JWE Encryption and Decryption](#performing-JWE-encryption-and-decryption)
2021
* [Integrating with OpenAPI Generator API Client Libraries](#integrating-with-openapi-generator-api-client-libraries)
2122

2223
## Overview <a name="overview"></a>
@@ -387,6 +388,191 @@ Output:
387388
}
388389
```
389390

391+
### Performing JWE Encryption and Decryption <a name="performing-JWE-encryption-and-decryption"></a>
392+
393+
+ [Introduction](#introduction-jwe)
394+
+ [Configuring the JWE Encryption](#configuring-the-JWE-encryption)
395+
+ [Performing Encryption](#performing-encryption-jwe)
396+
+ [Performing Decryption](#performing-decryption-jwe)
397+
+ [Encrypting Entire Payloads](#encrypting-entire-payloads-jwe)
398+
+ [Decrypting Entire Payloads](#decrypting-entire-payloads-jwe)
399+
400+
#### Introduction <a name="introduction-jwe"></a>
401+
402+
The core methods responsible for payload encryption and decryption are `encryptPayload` and `decryptPayload` in the `JWEEncryption` class.
403+
404+
* `encryptPayload` usage:
405+
```php
406+
use Mastercard\Developer\Encryption;
407+
// …
408+
$encryptedRequestPayload = JWEEncryption::encryptPayload($requestPayload, $config);
409+
```
410+
411+
* `decryptPayload` usage:
412+
```php
413+
use Mastercard\Developer\Encryption;
414+
// …
415+
$responsePayload = JWEEncryption::decryptPayload($encryptedResponsePayload, $config);
416+
```
417+
418+
#### Configuring the JWE Encryption <a name="configuring-the-JWE-encryption"></a>
419+
Use the `JWEEncryptionConfigBuilder` to create `JWEEncryptionConfig` instances. Example:
420+
```php
421+
use Mastercard\Developer\Encryption;
422+
// …
423+
$config = FieldLevelEncryptionConfigBuilder::aFieldLevelEncryptionConfig()
424+
->withEncryptionCertificate($encryptionCertificate)
425+
->withDecryptionKey($decryptionKey)
426+
->withEncryptionPath('$.path.to.foo', '$.path.to.encryptedFoo')
427+
->withDecryptionPath('$.path.to.encryptedFoo', '$.path.to.foo')
428+
->withEncryptedValueFieldName('encryptedValue')
429+
->build();
430+
```
431+
Note: If `withEncryptedValueFieldName` is left blank, the value will default to `encryptedData`
432+
433+
See also:
434+
* [JWEEncryptionConfig.php](https://github.com/Mastercard/client-encryption-php/blob/master/src/Developer/Encryption/JWEEncryptionConfig.php) for all config options
435+
* [Service Configurations for Client Encryption PHP](https://github.com/Mastercard/client-encryption-php/wiki/Service-Configurations-for-Client-Encryption-PHP)
436+
437+
#### Performing Encryption <a name="performing-encryption-jwe"></a>
438+
439+
Call `JWEEncryption::encryptPayload` with a JSON request payload and a `JWEEncryptionConfig` instance.
440+
441+
Example using the configuration [above](#configuring-the-JWE-encryption):
442+
```php
443+
use Mastercard\Developer\Encryption;
444+
// …
445+
$payload = '{
446+
"path": {
447+
"to": {
448+
"foo": {
449+
"sensitiveField1": "sensitiveValue1",
450+
"sensitiveField2": "sensitiveValue2"
451+
}
452+
}
453+
}
454+
}';
455+
$encryptedPayload = JWEEncryption::encryptPayload($payload, $config);
456+
echo (json_encode(json_decode($encryptedPayload), JSON_PRETTY_PRINT));
457+
```
458+
459+
Output:
460+
461+
```json
462+
{
463+
"path": {
464+
"to": {
465+
"encryptedFoo": "809a09d78257af5379df0c454dcdf…353ed59fe72fd4a7735c69da4080e74f"
466+
}
467+
}
468+
}
469+
```
470+
471+
#### Performing Decryption <a name="performing-decryption-jwe"></a>
472+
473+
Call `JWEEncryption::decryptPayload` with a JSON response payload and a `JWEEncryptionConfig` instance.
474+
475+
Example using the configuration [above](#configuring-the-JWE-encryption):
476+
```php
477+
use Mastercard\Developer\Encryption;
478+
// …
479+
$encryptedPayload = '{
480+
"path": {
481+
"to": {
482+
"encryptedFoo": {
483+
"encryptedValue": "809a09d78257af5379df0c454dcdf…353ed59fe72fd4a7735c69da4080e74f"
484+
}
485+
}
486+
}
487+
}';
488+
$payload = JWEEncryption::decryptPayload($encryptedPayload, $config);
489+
echo (json_encode(json_decode($payload), JSON_PRETTY_PRINT));
490+
```
491+
492+
Output:
493+
```json
494+
{
495+
"path": {
496+
"to": {
497+
"foo": {
498+
"sensitiveField1": "sensitiveValue1",
499+
"sensitiveField2": "sensitiveValue2"
500+
}
501+
}
502+
}
503+
}
504+
```
505+
506+
#### Encrypting Entire Payloads <a name="encrypting-entire-payloads-jwe"></a>
507+
508+
Entire payloads can be encrypted using the '$' operator as encryption path:
509+
510+
```php
511+
use Mastercard\Developer\Encryption;
512+
// …
513+
$config = JWEConfigBuilder::aFieldLevelEncryptionConfig()
514+
->withEncryptionCertificate(encryptionCertificate)
515+
->withEncryptionPath('$', '$')
516+
->withEncryptedValueFieldName("encryptedValue")
517+
// …
518+
->build();
519+
```
520+
521+
Example:
522+
```php
523+
use Mastercard\Developer\Encryption;
524+
// …
525+
$payload = '{
526+
"sensitiveField1": "sensitiveValue1",
527+
"sensitiveField2": "sensitiveValue2"
528+
}';
529+
$encryptedPayload = JWEEncryption::encryptPayload($payload, $config);
530+
echo (json_encode(json_decode($encryptedPayload), JSON_PRETTY_PRINT));
531+
```
532+
533+
Output:
534+
```json
535+
{
536+
"encryptedValue": "e5e9340f4d2618d27f8955828c86…379b13901a3b1e2efed616b6750a90fd379515"
537+
}
538+
```
539+
540+
#### Decrypting Entire Payloads <a name="decrypting-entire-payloads-jwe"></a>
541+
542+
Entire payloads can be decrypted using the '$' operator as decryption path:
543+
544+
```php
545+
use Mastercard\Developer\Encryption;
546+
// …
547+
$config = JWEEncryptionConfigBuilder::aFieldLevelEncryptionConfig()
548+
->withDecryptionKey(decryptionKey)
549+
->withDecryptionPath('$', '$')
550+
->withEncryptedValueFieldName("encryptedValue")
551+
// …
552+
->build();
553+
```
554+
555+
Example:
556+
```php
557+
use Mastercard\Developer\Encryption;
558+
// …
559+
$encryptedPayload = '{
560+
"encryptedValue": "e5e9340f4d2618d27f8955828c86…379b13901a3b1e2efed616b6750a90fd379515"
561+
}';
562+
$payload = FieldLevelEncryption::decryptPayload($encryptedPayload, $config);
563+
echo (json_encode(json_decode($payload), JSON_PRETTY_PRINT));
564+
```
565+
566+
Output:
567+
```json
568+
{
569+
"sensitiveField1": "sensitiveValue1",
570+
"sensitiveField2": "sensitiveValue2"
571+
}
572+
```
573+
574+
575+
390576
### Integrating with OpenAPI Generator API Client Libraries <a name="integrating-with-openapi-generator-api-client-libraries"></a>
391577

392578
[OpenAPI Generator](https://github.com/OpenAPITools/openapi-generator) generates API client libraries from [OpenAPI Specs](https://github.com/OAI/OpenAPI-Specification).
@@ -410,7 +596,7 @@ See also:
410596
* [OpenAPI Generator CLI Installation](https://openapi-generator.tech/docs/installation/)
411597
* [CONFIG OPTIONS for php](https://github.com/OpenAPITools/openapi-generator/blob/master/docs/generators/php.md)
412598

413-
##### Usage of the `PsrHttpMessageEncryptionInterceptor`
599+
##### Usage of the `PsrHttpMessageEncryptionInterceptor` for Field Level Encryption
414600

415601
```php
416602
use GuzzleHttp;
@@ -436,3 +622,29 @@ $config->setHost('https://sandbox.api.mastercard.com');
436622
$serviceApi = new ServiceApi($client, $config);
437623
// …
438624
```
625+
##### Usage of the `PsrHttpMessageEncryptionInterceptor` for JWE Encryption
626+
627+
```php
628+
use GuzzleHttp;
629+
use OpenAPI\Client\Api\ServiceApi;
630+
use OpenAPI\Client\Configuration
631+
use Mastercard\Developer\Signers\PsrHttpMessageSigner;
632+
use Mastercard\Developer\Interceptors\PsrHttpMessageEncryptionInterceptor;
633+
// …
634+
635+
$stack = new GuzzleHttp\HandlerStack();
636+
$stack->setHandler(new GuzzleHttp\Handler\CurlHandler());
637+
$jweEncryptionConfig = JWEEncryptionConfigBuilder::aJWEEncryptionConfig()
638+
// …
639+
->build();
640+
$jweEncryptionInterceptor = new PsrHttpMessageEncryptionInterceptor($jweEncryptionConfig);
641+
$stack->push(GuzzleHttp\Middleware::mapRequest([$jweEncryptionInterceptor, 'interceptRequest']));
642+
$stack->push(GuzzleHttp\Middleware::mapResponse([$jweEncryptionInterceptor, 'interceptResponse']));
643+
$stack->push(GuzzleHttp\Middleware::mapRequest([new PsrHttpMessageSigner($consumerKey, $signingKey), 'sign']));
644+
$options = ['handler' => $stack];
645+
$client = new GuzzleHttp\Client($options);
646+
$config = new Configuration();
647+
$config->setHost('https://sandbox.api.mastercard.com');
648+
$serviceApi = new ServiceApi($client, $config);
649+
// …
650+
```

0 commit comments

Comments
 (0)