Skip to content

Commit 964f24c

Browse files
Revert "feat/add support for rsa pss"
1 parent 11b05f7 commit 964f24c

33 files changed

+47
-1366
lines changed

README.md

Lines changed: 12 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,7 @@ URI uri = URI.create("https://sandbox.api.mastercard.com/service");
8787
String method = "POST";
8888
String payload = "Hello world!";
8989
Charset charset = StandardCharsets.UTF_8;
90-
String authHeader = OAuth.getAuthorizationHeader(uri, method, payload, charset, consumerKey, signingKey); // uses RSA_SHA256 as the default signature method
91-
```
92-
Alternatively, you can specify the signature method as well:
93-
```java
94-
String authHeader = OAuth.getAuthorizationHeader(uri, method, payload, charset, consumerKey, signingKey, SignatureMethod.RSA_PSS_SHA256);
90+
String authHeader = OAuth.getAuthorizationHeader(uri, method, payload, charset, consumerKey, signingKey);
9591
```
9692

9793
### Signing HTTP Client Request Objects <a name="signing-http-client-request-objects"></a>
@@ -117,13 +113,7 @@ HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
117113
con.setRequestMethod("POST");
118114
con.setRequestProperty("Content-Type", "application/json; charset=" + charset.name());
119115

120-
HttpsUrlConnectionSigner signer = new HttpsUrlConnectionSigner(charset, consumerKey, signingKey); // uses RSA_SHA256 as the default signature method
121-
signer.sign(con, payload);
122-
```
123-
124-
You can also specify the signature method when creating the signer object:
125-
```java
126-
HttpsUrlConnectionSigner signer = new HttpsUrlConnectionSigner(charset, consumerKey, signingKey, SignatureMethod.RSA_PSS_SHA256);
116+
HttpsUrlConnectionSigner signer = new HttpsUrlConnectionSigner(charset, consumerKey, signingKey);
127117
signer.sign(con, payload);
128118
```
129119

@@ -135,9 +125,7 @@ HttpClient httpClient = HttpClientBuilder.create().build();
135125
HttpPost httpPost = new HttpPost("https://sandbox.api.mastercard.com/service");
136126
httpPost.setEntity(new StringEntity(payload, ContentType.APPLICATION_JSON));
137127

138-
ApacheHttpClient4Signer signer = new ApacheHttpClient4Signer(consumerKey, signingKey); // uses RSA_SHA256 as the default signature method
139-
// You can also specify the signature method:
140-
// ApacheHttpClient4Signer signer = new ApacheHttpClient4Signer(consumerKey, signingKey, SignatureMethod.RSA_PSS_SHA256);
128+
ApacheHttpClient4Signer signer = new ApacheHttpClient4Signer(consumerKey, signingKey);
141129
signer.sign(httpPost);
142130
```
143131

@@ -152,9 +140,7 @@ Request.Builder request = new Request.Builder()
152140
.url("https://sandbox.api.mastercard.com/service")
153141
.post(body);
154142

155-
OkHttpSigner signer = new OkHttpSigner(consumerKey, signingKey); // uses RSA_SHA256 as the default signature method
156-
// You can also specify the signature method:
157-
// OkHttpSigner signer = new OkHttpSigner(consumerKey, signingKey, SignatureMethod.RSA_PSS_SHA256);
143+
OkHttpSigner signer = new OkHttpSigner(consumerKey, signingKey);
158144
signer.sign(request);
159145
```
160146

@@ -166,9 +152,7 @@ ClientRequest request = ClientRequest.create(HttpMethod.POST, URI.create("https:
166152
.body(BodyInserters.fromValue(new BodyInserterWrapper(yourRequestObject)))
167153
.build();
168154

169-
SpringWebfluxSigner signer = new SpringWebfluxSigner(consumerKey, signingKey); // uses RSA_SHA256 as the default signature method
170-
// You can also specify the signature method:
171-
// SpringWebfluxSigner signer = new SpringWebfluxSigner(consumerKey, signingKey, SignatureMethod.RSA_PSS_SHA256);
155+
SpringWebfluxSigner signer = new SpringWebfluxSigner(consumerKey, signingKey);
172156
ClientRequest signedRequest = signer.sign(request);
173157
client.exchange(signedRequest);
174158
```
@@ -209,11 +193,7 @@ See also:
209193
ApiClient client = new ApiClient();
210194
client.setBasePath("https://sandbox.api.mastercard.com");
211195
List<Interceptor> interceptors = client.getHttpClient().interceptors();
212-
interceptors.add(
213-
new OkHttp2OAuth1Interceptor(consumerKey, signingKey) // uses RSA_SHA256 as the default signature method
214-
// if you want to specify the signature method
215-
// new OkHttp2OAuth1Interceptor(consumerKey, signingKey, SignatureMethod.RSA_PSS_SHA256)
216-
);
196+
interceptors.add(new OkHttp2OAuth1Interceptor(consumerKey, signingKey));
217197
ServiceApi serviceApi = new ServiceApi(client);
218198
// ...
219199
```
@@ -227,11 +207,7 @@ client.setHttpClient(
227207
client.getHttpClient()
228208
.newBuilder()
229209
.proxy(proxy) // Optional proxy
230-
.addInterceptor(
231-
new OkHttpOAuth1Interceptor(consumerKey, signingKey) // uses RSA_SHA256 as the default signature method
232-
// if you want to specify the signature method
233-
// new OkHttpOAuth1Interceptor(consumerKey, signingKey, SignatureMethod.RSA_PSS_SHA256)
234-
)
210+
.addInterceptor(new OkHttpOAuth1Interceptor(consumerKey, signingKey))
235211
.build()
236212
);
237213
ServiceApi serviceApi = new ServiceApi(client);
@@ -255,11 +231,7 @@ ApiClient client = new ApiClient();
255231
client.setBasePath("https://sandbox.api.mastercard.com");
256232
Feign.Builder feignBuilder = client.getFeignBuilder();
257233
ArrayList<RequestInterceptor> interceptors = new ArrayList<>();
258-
interceptors.add(
259-
new OpenFeignOAuth1Interceptor(consumerKey, signingKey, client.getBasePath()) // uses RSA_SHA256 as the default signature method
260-
// if you want to specify the signature method
261-
// new OpenFeignOAuth1Interceptor(consumerKey, signingKey, client.getBasePath(), SignatureMethod.RSA_PSS_SHA256)
262-
);
234+
interceptors.add(new OpenFeignOAuth1Interceptor(consumerKey, signingKey, client.getBasePath()));
263235
feignBuilder.requestInterceptors(interceptors);
264236
ServiceApi serviceApi = client.buildClient(ServiceApi.class);
265237
// ...
@@ -282,11 +254,7 @@ ApiClient client = new ApiClient();
282254
RestAdapter.Builder adapterBuilder = client.getAdapterBuilder();
283255
adapterBuilder.setEndpoint("https://sandbox.api.mastercard.com");
284256
List<Interceptor> interceptors = client.getOkClient().interceptors();
285-
interceptors.add(
286-
new OkHttp2OAuth1Interceptor(consumerKey, signingKey) // uses RSA_SHA256 as the default signature method
287-
// if you want to specify the signature method
288-
// new OkHttp2OAuth1Interceptor(consumerKey, signingKey, SignatureMethod.RSA_PSS_SHA256)
289-
);
257+
interceptors.add(new OkHttp2OAuth1Interceptor(consumerKey, signingKey));
290258
ServiceApi serviceApi = client.createService(ServiceApi.class);
291259
// ...
292260
```
@@ -308,11 +276,7 @@ ApiClient client = new ApiClient();
308276
Retrofit.Builder adapterBuilder = client.getAdapterBuilder();
309277
adapterBuilder.baseUrl("https://sandbox.api.mastercard.com");
310278
OkHttpClient.Builder okBuilder = client.getOkBuilder();
311-
okBuilder.addInterceptor(
312-
new OkHttpOAuth1Interceptor(consumerKey, signingKey) // uses RSA_SHA256 as the default signature method
313-
// if you want to specify the signature method
314-
// new OkHttpOAuth1Interceptor(consumerKey, signingKey, SignatureMethod.RSA_PSS_SHA256)
315-
);
279+
okBuilder.addInterceptor(new OkHttpOAuth1Interceptor(consumerKey, signingKey));
316280
ServiceApi serviceApi = client.createService(ServiceApi.class);
317281
// ...
318282
```
@@ -333,11 +297,7 @@ ServiceApi serviceApi = client.createService(ServiceApi.class);
333297
HttpRequestInitializer initializer = new HttpRequestInitializer() {
334298
@Override
335299
public void initialize(HttpRequest request) {
336-
request.setInterceptor(
337-
new HttpExecuteOAuth1Interceptor(consumerKey, signingKey) // uses RSA_SHA256 as the default signature method
338-
// if you want to specify the signature method
339-
// new HttpExecuteOAuth1Interceptor(consumerKey, signingKey, SignatureMethod.RSA_PSS_SHA256)
340-
);
300+
request.setInterceptor(new HttpExecuteOAuth1Interceptor(consumerKey, signingKey));
341301
}
342302
};
343303
ApiClient client = new ApiClient("https://sandbox.api.mastercard.com", null, initializer, null);
@@ -360,11 +320,7 @@ ServiceApi serviceApi = client.serviceApi();
360320
```java
361321
WebClient.Builder webClientBuilder = WebClient.builder()
362322
.baseUrl("https://api.mastercard.com/service")
363-
.filter(
364-
new SpringWebfluxOAuth1Interceptor(consumerKey, signingKey) // uses RSA_SHA256 as the default signature method
365-
// if you want to specify the signature method
366-
// new SpringWebfluxOAuth1Interceptor(consumerKey, signingKey, SignatureMethod.RSA_PSS_SHA256)
367-
);
323+
.filter(new SpringWebfluxOAuth1Interceptor(consumerKey, signingKey));
368324

369325
ApiClient apiClient = new ApiClient(webClientBuilder);
370326
ServiceApi serviceApi = client.serviceApi();

pom.xml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.mastercard.developer</groupId>
88
<artifactId>oauth1-signer</artifactId>
9-
<version>1.6.0</version>
9+
<version>1.5.6</version>
1010
<packaging>jar</packaging>
1111
<description>Zero dependency library for generating a Mastercard API compliant OAuth signature</description>
1212
<url>https://github.com/Mastercard/oauth1-signer-java</url>
@@ -113,12 +113,6 @@
113113
<version>3.27.7</version> <!-- We can't bump this one because of the Java 7 support -->
114114
<scope>test</scope>
115115
</dependency>
116-
<dependency>
117-
<groupId>org.mockito</groupId>
118-
<artifactId>mockito-core</artifactId>
119-
<version>5.21.0</version>
120-
<scope>test</scope>
121-
</dependency>
122116
</dependencies>
123117

124118
<profiles>

src/main/java/com/mastercard/developer/interceptors/HttpExecuteOAuth1Interceptor.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import com.google.api.client.http.HttpExecuteInterceptor;
44
import com.google.api.client.http.HttpRequest;
5-
import com.mastercard.developer.oauth.OAuth;
6-
import com.mastercard.developer.oauth.SignatureMethod;
75
import com.mastercard.developer.signers.GoogleApiClientSigner;
86

97
import java.io.IOException;
@@ -18,11 +16,7 @@ public class HttpExecuteOAuth1Interceptor implements HttpExecuteInterceptor {
1816
private final GoogleApiClientSigner signer;
1917

2018
public HttpExecuteOAuth1Interceptor(String consumerKey, PrivateKey signingKey) {
21-
this(consumerKey, signingKey, OAuth.DEFAULT_SIGNATURE_METHOD);
22-
}
23-
24-
public HttpExecuteOAuth1Interceptor(String consumerKey, PrivateKey signingKey, SignatureMethod signatureMethod) {
25-
this.signer = new GoogleApiClientSigner(consumerKey, signingKey, signatureMethod);
19+
this.signer = new GoogleApiClientSigner(consumerKey, signingKey);
2620
}
2721

2822
public void intercept(HttpRequest request) throws IOException {

src/main/java/com/mastercard/developer/interceptors/OkHttp2OAuth1Interceptor.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.mastercard.developer.interceptors;
22

3-
import com.mastercard.developer.oauth.OAuth;
4-
import com.mastercard.developer.oauth.SignatureMethod;
53
import com.mastercard.developer.signers.OkHttp2Signer;
64
import com.squareup.okhttp.*;
75

@@ -16,11 +14,7 @@ public class OkHttp2OAuth1Interceptor implements Interceptor {
1614
private final OkHttp2Signer signer;
1715

1816
public OkHttp2OAuth1Interceptor(String consumerKey, PrivateKey signingKey) {
19-
this(consumerKey, signingKey, OAuth.DEFAULT_SIGNATURE_METHOD);
20-
}
21-
22-
public OkHttp2OAuth1Interceptor(String consumerKey, PrivateKey signingKey, SignatureMethod signatureMethod) {
23-
this.signer = new OkHttp2Signer(consumerKey, signingKey, signatureMethod);
17+
this.signer = new OkHttp2Signer(consumerKey, signingKey);
2418
}
2519

2620
@Override

src/main/java/com/mastercard/developer/interceptors/OkHttpOAuth1Interceptor.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.mastercard.developer.interceptors;
22

3-
import com.mastercard.developer.oauth.OAuth;
4-
import com.mastercard.developer.oauth.SignatureMethod;
53
import com.mastercard.developer.signers.OkHttpSigner;
64
import okhttp3.*;
75

@@ -16,11 +14,7 @@ public class OkHttpOAuth1Interceptor implements Interceptor {
1614
private final OkHttpSigner signer;
1715

1816
public OkHttpOAuth1Interceptor(String consumerKey, PrivateKey signingKey) {
19-
this(consumerKey, signingKey, OAuth.DEFAULT_SIGNATURE_METHOD);
20-
}
21-
22-
public OkHttpOAuth1Interceptor(String consumerKey, PrivateKey signingKey, SignatureMethod signatureMethod) {
23-
this.signer = new OkHttpSigner(consumerKey, signingKey, signatureMethod);
17+
this.signer = new OkHttpSigner(consumerKey, signingKey);
2418
}
2519

2620
@Override

src/main/java/com/mastercard/developer/interceptors/OpenFeignOAuth1Interceptor.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.mastercard.developer.interceptors;
22

3-
import com.mastercard.developer.oauth.OAuth;
4-
import com.mastercard.developer.oauth.SignatureMethod;
53
import com.mastercard.developer.signers.OpenFeignSigner;
64
import feign.RequestInterceptor;
75
import feign.RequestTemplate;
@@ -17,11 +15,7 @@ public class OpenFeignOAuth1Interceptor implements RequestInterceptor {
1715
private final OpenFeignSigner signer;
1816

1917
public OpenFeignOAuth1Interceptor(String consumerKey, PrivateKey signingKey, String baseUri) {
20-
this(consumerKey, signingKey, baseUri, OAuth.DEFAULT_SIGNATURE_METHOD);
21-
}
22-
23-
public OpenFeignOAuth1Interceptor(String consumerKey, PrivateKey signingKey, String baseUri, SignatureMethod signatureMethod) {
24-
this.signer = new OpenFeignSigner(consumerKey, signingKey, baseUri, signatureMethod);
18+
this.signer = new OpenFeignSigner(consumerKey, signingKey, baseUri);
2519
}
2620

2721
@Override

src/main/java/com/mastercard/developer/interceptors/SpringWebfluxOAuth1Interceptor.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.mastercard.developer.interceptors;
22

3-
import com.mastercard.developer.oauth.OAuth;
4-
import com.mastercard.developer.oauth.SignatureMethod;
53
import com.mastercard.developer.signers.SpringWebfluxSigner;
64
import org.springframework.web.reactive.function.client.ClientRequest;
75
import org.springframework.web.reactive.function.client.ClientResponse;
@@ -19,11 +17,7 @@ public class SpringWebfluxOAuth1Interceptor implements ExchangeFilterFunction {
1917
private final SpringWebfluxSigner signer;
2018

2119
public SpringWebfluxOAuth1Interceptor(String consumerKey, PrivateKey signingKey) {
22-
this(consumerKey, signingKey, OAuth.DEFAULT_SIGNATURE_METHOD);
23-
}
24-
25-
public SpringWebfluxOAuth1Interceptor(String consumerKey, PrivateKey signingKey, SignatureMethod signatureMethod) {
26-
this.signer = new SpringWebfluxSigner(consumerKey, signingKey, signatureMethod);
20+
this.signer = new SpringWebfluxSigner(consumerKey, signingKey);
2721
}
2822

2923
@Override

src/main/java/com/mastercard/developer/oauth/OAuth.java

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,12 @@ private OAuth() {
2424

2525
public static final String EMPTY_STRING = "";
2626
public static final String AUTHORIZATION_HEADER_NAME = "Authorization";
27-
public static final SignatureMethod DEFAULT_SIGNATURE_METHOD = SignatureMethod.RSA_SHA256;
2827

2928
private static final Logger LOG = Logger.getLogger(OAuth.class.getName());
30-
private static final String BODY_HASH_ALGORITHM = "SHA-256";
29+
private static final String HASH_ALGORITHM = "SHA-256";
3130
private static final int NONCE_LENGTH = 16;
3231
private static final String ALPHA_NUMERIC_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
3332

34-
/**
35-
* Creates a Mastercard API compliant OAuth Authorization header, using RSA-SHA256 as the signature method
36-
*
37-
* @param uri Target URI for this request
38-
* @param method HTTP method of the request
39-
* @param payload Payload (nullable)
40-
* @param charset Charset encoding of the request
41-
* @param consumerKey Consumer key set up in a Mastercard Developer Portal project
42-
* @param signingKey The private key that will be used for signing the request that corresponds to the consumerKey
43-
* @return Valid OAuth1.0a signature with a body hash when payload is present
44-
* @see #getAuthorizationHeader(URI, String, String, Charset, String, PrivateKey, SignatureMethod)
45-
*/
46-
public static String getAuthorizationHeader(URI uri, String method, String payload, Charset charset, String consumerKey, PrivateKey signingKey) {
47-
return getAuthorizationHeader(uri, method, payload, charset, consumerKey, signingKey, DEFAULT_SIGNATURE_METHOD);
48-
}
49-
5033
/**
5134
* Creates a Mastercard API compliant OAuth Authorization header
5235
*
@@ -56,19 +39,18 @@ public static String getAuthorizationHeader(URI uri, String method, String paylo
5639
* @param charset Charset encoding of the request
5740
* @param consumerKey Consumer key set up in a Mastercard Developer Portal project
5841
* @param signingKey The private key that will be used for signing the request that corresponds to the consumerKey
59-
* @param signMethod The signature method to use when signing the request
6042
* @return Valid OAuth1.0a signature with a body hash when payload is present
6143
*/
62-
public static String getAuthorizationHeader(URI uri, String method, String payload, Charset charset, String consumerKey, PrivateKey signingKey, SignatureMethod signMethod) {
44+
public static String getAuthorizationHeader(URI uri, String method, String payload, Charset charset, String consumerKey, PrivateKey signingKey) {
6345
TreeMap<String, List<String>> queryParams = extractQueryParams(uri, charset);
6446

6547
HashMap<String, String> oauthParams = new HashMap<>();
6648
oauthParams.put("oauth_consumer_key", consumerKey);
6749
oauthParams.put("oauth_nonce", getNonce());
68-
oauthParams.put("oauth_signature_method", signMethod.getOauthName());
50+
oauthParams.put("oauth_signature_method", "RSA-" + HASH_ALGORITHM.replace("-", ""));
6951
oauthParams.put("oauth_timestamp", getTimestamp());
7052
oauthParams.put("oauth_version", "1.0");
71-
oauthParams.put("oauth_body_hash", getBodyHash(payload, charset, BODY_HASH_ALGORITHM));
53+
oauthParams.put("oauth_body_hash", getBodyHash(payload, charset, HASH_ALGORITHM));
7254

7355
// Combine query and oauth_ parameters into lexicographically sorted string
7456
String paramString = toOauthParamString(queryParams, oauthParams);
@@ -80,7 +62,7 @@ public static String getAuthorizationHeader(URI uri, String method, String paylo
8062
String sbs = getSignatureBaseString(method, baseUri, paramString, charset);
8163

8264
// Signature
83-
String signature = signSignatureBaseString(sbs, signingKey, charset, signMethod);
65+
String signature = signSignatureBaseString(sbs, signingKey, charset);
8466
oauthParams.put("oauth_signature", Util.percentEncode(signature, charset));
8567

8668
return getAuthorizationString(oauthParams);
@@ -270,19 +252,6 @@ static String getBodyHash(String payload, Charset charset, String hashAlg) {
270252
return Util.b64Encode(hash);
271253
}
272254

273-
/**
274-
* Signs the signature base string using an RSA private key and RSA-SHA256 as the signature method.
275-
*
276-
* @param sbs Signature base string formatted as per https://tools.ietf.org/html/rfc5849#section-3.4.1
277-
* @param signingKey Private key of the RSA key pair that was established with the service provider
278-
* @param charset Charset encoding of the request
279-
* @return RSA signature matching the contents of signature base string
280-
* @see #signSignatureBaseString(String, PrivateKey, Charset, SignatureMethod)
281-
*/
282-
static String signSignatureBaseString(String sbs, PrivateKey signingKey, Charset charset) {
283-
return signSignatureBaseString(sbs, signingKey, charset, DEFAULT_SIGNATURE_METHOD);
284-
}
285-
286255
/**
287256
* Signs the signature base string using an RSA private key. The methodology is described at
288257
* https://tools.ietf.org/html/rfc5849#section-3.4.3 but Mastercard uses the stronger SHA-256 algorithm
@@ -291,22 +260,18 @@ static String signSignatureBaseString(String sbs, PrivateKey signingKey, Charset
291260
* @param sbs Signature base string formatted as per https://tools.ietf.org/html/rfc5849#section-3.4.1
292261
* @param signingKey Private key of the RSA key pair that was established with the service provider
293262
* @param charset Charset encoding of the request
294-
* @param signMethod The signature method to use when signing the request
295263
* @return RSA signature matching the contents of signature base string
296264
*/
297-
static String signSignatureBaseString(String sbs, PrivateKey signingKey, Charset charset, SignatureMethod signMethod) {
265+
static String signSignatureBaseString(String sbs, PrivateKey signingKey, Charset charset) {
298266
try {
299-
Signature signer = Signature.getInstance(signMethod.getJcaName());
300-
if(signMethod.getAlgorithmParams() != null) {
301-
signer.setParameter(signMethod.getAlgorithmParams());
302-
}
267+
Signature signer = Signature.getInstance("SHA256withRSA");
303268
signer.initSign(signingKey);
304269
byte[] sbsBytes = sbs.getBytes(charset);
305270
signer.update(sbsBytes);
306271
byte[] signatureBytes = signer.sign();
307272
return Util.b64Encode(signatureBytes);
308273
} catch (GeneralSecurityException e) {
309-
throw new IllegalStateException("Unable to sign with method " + signMethod.getOauthName() + " using the provided key", e);
274+
throw new IllegalStateException("Unable to RSA-SHA256 sign the given string with the provided key", e);
310275
}
311276
}
312277

0 commit comments

Comments
 (0)