Skip to content

Commit 0c85086

Browse files
Merge pull request #56 from Mastercard/feature/fix-webflux-signer
Fixing Webflux Signer
2 parents 78f4e15 + 63ab624 commit 0c85086

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ signer.sign(request);
145145
WebClient client = WebClient.create();
146146
ClientRequest request = ClientRequest.create(HttpMethod.POST, URI.create("https://api.mastercard.com/service"))
147147
.header("Content-Type", MediaType.APPLICATION_JSON_VALUE)
148-
.body(BodyInserters.fromValue(yourRequestObject))
148+
.body(BodyInserters.fromValue(new BodyInserterWrapper(yourRequestObject)))
149149
.build();
150150

151151
SpringWebfluxSigner signer = new SpringWebfluxSigner(consumerKey, signingKey);

src/main/java/com/mastercard/developer/signers/SpringWebfluxSigner.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package com.mastercard.developer.signers;
22

3+
import com.fasterxml.jackson.databind.ObjectMapper;
34
import com.mastercard.developer.oauth.OAuth;
5+
import org.springframework.http.ReactiveHttpOutputMessage;
6+
import org.springframework.web.reactive.function.BodyInserter;
7+
import org.springframework.web.reactive.function.BodyInserters;
48
import org.springframework.web.reactive.function.client.ClientRequest;
59
import reactor.core.publisher.Mono;
610

@@ -19,12 +23,36 @@ public SpringWebfluxSigner(String consumerKey, PrivateKey signingKey) {
1923
public ClientRequest sign(ClientRequest request) throws Exception {
2024
URI uri = request.url();
2125
String method = request.method().name();
26+
BodyInserterWrapper<Object> bodyInserterWrapper = (BodyInserterWrapper<Object>) request.body();
27+
String payload = new ObjectMapper().writeValueAsString(bodyInserterWrapper.getBody());
2228

23-
String authHeader = OAuth.getAuthorizationHeader(uri, method, request.body().toString(), charset, consumerKey, signingKey);
29+
String authHeader = OAuth.getAuthorizationHeader(uri, method, payload, charset, consumerKey, signingKey);
2430

2531
// Add auth header
2632
return Mono.just(ClientRequest.from(request)
2733
.headers(headers -> headers.add(OAuth.AUTHORIZATION_HEADER_NAME, authHeader))
2834
.build()).block();
2935
}
3036
}
37+
38+
class BodyInserterWrapper<T> implements BodyInserter<T, ReactiveHttpOutputMessage> {
39+
private final T body;
40+
private final BodyInserter<T, ReactiveHttpOutputMessage> delegate;
41+
42+
public BodyInserterWrapper(T body) {
43+
this.body = body;
44+
this.delegate = BodyInserters.fromValue(body);
45+
}
46+
47+
@Override
48+
public Mono<Void> insert(
49+
ReactiveHttpOutputMessage outputMessage,
50+
BodyInserter.Context context
51+
) {
52+
return delegate.insert(outputMessage, context);
53+
}
54+
55+
public T getBody() {
56+
return body;
57+
}
58+
}

src/test/java/com/mastercard/developer/signers/SpringWebfluxSignerTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@ public void testSign_ShouldAddOAuth1HeaderToPostRequest() throws Exception {
2424
// GIVEN
2525
PrivateKey signingKey = getTestSigningKey();
2626
String consumerKey = "Some key";
27-
MediaType jsonMediaType = MediaType.parse("application/json; charset=" + UTF8_CHARSET.name());
28-
RequestBody body = RequestBody.create(jsonMediaType, "{\"foo\":\"bår\"}");
29-
BodyInserter<RequestBody, ReactiveHttpOutputMessage> inserter = BodyInserters.fromValue(body);
30-
ClientRequest request = ClientRequest.create(HttpMethod.GET, URI.create("https://api.mastercard.com/service")).body(inserter).build();
27+
BodyInserterWrapper bodyWrapper = new BodyInserterWrapper("{\"foo\":\"bår\"}");
28+
ClientRequest request = ClientRequest.create(HttpMethod.GET, URI.create("https://api.mastercard.com/service")).body(bodyWrapper).build();
3129

3230
// WHEN
3331
SpringWebfluxSigner instanceUnderTest = new SpringWebfluxSigner(consumerKey, signingKey);

0 commit comments

Comments
 (0)