11package com .mastercard .developer .signers ;
22
3+ import com .fasterxml .jackson .databind .ObjectMapper ;
34import 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 ;
48import org .springframework .web .reactive .function .client .ClientRequest ;
59import 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+ }
0 commit comments