Skip to content

Commit 2fd9bc6

Browse files
committed
Merge branch '940-req-res-builders' into 940-jwts-util-migration
2 parents dfcd972 + 5a8a2ac commit 2fd9bc6

File tree

5 files changed

+65
-13
lines changed

5 files changed

+65
-13
lines changed

api/src/main/java/io/jsonwebtoken/security/DecryptAeadRequest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public interface DecryptAeadRequest extends AeadRequest, IvSupplier, DigestSuppl
3030
/**
3131
* Named parameters (setters) used to configure an {@link AeadRequest AeadRequest} instance.
3232
*
33+
* @param <M> the instance type returned for method chaining.
3334
* @since JJWT_RELEASE_VERSION
3435
*/
3536
interface Params<M extends Params<M>> extends AeadRequest.Params<M> {

api/src/main/java/io/jsonwebtoken/security/DecryptionKeyRequest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ interface Builder<K extends Key> extends Params<K, Builder<K>>, io.jsonwebtoken.
6767
* Returns a new {@link DecryptionKeyRequest.Builder} for creating immutable {@link DecryptionKeyRequest}s used to
6868
* get a JWE decryption key via {@link KeyAlgorithm#getDecryptionKey(DecryptionKeyRequest)}.
6969
*
70+
* @param <K> the type of key used to obtain the JWE decryption key via
71+
* {@link KeyAlgorithm#getDecryptionKey(DecryptionKeyRequest)}.
7072
* @return a new {@link DecryptionKeyRequest.Builder} for creating immutable {@link DecryptionKeyRequest}s used to
7173
* get a JWE decryption key via {@link KeyAlgorithm#getDecryptionKey(DecryptionKeyRequest)}.
7274
* @since JJWT_RELEASE_VERSION

api/src/main/java/io/jsonwebtoken/security/HashAlgorithm.java

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,79 @@
4444
*/
4545
public interface HashAlgorithm extends DigestAlgorithm<Request<InputStream>, VerifyDigestRequest> {
4646

47+
/**
48+
* Computes a digest of a request {@link Request#getPayload() payload} {@code InputStream} using configured
49+
* parameters. This is a lambda-style method to execute the request in-line instead of requiring the caller to
50+
* first use a {@link Request.Builder} to construct the request.
51+
*
52+
* <p>Callers are expected to {@link InputStream#close() close} or {@link InputStream#reset() reset} the request
53+
* payload stream if necessary after calling this method.</p>
54+
*
55+
* @param c consumer supporting lambda-style specification of digest {@link Request.Params}.
56+
* @return the computed digest of the request {@link Request#getPayload() payload}.
57+
* @since JJWT_RELEASE_VERSION
58+
*/
4759
default byte[] digest(Consumer<Request.Params<InputStream, ?>> c) {
4860
Request.Builder<InputStream> b = Request.builder();
4961
c.accept(b);
5062
Request<InputStream> r = b.build();
5163
return digest(r);
5264
}
5365

66+
/**
67+
* Computes a digest of the specified {@code is} input stream.
68+
*
69+
* <p>Callers are expected to {@link InputStream#close() close} or {@link InputStream#reset() reset} the payload
70+
* stream if necessary after calling this method.</p>
71+
*
72+
* @param is the {@code InputStream} that will be consumed to compute the digest. Callers are expected to
73+
* {@link InputStream#close() close} or {@link InputStream#reset() reset} the payload stream if necessary
74+
* after calling this method.
75+
* @return the computed digest of the specified {@code is} input stream.
76+
* @since JJWT_RELEASE_VERSION
77+
*/
5478
default byte[] digest(InputStream is) {
5579
return digest(c -> c.payload(is));
5680
}
5781

82+
/**
83+
* Returns {@code true} if the request's specified {@link VerifyDigestRequest#getDigest() digest} matches (equals)
84+
* the algorithm's computed digest of the request {@link VerifyDigestRequest#getPayload() payload}, {@code false}
85+
* otherwise. This is a lambda-style method to execute the request in-line instead of requiring the caller to first
86+
* use a {@link VerifyDigestRequest.Builder} to construct the request.
87+
*
88+
* <p>Callers are expected to {@link InputStream#close() close} or {@link InputStream#reset() reset} the request
89+
* payload stream if necessary after calling this method.</p>
90+
*
91+
* @param c consumer supporting lambda-style specification of {@link VerifyDigestRequest.Params}.
92+
* @return {@code true} if the request's specified {@link VerifyDigestRequest#getDigest() digest} matches (equals)
93+
* the algorithm's computed digest of the request {@link VerifyDigestRequest#getPayload() payload}, {@code false}
94+
* otherwise.
95+
* @since JJWT_RELEASE_VERSION
96+
*/
5897
default boolean verify(Consumer<VerifyDigestRequest.Params<?>> c) {
5998
VerifyDigestRequest.Builder b = VerifyDigestRequest.builder();
6099
c.accept(b);
61100
VerifyDigestRequest r = b.build();
62101
return verify(r);
63102
}
64103

104+
/**
105+
* Returns {@code true} if the specified {@code digest} matches (equals) the algorithm's computed digest of the
106+
* specified {@code is} input stream, {@code false} otherwise.
107+
*
108+
* <p>Callers are expected to {@link InputStream#close() close} or {@link InputStream#reset() reset} the payload
109+
* stream if necessary after calling this method.</p>
110+
*
111+
* @param is the {@code InputStream} that will be consumed to compute the digest. Callers are expected to
112+
* {@link InputStream#close() close} or {@link InputStream#reset() reset} the payload stream if
113+
* necessary after calling this method.
114+
* @param digest the previously-computed digest to compare with the algorithm's computed digest of {@code is}.
115+
* @return {@code true} if the specified {@code digest} matches (equals) the algorithm's computed digest of the
116+
* specified {@code is} input stream, {@code false} otherwise.
117+
* @since JJWT_RELEASE_VERSION
118+
*/
65119
default boolean verify(InputStream is, byte[] digest) {
66120
return verify(c -> c.payload(is).digest(digest));
67121
}
68-
69122
}

api/src/main/java/io/jsonwebtoken/security/KeyRequest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ interface Builder<K extends Key> extends Params<K, Builder<K>>, io.jsonwebtoken.
136136
* Returns a new {@link KeyRequest.Builder} for creating immutable {@link KeyRequest}s used to get a JWE
137137
* encryption key via {@link KeyAlgorithm#getEncryptionKey(KeyRequest)}.
138138
*
139+
* @param <K> the type of {@link java.security.Key Key} used to obtain the JWE content encryption key.
139140
* @return a new {@link KeyRequest.Builder} for creating immutable {@link KeyRequest}s used to get a JWE
140141
* encryption key via {@link KeyAlgorithm#getEncryptionKey(KeyRequest)}.
141142
* @since JJWT_RELEASE_VERSION

impl/src/main/java/io/jsonwebtoken/impl/security/DefaultHashAlgorithm.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515
*/
1616
package io.jsonwebtoken.impl.security;
1717

18-
import io.jsonwebtoken.impl.lang.CheckedFunction;
1918
import io.jsonwebtoken.lang.Assert;
2019
import io.jsonwebtoken.security.HashAlgorithm;
2120
import io.jsonwebtoken.security.Request;
2221
import io.jsonwebtoken.security.VerifyDigestRequest;
2322

24-
import java.io.IOException;
2523
import java.io.InputStream;
2624
import java.security.MessageDigest;
2725
import java.util.Locale;
@@ -38,17 +36,14 @@ public final class DefaultHashAlgorithm extends CryptoAlgorithm implements HashA
3836
public byte[] digest(final Request<InputStream> request) {
3937
Assert.notNull(request, "Request cannot be null.");
4038
final InputStream payload = Assert.notNull(request.getPayload(), "Request payload cannot be null.");
41-
return jca(request).withMessageDigest(new CheckedFunction<MessageDigest, byte[]>() {
42-
@Override
43-
public byte[] apply(MessageDigest md) throws IOException {
44-
byte[] buf = new byte[1024];
45-
int len = 0;
46-
while (len != -1) {
47-
len = payload.read(buf);
48-
if (len > 0) md.update(buf, 0, len);
49-
}
50-
return md.digest();
39+
return jca(request).withMessageDigest(md -> {
40+
byte[] buf = new byte[1024];
41+
int len = 0;
42+
while (len != -1) {
43+
len = payload.read(buf);
44+
if (len > 0) md.update(buf, 0, len);
5145
}
46+
return md.digest();
5247
});
5348
}
5449

0 commit comments

Comments
 (0)