25
25
import java .util .List ;
26
26
import java .util .Objects ;
27
27
import java .util .function .Consumer ;
28
+ import java .util .function .Supplier ;
28
29
29
30
/**
30
31
* @author Daniel DeGroff
@@ -41,6 +42,24 @@ public String encode(JWT jwt, Signer signer) {
41
42
return encode (jwt , signer , h -> h .set ("kid" , signer .getKid ()));
42
43
}
43
44
45
+ /**
46
+ * Encode the JWT to produce a dot separated encoded string that can be sent in an HTTP request header.
47
+ *
48
+ * @param jwt The JWT.
49
+ * @param signer The signer used to add a signature to the JWT.
50
+ * @param supplier A header supplier to optionally add header values to the encoded JWT. May be null.
51
+ * @return the encoded JWT string.
52
+ */
53
+ public String encode (JWT jwt , Signer signer , Supplier <Header > supplier ) {
54
+ final Header header ;
55
+ if (supplier != null ) {
56
+ header = supplier .get ();
57
+ } else {
58
+ header = new Header ();
59
+ }
60
+ return encode (jwt , signer , header );
61
+ }
62
+
44
63
/**
45
64
* Encode the JWT to produce a dot separated encoded string that can be sent in an HTTP request header.
46
65
*
@@ -50,14 +69,18 @@ public String encode(JWT jwt, Signer signer) {
50
69
* @return the encoded JWT string.
51
70
*/
52
71
public String encode (JWT jwt , Signer signer , Consumer <Header > consumer ) {
53
- Objects .requireNonNull (jwt );
54
- Objects .requireNonNull (signer );
55
-
56
- List <String > parts = new ArrayList <>(3 );
57
72
Header header = new Header ();
58
73
if (consumer != null ) {
59
74
consumer .accept (header );
60
75
}
76
+ return encode (jwt , signer , header );
77
+ }
78
+
79
+ private String encode (JWT jwt , Signer signer , Header header ) {
80
+ Objects .requireNonNull (jwt );
81
+ Objects .requireNonNull (signer );
82
+
83
+ List <String > parts = new ArrayList <>(3 );
61
84
// Set this after we pass the header to the consumer to ensure it isn't tampered with, only the signer can set the algorithm.
62
85
header .algorithm = signer .getAlgorithm ();
63
86
parts .add (base64Encode (Mapper .serialize (header )));
0 commit comments