Skip to content

Commit 7c854f0

Browse files
Updating README to reflect JWE support
1 parent cab1d86 commit 7c854f0

File tree

1 file changed

+192
-18
lines changed

1 file changed

+192
-18
lines changed

README.md

Lines changed: 192 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
* [Selecting a JSON Engine](#selecting-a-json-engine)
1919
* [Loading the Encryption Certificate](#loading-the-encryption-certificate)
2020
* [Loading the Decryption Key](#loading-the-decryption-key)
21-
* [Performing Field Level Encryption and Decryption](#performing-field-level-encryption-and-decryption)
22-
* [Integrating with OpenAPI Generator API Client Libraries](#integrating-with-openapi-generator-api-client-libraries)
21+
* [Performing Encryption and Decryption](#performing-encryption-and-decryption)
22+
* [Integrating with OpenAPI Generator API Client Libraries](#integrating-with-openapi-generator-api-client-libraries) // Make Generic
2323

2424
## Overview <a name="overview"></a>
2525
Library for Mastercard API compliant payload encryption/decryption.
2626

2727
### Compatibility <a name="compatibility"></a>
28-
Java 7+
28+
Java 8+
2929

3030
### References <a name="references"></a>
3131
<img src="https://user-images.githubusercontent.com/3964455/55345820-c520a280-54a8-11e9-8235-407199fa1d97.png" alt="Encryption of sensitive data" width="75%" height="75%"/>
@@ -69,11 +69,11 @@ This library requires one of the following dependencies to be added to your clas
6969
* [Jettison](https://search.maven.org/artifact/org.codehaus.jettison/jettison) 1.0+
7070
* [Org JSON](https://search.maven.org/artifact/org.json/json) 20070829+
7171

72-
You can either let the library choose for you, or force the one to be used by calling `withJsonEngine` on the `FieldLevelEncryption` class.
72+
You can either let the library choose for you, or force the one to be used by calling `withJsonEngine` on the `JsonParser` class.
7373
Example:
7474

7575
```java
76-
FieldLevelEncryption.withJsonEngine(new JettisonJsonEngine());
76+
JsonParser.withJsonEngine(new JettisonJsonEngine());
7777
```
7878

7979
Available engine classes:
@@ -116,7 +116,180 @@ Supported RSA key formats:
116116
* PKCS#8 PEM (starts with "-----BEGIN PRIVATE KEY-----")
117117
* Binary DER-encoded PKCS#8
118118

119-
### Performing Field Level Encryption and Decryption <a name="performing-field-level-encryption-and-decryption"></a>
119+
### Performing Encryption and Decryption <a name="performing-encryption-and-decryption"></a>
120+
121+
+ [Introduction](#introduction)
122+
+ [JWE Encryption and Decryption](#jwe-encryption-and-decryption)
123+
+ [Field Level Encryption and Decryption](#field-level-encryption-and-decryption)
124+
125+
#### Introduction <a name="introduction"></a>
126+
127+
This library supports 2 different types of encryption/decryption. Field level encryption (deprecated) and JWE encryption.
128+
129+
#### JWE Encryption <a name="jwe-encryption-and-decryption"></a>
130+
131+
+ [Introduction](#jwe-introduction)
132+
+ [Configuring the JWE Encryption](#configuring-the-jwe-encryption)
133+
+ [Performing JWE Encryption](#performing-jwe-encryption)
134+
+ [Performing JWE Decryption](#performing-jwe-decryption)
135+
+ [Encrypting Entire JWE Payloads](#encrypting-entire-jwe-payloads)
136+
+ [Decrypting Entire JWE Payloads](#decrypting-entire-jwe-payloads)
137+
138+
#### Introduction <a name="jwe-introduction"></a>
139+
140+
The core methods responsible for payload encryption and decryption are `encryptPayload` and `decryptPayload` in the `JweEncryption` class.
141+
142+
* `encryptPayload` usage:
143+
```java
144+
String encryptedRequestPayload = JweEncryption.encryptPayload(requestPayload, config);
145+
146+
```
147+
148+
* `decryptPayload` usage:
149+
```java
150+
String responsePayload = JweEncryption.decryptPayload(encryptedResponsePayload, config);
151+
```
152+
153+
#### Configuring the JWE Encryption <a name="configuring-the-jwe-encryption"></a>
154+
Use the `FieldLevelEncryptionConfigBuilder` to create `FieldLevelEncryptionConfig` instances. Example:
155+
```java
156+
EncryptionConfig config = JweConfigBuilder.aJweEncryptionConfig()
157+
.withEncryptionCertificate(encryptionCertificate)
158+
.withDecryptionKey(decryptionKey)
159+
.withEncryptionPath("$.path.to.foo", "$.path.to.encryptedFoo")
160+
.withDecryptionPath("$.path.to.encryptedFoo", "$.path.to.foo")
161+
.withEncryptedValueFieldName("encryptedValue")
162+
.build();
163+
```
164+
165+
See also:
166+
* [Service Configurations for Client Encryption Java](https://github.com/Mastercard/client-encryption-java/wiki/Service-Configurations-for-Client-Encryption-Java)
167+
168+
#### Performing JWE Encryption <a name="performing-jwe-encryption"></a>
169+
170+
Call `JweEncryption.encryptPayload` with a JSON request payload and a `JweConfig` instance.
171+
172+
Example using the configuration [above](#configuring-the-jwe-encryption):
173+
```java
174+
String payload = "{" +
175+
" \"path\": {" +
176+
" \"to\": {" +
177+
" \"foo\": {" +
178+
" \"sensitiveField1\": \"sensitiveValue1\"," +
179+
" \"sensitiveField2\": \"sensitiveValue2\"" +
180+
" }" +
181+
" }" +
182+
" }" +
183+
"}";
184+
String encryptedPayload = JweEncryption.encryptPayload(payload, config);
185+
System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(new JsonParser().parse(encryptedPayload)));
186+
```
187+
188+
Output:
189+
```json
190+
{
191+
"path": {
192+
"to": {
193+
"encryptedFoo": {
194+
"encryptedValue": "eyJraWQiOiI3NjFiMDAzYzFlYWRlM(...)==.Y+oPYKZEMTKyYcSIVEgtQw=="
195+
}
196+
}
197+
}
198+
}
199+
```
200+
201+
#### Performing Decryption <a name="performing-jwe-decryption"></a>
202+
203+
Call `JweEncryption.decryptPayload` with a JSON response payload and a `JweConfig` instance.
204+
205+
Example using the configuration [above](#configuring-the-jwe-encryption):
206+
```java
207+
String encryptedPayload = "{" +
208+
" \"path\": {" +
209+
" \"to\": {" +
210+
" \"encryptedFoo\": {" +
211+
" \"encryptedValue\": \"eyJraWQiOiI3NjFiMDAzYzFlYWRlM(...)==.Y+oPYKZEMTKyYcSIVEgtQw==\"" +
212+
" }" +
213+
" }" +
214+
" }" +
215+
"}";
216+
String payload = JweEncryption.decryptPayload(encryptedPayload, config);
217+
System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(new JsonParser().parse(payload)));
218+
```
219+
220+
Output:
221+
```json
222+
{
223+
"path": {
224+
"to": {
225+
"foo": {
226+
"sensitiveField1": "sensitiveValue1",
227+
"sensitiveField2": "sensitiveValue2"
228+
}
229+
}
230+
}
231+
}
232+
```
233+
234+
#### Encrypting Entire JWE Payloads <a name="encrypting-entire-jwe-payloads"></a>
235+
236+
Entire payloads can be encrypted using the "$" operator as encryption path:
237+
238+
```java
239+
JweConfig config = JweConfigBuilder.aJweEncryptionConfig()
240+
.withEncryptionCertificate(encryptionCertificate)
241+
.withEncryptionPath("$", "$")
242+
// ...
243+
.build();
244+
```
245+
246+
Example:
247+
```java
248+
String payload = "{" +
249+
" \"sensitiveField1\": \"sensitiveValue1\"," +
250+
" \"sensitiveField2\": \"sensitiveValue2\"" +
251+
"}";
252+
String encryptedPayload = FieldLevelEncryption.encryptPayload(payload, config);
253+
System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(new JsonParser().parse(encryptedPayload)));
254+
```
255+
256+
Output:
257+
```json
258+
{
259+
"encryptedValue": "eyJraWQiOiI3NjFiMDAzYzFlYWRlM(...)==.Y+oPYKZEMTKyYcSIVEgtQw=="
260+
}
261+
```
262+
263+
#### Decrypting Entire JWE Payloads <a name="decrypting-entire-jwe-payloads"></a>
264+
265+
Entire payloads can be decrypted using the "$" operator as decryption path:
266+
267+
```java
268+
JweConfig config = JweConfigBuilder.aJweEncryptionConfig()
269+
.withDecryptionKey(decryptionKey)
270+
.withDecryptionPath("$", "$")
271+
// ...
272+
.build();
273+
```
274+
275+
Example:
276+
```java
277+
String encryptedPayload = "{" +
278+
" \"encryptedValue\": \"eyJraWQiOiI3NjFiMDAzYzFlYWRlM(...)==.Y+oPYKZEMTKyYcSIVEgtQw==\"" +
279+
"}";
280+
String payload = JweEncryption.decryptPayload(encryptedPayload, config);
281+
System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(new JsonParser().parse(payload)));
282+
```
283+
284+
Output:
285+
```json
286+
{
287+
"sensitiveField1": "sensitiveValue1",
288+
"sensitiveField2": "sensitiveValue2"
289+
}
290+
```
291+
292+
#### Performing Field Level Encryption <a name="field-level-encryption-and-decryption"></a>
120293

121294
+ [Introduction](#introduction)
122295
+ [Configuring the Field Level Encryption](#configuring-the-field-level-encryption)
@@ -133,6 +306,7 @@ The core methods responsible for payload encryption and decryption are `encryptP
133306
* `encryptPayload` usage:
134307
```java
135308
String encryptedRequestPayload = FieldLevelEncryption.encryptPayload(requestPayload, config);
309+
136310
```
137311

138312
* `decryptPayload` usage:
@@ -433,12 +607,12 @@ See also:
433607
</configuration>
434608
```
435609

436-
##### Usage of the `OkHttp2FieldLevelEncryptionInterceptor` (OpenAPI Generator 3.3.x)
610+
##### Usage of the `OkHttp2EncryptionInterceptor` (OpenAPI Generator 3.3.x)
437611
```java
438612
ApiClient client = new ApiClient();
439613
client.setBasePath("https://sandbox.api.mastercard.com");
440614
List<Interceptor> interceptors = client.getHttpClient().interceptors();
441-
interceptors.add(new OkHttp2FieldLevelEncryptionInterceptor(config));
615+
interceptors.add(OkHttp2EncryptionInterceptor.from(config));
442616
interceptors.add(new OkHttp2OAuth1Interceptor(consumerKey, signingKey));
443617
ServiceApi serviceApi = new ServiceApi(client);
444618
// ...
@@ -451,7 +625,7 @@ client.setBasePath("https://sandbox.api.mastercard.com");
451625
client.setHttpClient(
452626
client.getHttpClient()
453627
.newBuilder()
454-
.addInterceptor(new OkHttpFieldLevelEncryptionInterceptor(config))
628+
.addInterceptor(OkHttpFieldLevelEncryptionInterceptor.from(config))
455629
.addInterceptor(new OkHttpOAuth1Interceptor(consumerKey, signingKey))
456630
.build()
457631
);
@@ -470,7 +644,7 @@ ServiceApi serviceApi = new ServiceApi(client);
470644
</configuration>
471645
```
472646

473-
##### Usage of `OpenFeignFieldLevelEncryptionEncoder` and `OpenFeignFieldLevelEncryptionDecoder`
647+
##### Usage of `OpenFeignEncoderExecutor` and `OpenFeignDecoderExecutor`
474648
```java
475649
ApiClient client = new ApiClient();
476650
ObjectMapper objectMapper = client.getObjectMapper();
@@ -479,8 +653,8 @@ Feign.Builder feignBuilder = client.getFeignBuilder();
479653
ArrayList<RequestInterceptor> interceptors = new ArrayList<>();
480654
interceptors.add(new OpenFeignOAuth1Interceptor(consumerKey, signingKey, client.getBasePath()));
481655
feignBuilder.requestInterceptors(interceptors);
482-
feignBuilder.encoder(new OpenFeignFieldLevelEncryptionEncoder(config, new FormEncoder(new JacksonEncoder(objectMapper))));
483-
feignBuilder.decoder(new OpenFeignFieldLevelEncryptionDecoder(config, new JacksonDecoder(objectMapper)));
656+
feignBuilder.encoder(OpenFeignEncoderExecutor.from(config, new FormEncoder(new JacksonEncoder(objectMapper))));
657+
feignBuilder.decoder(OpenFeignDecoderExecutor.from(config, new JacksonDecoder(objectMapper)));
484658
ServiceApi serviceApi = client.buildClient(ServiceApi.class);
485659
// ...
486660
```
@@ -496,13 +670,13 @@ ServiceApi serviceApi = client.buildClient(ServiceApi.class);
496670
</configuration>
497671
```
498672

499-
##### Usage of the `OkHttp2FieldLevelEncryptionInterceptor`
673+
##### Usage of the `OkHttp2EncryptionInterceptor`
500674
```java
501675
ApiClient client = new ApiClient();
502676
RestAdapter.Builder adapterBuilder = client.getAdapterBuilder();
503677
adapterBuilder.setEndpoint("https://sandbox.api.mastercard.com");
504678
List<Interceptor> interceptors = client.getOkClient().interceptors();
505-
interceptors.add(new OkHttp2FieldLevelEncryptionInterceptor(config));
679+
interceptors.add(OkHttp2EncryptionInterceptor.from(config));
506680
interceptors.add(new OkHttp2OAuth1Interceptor(consumerKey, signingKey));
507681
ServiceApi serviceApi = client.createService(ServiceApi.class);
508682
// ...
@@ -519,13 +693,13 @@ ServiceApi serviceApi = client.createService(ServiceApi.class);
519693
</configuration>
520694
```
521695

522-
##### Usage of the `OkHttpFieldLevelEncryptionInterceptor`
696+
##### Usage of the `OkHttpEncryptionInterceptor`
523697
```java
524698
ApiClient client = new ApiClient();
525699
Retrofit.Builder adapterBuilder = client.getAdapterBuilder();
526700
adapterBuilder.baseUrl("https://sandbox.api.mastercard.com");
527701
OkHttpClient.Builder okBuilder = client.getOkBuilder();
528-
okBuilder.addInterceptor(new OkHttpFieldLevelEncryptionInterceptor(config));
702+
okBuilder.addInterceptor(OkHttpEncryptionInterceptor.from(config));
529703
okBuilder.addInterceptor(new OkHttpOAuth1Interceptor(consumerKey, signingKey));
530704
ServiceApi serviceApi = client.createService(ServiceApi.class);
531705
// ...
@@ -542,13 +716,13 @@ ServiceApi serviceApi = client.createService(ServiceApi.class);
542716
</configuration>
543717
```
544718

545-
##### Usage of `HttpExecuteFieldLevelEncryptionInterceptor` and `HttpExecuteInterceptorChain`
719+
##### Usage of `HttpExecuteEncryptionInterceptor` and `HttpExecuteInterceptorChain`
546720
```java
547721
HttpRequestInitializer initializer = new HttpRequestInitializer() {
548722
@Override
549723
public void initialize(HttpRequest request) {
550724
HttpExecuteOAuth1Interceptor authenticationInterceptor = new HttpExecuteOAuth1Interceptor(consumerKey, signingKey);
551-
HttpExecuteFieldLevelEncryptionInterceptor encryptionInterceptor = new HttpExecuteFieldLevelEncryptionInterceptor(config);
725+
HttpExecuteEncryptionInterceptor encryptionInterceptor = HttpExecuteEncryptionInterceptor.from(config);
552726
request.setInterceptor(new HttpExecuteInterceptorChain(Arrays.asList(encryptionInterceptor, authenticationInterceptor)));
553727
request.setResponseInterceptor(encryptionInterceptor);
554728
}

0 commit comments

Comments
 (0)