File tree Expand file tree Collapse file tree 4 files changed +76
-1
lines changed
src/main/java/io/bspk/httpsig Expand file tree Collapse file tree 4 files changed +76
-1
lines changed Original file line number Diff line number Diff line change 7575 <version >2.5</version >
7676 <scope >provided</scope >
7777 </dependency >
78+ <dependency >
79+ <groupId >org.apache.commons</groupId >
80+ <artifactId >commons-lang3</artifactId >
81+ <version >3.12.0</version >
82+ </dependency >
7883 </dependencies >
7984 <distributionManagement >
8085 <snapshotRepository >
Original file line number Diff line number Diff line change 1+ package io .bspk .httpsig ;
2+
3+ import org .apache .commons .lang3 .RandomStringUtils ;
4+
5+ /**
6+ * @author jricher
7+ *
8+ */
9+ public interface MessageWrapper {
10+
11+ default void addSignature (SignatureParameters signatureInput , byte [] signature ) {
12+ String sigId = RandomStringUtils .randomAlphabetic (5 ).toLowerCase ();
13+
14+ addSignature (sigId , signatureInput , signature );
15+ }
16+
17+ void addSignature (String signatureId , SignatureParameters signatureInput , byte [] signature );
18+
19+ }
Original file line number Diff line number Diff line change @@ -173,6 +173,9 @@ public InnerList toComponentValue() {
173173 return list ;
174174 }
175175
176+ /**
177+ * Add a component without parameters.
178+ */
176179 public SignatureParameters addComponentIdentifier (String identifier ) {
177180 if (!identifier .startsWith ("@" )) {
178181 componentIdentifiers .add (StringItem .valueOf (identifier .toLowerCase ()));
@@ -182,6 +185,10 @@ public SignatureParameters addComponentIdentifier(String identifier) {
182185 return this ;
183186 }
184187
188+ /**
189+ * Add a component with optional parameters. Field components are assumed to be
190+ * already set to lowercase.
191+ */
185192 public SignatureParameters addComponentIdentifier (StringItem identifier ) {
186193 componentIdentifiers .add (identifier );
187194 return this ;
@@ -196,7 +203,14 @@ public boolean containsComponentIdentifier(String identifier) {
196203
197204 // does not ignore parameters
198205 public boolean containsComponentIdentifier (StringItem identifier ) {
199- return componentIdentifiers .contains (identifier );
206+ return componentIdentifiers .stream ()
207+ .filter ((i ) -> {
208+ return
209+ i .get ().equals (identifier .get ())
210+ && i .getParams ().equals (identifier .getParams ());
211+ })
212+ .findAny ()
213+ .isPresent ();
200214 }
201215
202216 /**
Original file line number Diff line number Diff line change 1+ package io .bspk .httpsig .spring ;
2+
3+ import java .util .Map ;
4+
5+ import org .greenbytes .http .sfv .ByteSequenceItem ;
6+ import org .greenbytes .http .sfv .Dictionary ;
7+ import org .springframework .http .HttpRequest ;
8+
9+ import io .bspk .httpsig .MessageWrapper ;
10+ import io .bspk .httpsig .SignatureParameters ;
11+
12+ /**
13+ * @author jricher
14+ *
15+ */
16+ public class RestTemplateMessageWrapper implements MessageWrapper {
17+
18+ private HttpRequest request ;
19+
20+ public RestTemplateMessageWrapper (HttpRequest request ) {
21+ this .request = request ;
22+ }
23+
24+ @ Override
25+ public void addSignature (String signatureId , SignatureParameters signatureInput , byte [] signature ) {
26+ Dictionary sigHeader = Dictionary .valueOf (Map .of (
27+ signatureId , ByteSequenceItem .valueOf (signature )));
28+
29+ Dictionary sigInputHeader = Dictionary .valueOf (Map .of (
30+ signatureId , signatureInput .toComponentValue ()));
31+
32+ request .getHeaders ().add ("Signature" , sigHeader .serialize ());
33+ request .getHeaders ().add ("Signature-Input" , sigInputHeader .serialize ());
34+ }
35+
36+
37+ }
You can’t perform that action at this time.
0 commit comments