@@ -55,12 +55,12 @@ public class HmacSignatureVerifier implements SignatureVerifier {
5555 * @param signatureValueTemplate Template for signature format.
5656 */
5757 public HmacSignatureVerifier (
58- String secretKey ,
59- String signatureHeaderName ,
60- DigestCodec digestCodec ,
61- Function <Request , byte []> requestBytesResolver ,
62- String algorithm ,
63- String signatureValueTemplate
58+ final String secretKey ,
59+ final String signatureHeaderName ,
60+ final DigestCodec digestCodec ,
61+ final Function <Request , byte []> requestBytesResolver ,
62+ final String algorithm ,
63+ final String signatureValueTemplate
6464 ) {
6565
6666 if (secretKey == null || secretKey .trim ().isEmpty ()) {
@@ -73,7 +73,8 @@ public HmacSignatureVerifier(
7373 throw new IllegalArgumentException ("Signature value template cannot be null or Empty." );
7474 }
7575 if (requestBytesResolver == null ) {
76- throw new IllegalArgumentException ("Request signature template resolver function cannot be null." );
76+ throw new IllegalArgumentException (
77+ "Request signature template resolver function cannot be null." );
7778 }
7879 if (digestCodec == null ) {
7980 throw new IllegalArgumentException ("Digest encoding cannot be null." );
@@ -92,22 +93,30 @@ public HmacSignatureVerifier(
9293
9394 /**
9495 * Verifies the HMAC signature of the specified HTTP request.
96+ *
97+ * @param request The HTTP request to verify.
98+ * @return A CompletableFuture containing the verification result.
9599 */
96100 @ Override
97- public CompletableFuture <VerificationResult > verifyAsync (Request request ) {
101+ public CompletableFuture <VerificationResult > verifyAsync (final Request request ) {
98102 return CompletableFuture .supplyAsync (() -> {
99103 try {
100104 String headerValue = request .getHeaders ().asSimpleMap ().entrySet ().stream ()
101- .filter (e -> e .getKey () != null && e .getKey ().equalsIgnoreCase (signatureHeaderName ))
102- .map (Map .Entry ::getValue ).findFirst ().orElse (null );
105+ .filter (e -> e .getKey () != null
106+ && e .getKey ().equalsIgnoreCase (signatureHeaderName ))
107+ .map (Map .Entry ::getValue )
108+ .findFirst ()
109+ .orElse (null );
103110
104111 if (headerValue == null ) {
105- return VerificationResult .failure ("Signature header '" + signatureHeaderName + "' is missing." );
112+ return VerificationResult .failure (
113+ "Signature header '" + signatureHeaderName + "' is missing." );
106114 }
107115
108116 byte [] provided = extractSignature (headerValue );
109117 if (provided == null ) {
110- return VerificationResult .failure ("Malformed signature header '" + signatureHeaderName + "'." );
118+ return VerificationResult .failure (
119+ "Malformed signature header '" + signatureHeaderName + "'." );
111120 }
112121
113122 byte [] message = requestBytesResolver .apply (request );
@@ -126,20 +135,24 @@ public CompletableFuture<VerificationResult> verifyAsync(Request request) {
126135 }
127136
128137 /**
129- * Extracts the digest value from the signature header according to the template.
130- * And decode the signature from the header value.
138+ * Extracts the digest value from the signature header according to the template
139+ * and decodes the signature from the header value.
140+ *
141+ * @param headerValue The value of the signature header.
142+ * @return The decoded signature as a byte array, or null if extraction fails.
131143 */
132- private byte [] extractSignature (String headerValue ) {
144+ private byte [] extractSignature (final String headerValue ) {
133145 try {
134146 int index = signatureValueTemplate .indexOf (SIGNATURE_VALUE_PLACEHOLDER );
135- if (index < 0 ) return null ;
147+ if (index < 0 ) { return null ; }
136148
137149 String prefix = signatureValueTemplate .substring (0 , index );
138- String suffix = signatureValueTemplate .substring (index + SIGNATURE_VALUE_PLACEHOLDER .length ());
150+ String suffix = signatureValueTemplate .substring (
151+ index + SIGNATURE_VALUE_PLACEHOLDER .length ());
139152
140153 // find prefix anywhere (case-insensitive)
141154 int prefixAt = indexOfIgnoreCase (headerValue , prefix , 0 );
142- if (prefixAt < 0 ) return null ;
155+ if (prefixAt < 0 ) { return null ; }
143156
144157 int digestStart = prefixAt + prefix .length ();
145158
@@ -149,14 +162,15 @@ private byte[] extractSignature(String headerValue) {
149162 digestEnd = headerValue .length ();
150163 } else {
151164 digestEnd = indexOfIgnoreCase (headerValue , suffix , digestStart );
152- if (digestEnd < 0 ) return null ;
165+ if (digestEnd < 0 ) { return null ; }
153166 }
154167
155- if (digestEnd < digestStart ) return null ;
168+ if (digestEnd < digestStart ) { return null ; }
156169
157170 String digest = headerValue .substring (digestStart , digestEnd ).trim ();
158171 // strip Optional Quotes
159- if (digest .length () >= 2 && digest .charAt (0 ) == '"' && digest .charAt (digest .length () - 1 ) == '"' ) {
172+ if (digest .length () >= 2 && digest .charAt (0 ) == '"'
173+ && digest .charAt (digest .length () - 1 ) == '"' ) {
160174 digest = digest .substring (1 , digest .length () - 1 );
161175 }
162176
@@ -168,13 +182,25 @@ private byte[] extractSignature(String headerValue) {
168182 }
169183
170184 /**
171- * Finds the index of the first case-insensitive occurrence of {@code needle} in {@code haystack} starting from {@code fromIndex}, or -1 if not found.
185+ * Finds the index of the first case-insensitive occurrence of {@code needle} in {@code haystack}
186+ * starting from {@code fromIndex}, or -1 if not found.
187+ *
188+ * @param haystack The string to search in.
189+ * @param needle The substring to search for.
190+ * @param fromIndex The index to start searching from.
191+ * @return The index of the first occurrence, or -1 if not found.
172192 */
173- private static int indexOfIgnoreCase (String haystack , String needle , int fromIndex ) {
174- if (needle .isEmpty ()) return fromIndex ;
193+ private static int indexOfIgnoreCase (
194+ final String haystack ,
195+ final String needle ,
196+ final int fromIndex
197+ ) {
198+ if (needle .isEmpty ()) { return fromIndex ; }
175199 int max = haystack .length () - needle .length ();
176200 for (int i = Math .max (0 , fromIndex ); i <= max ; i ++) {
177- if (haystack .regionMatches (true , i , needle , 0 , needle .length ())) return i ;
201+ if (haystack .regionMatches (true , i , needle , 0 , needle .length ())) {
202+ return i ;
203+ }
178204 }
179205 return -1 ;
180206 }
0 commit comments