2525import com .onixbyte .simplejwt .annotations .ExcludeFromPayload ;
2626import com .onixbyte .simplejwt .annotations .TokenEnum ;
2727import com .onixbyte .simplejwt .authzero .config .AuthzeroTokenResolverConfig ;
28+ import com .onixbyte .simplejwt .config .TokenResolverConfig ;
2829import com .onixbyte .simplejwt .constants .PredefinedKeys ;
2930import com .onixbyte .simplejwt .constants .TokenAlgorithm ;
3031import com .auth0 .jwt .JWT ;
4546import java .util .*;
4647
4748/**
48- * The {@code AuthzeroTokenResolver} class is an implementation of the {@link
49- * TokenResolver} interface. It uses the {@code
50- * com.auth0:java-jwt} library to handle JSON Web Token (JWT) resolution. This
51- * resolver provides functionality to create, extract, verify, and renew JWT
49+ * The {@code AuthzeroTokenResolver} class is an implementation of the {@link TokenResolver}
50+ * interface. It uses the {@code com.auth0:java-jwt} library to handle JSON Web Token (JWT)
51+ * resolution. This resolver provides functionality to create, extract, verify, and renew JWT
5252 * tokens using various algorithms and custom payload data.
5353 * <p>
5454 * <b>Usage:</b>
55- * To use the {@code AuthzeroTokenResolver}, first, create an instance of this
56- * class:
55+ * To use the {@code AuthzeroTokenResolver}, first, create an instance of this class:
5756 * <pre>{@code
5857 * TokenResolver<DecodedJWT> tokenResolver =
5958 * new AuthzeroTokenResolver(TokenAlgorithm.HS256,
6261 * "Token Secret");
6362 * }</pre>
6463 * <p>
65- * Then, you can utilize the various methods provided by this resolver to
66- * handle JWT tokens:
64+ * Then, you can utilize the various methods provided by this resolver to handle JWT tokens:
6765 * <pre>{@code
6866 * // Creating a new JWT token
6967 * String token =
8280 * }</pre>
8381 * <p>
8482 * <b>Note:</b>
85- * It is essential to configure the appropriate algorithms, secret, and issuer
86- * according to your specific use case when using this resolver.
87- * Additionally, ensure that the {@code com.auth0:java-jwt} library is
88- * correctly configured in your project's dependencies.
83+ * It is essential to configure the appropriate algorithms, secret, and issuer according to your
84+ * specific use case when using this resolver. Additionally, ensure that the
85+ * {@code com.auth0:java-jwt} library is correctly configured in your project's dependencies.
8986 *
9087 * @author Zihlu Wang
9188 * @version 1.1.1
10097public class AuthzeroTokenResolver implements TokenResolver <DecodedJWT > {
10198
10299 /**
103- * Creates a new instance of {@code AuthzeroTokenResolver} with the
104- * provided configurations.
100+ * Creates a new instance of {@code AuthzeroTokenResolver} with the provided configurations.
105101 *
106- * @param jtiCreator the {@link GuidCreator} used for generating unique
107- * identifiers for "jti" claim in JWT tokens
108- * @param algorithm the algorithm used for signing and verifying JWT
109- * tokens
102+ * @param jtiCreator the {@link GuidCreator} used for generating unique identifiers for "jti"
103+ * claim in JWT tokens
104+ * @param algorithm the algorithm used for signing and verifying JWT tokens
110105 * @param issuer the issuer claim value to be included in JWT tokens
111- * @param secret the secret used for HMAC-based algorithms (HS256,
112- * HS384, HS512) for token signing and verification
106+ * @param privateKey the secret used for HMAC-based algorithms (HS256, HS384, HS512) for
107+ * token signing and verification, or the private key for ECDSA-based
108+ * algorithms
109+ * @param publicKey the public key for ECDSA-based algorithms
113110 * @param objectMapper JSON handler
114111 */
115- public AuthzeroTokenResolver (GuidCreator <?> jtiCreator , TokenAlgorithm algorithm , String issuer , String secret , ObjectMapper objectMapper ) {
116- if (secret == null || secret .isBlank ()) {
117- throw new IllegalArgumentException ("A secret is required to build a JSON Web Token." );
118- }
112+ public AuthzeroTokenResolver (GuidCreator <?> jtiCreator ,
113+ TokenAlgorithm algorithm ,
114+ String issuer ,
115+ String privateKey ,
116+ String publicKey ,
117+ ObjectMapper objectMapper ) {
118+ if (TokenResolverConfig .HMAC_ALGORITHMS .contains (algorithm )) {
119+ if (privateKey == null || privateKey .isBlank ()) {
120+ throw new IllegalArgumentException ("A secret is required to build a JSON Web Token." );
121+ }
119122
120- if (secret .length () < 32 ) {
121- log .warn ("The provided secret which owns {} characters is too weak. Please consider replacing it with a stronger one." , secret .length ());
123+ if (privateKey .length () < 32 ) {
124+ log .warn ("The provided secret which owns {} characters is too weak. Please consider" +
125+ " replacing it with a stronger one." , privateKey .length ());
126+ }
122127 }
123128
124129 this .jtiCreator = jtiCreator ;
125130 this .algorithm = config
126131 .getAlgorithm (algorithm )
127- .apply (secret );
132+ .apply (privateKey , publicKey );
128133 this .issuer = issuer ;
129134 this .verifier = JWT .require (this .algorithm ).build ();
130135 this .objectMapper = objectMapper ;
131136 }
132137
133138 /**
134- * Creates a new instance of {@link AuthzeroTokenResolver} with the
135- * provided configurations and a simple UUID GuidCreator.
139+ * Creates a new instance of {@link AuthzeroTokenResolver} with the provided configurations
140+ * and a simple UUID GuidCreator.
136141 *
137142 * @param algorithm the algorithm used for signing and verifying JWT tokens
138143 * @param issuer the issuer claim value to be included in JWT tokens
139- * @param secret the secret used for HMAC-based algorithms (HS256,
140- * HS384, HS512) for token signing and verification
144+ * @param privateKey the secret used for HMAC-based algorithms (HS256, HS384, HS512) for
145+ * token signing and verification, or the private key for ECDSA-based
146+ * algorithms
147+ * @param publicKey the public key for ECDSA-based algorithms
141148 * @param objectMapper Jackson Databind JSON Handler
142149 */
143- public AuthzeroTokenResolver (TokenAlgorithm algorithm , String issuer , String secret , ObjectMapper objectMapper ) {
144- this (UUID ::randomUUID , algorithm , issuer , secret , objectMapper );
150+ public AuthzeroTokenResolver (TokenAlgorithm algorithm ,
151+ String issuer ,
152+ String privateKey ,
153+ String publicKey ,
154+ ObjectMapper objectMapper ) {
155+ this (UUID ::randomUUID , algorithm , issuer , privateKey , publicKey , objectMapper );
145156 }
146157
147158 /**
148- * Creates a new instance of {@link AuthzeroTokenResolver} with the
149- * provided configurations and a simple UUID GuidCreator.
159+ * Creates a new instance of {@link AuthzeroTokenResolver} with the provided configurations
160+ * and a simple UUID GuidCreator.
150161 *
151- * @param algorithm the algorithm used for signing and verifying JWT tokens
152- * @param issuer the issuer claim value to be included in JWT tokens
153- * @param secret the secret used for HMAC-based algorithms (HS256,
154- * HS384, HS512) for token signing and verification
162+ * @param algorithm the algorithm used for signing and verifying JWT tokens
163+ * @param issuer the issuer claim value to be included in JWT tokens
164+ * @param privateKey the secret used for HMAC-based algorithms (HS256, HS384, HS512) for
165+ * token signing and verification, or the private key for ECDSA-based
166+ * algorithms
167+ * @param publicKey the public key for ECDSA-based algorithms
155168 */
156- public AuthzeroTokenResolver (TokenAlgorithm algorithm , String issuer , String secret ) {
157- this (UUID ::randomUUID , algorithm , issuer , secret , new ObjectMapper ());
169+ public AuthzeroTokenResolver (TokenAlgorithm algorithm , String issuer , String privateKey , String publicKey ) {
170+ this (UUID ::randomUUID , algorithm , issuer , privateKey , publicKey , new ObjectMapper ());
158171 }
159172
160173 /**
@@ -163,11 +176,10 @@ public AuthzeroTokenResolver(TokenAlgorithm algorithm, String issuer, String sec
163176 * UUID GuidCreator.
164177 *
165178 * @param issuer the issuer claim value to be included in JWT tokens
166- * @param secret the secret used for HMAC-based algorithms (HS256,
167- * HS384, HS512) for token signing and verification
179+ * @param secret the secret used for HS256 algorithms for token signing and verification
168180 */
169181 public AuthzeroTokenResolver (String issuer , String secret ) {
170- this (UUID ::randomUUID , TokenAlgorithm .HS256 , issuer , secret , new ObjectMapper ());
182+ this (UUID ::randomUUID , TokenAlgorithm .HS256 , issuer , secret , "" , new ObjectMapper ());
171183 }
172184
173185 /**
@@ -183,7 +195,7 @@ public AuthzeroTokenResolver(String issuer) {
183195 this .jtiCreator = UUID ::randomUUID ;
184196 this .algorithm = config
185197 .getAlgorithm (TokenAlgorithm .HS256 )
186- .apply (secret );
198+ .apply (secret , "" );
187199 this .issuer = issuer ;
188200 this .verifier = JWT .require (this .algorithm ).build ();
189201 this .objectMapper = new ObjectMapper ();
0 commit comments