1919import java .util .Map ;
2020import java .util .UUID ;
2121
22+ import org .apache .commons .lang .StringUtils ;
23+ import org .apache .logging .log4j .LogManager ;
24+ import org .apache .logging .log4j .Logger ;
25+
2226import javax .crypto .Mac ;
2327import javax .crypto .spec .SecretKeySpec ;
2428
2529import org .apache .commons .codec .binary .Base64 ;
30+ import org .apache .commons .lang .Validate ;
2631import org .bouncycastle .jce .provider .BouncyCastleProvider ;
2732
2833import com .fasterxml .jackson .databind .ObjectMapper ;
@@ -47,6 +52,7 @@ public class JWTSigner {
4752
4853 private byte [] secret ;
4954 private PrivateKey privateKey ;
55+ private static Logger logger = LogManager .getLogger (JWTSigner .class );
5056
5157 // Default algorithm HMAC SHA-256 ("HS256")
5258 protected final static Algorithm DEFAULT_ALGORITHM = Algorithm .HS256 ;
@@ -55,7 +61,8 @@ public JWTSigner(final String secret) {
5561 this (secret .getBytes ());
5662 }
5763
58- public JWTSigner (final byte [] secret ) {
64+ public JWTSigner (final byte [] secret ) {
65+ Validate .notNull (secret );
5966 this .secret = secret ;
6067 }
6168
@@ -78,7 +85,8 @@ public JWTSigner(final PrivateKey privateKey) {
7885 * the "options" parameter override claims in this map.
7986 * @param options Allow choosing the signing algorithm, and automatic setting of some registered claims.
8087 */
81- public String sign (final Map <String , Object > claims , final Options options ) {
88+ public String sign (final Map <String , Object > claims , final Options options ) {
89+ Validate .notNull (claims , "JWT claims cannot be null" );
8290 final Algorithm algorithm = (options != null && options .algorithm != null ) ? options .algorithm : DEFAULT_ALGORITHM ;
8391 final List <String > segments = new ArrayList <String >();
8492 try {
@@ -87,6 +95,7 @@ public String sign(final Map<String, Object> claims, final Options options) {
8795 segments .add (encodedSignature (join (segments , "." ), algorithm ));
8896 return join (segments , "." );
8997 } catch (Exception e ) {
98+ logger .error ("JWT Sign error" , e );
9099 throw new RuntimeException (e .getCause ());
91100 }
92101 }
@@ -95,14 +104,17 @@ public String sign(final Map<String, Object> claims, final Options options) {
95104 * Generate a JSON Web Token using the default algorithm HMAC SHA-256 ("HS256")
96105 * and no claims automatically set.
97106 */
98- public String sign (final Map <String , Object > claims ) {
107+ public String sign (final Map <String , Object > claims ) {
108+ Validate .notNull (claims );
99109 return sign (claims , null );
100110 }
101111
102112 /**
103113 * Generate the header part of a JSON web token.
104114 */
105- private String encodedHeader (final Algorithm algorithm ) throws UnsupportedEncodingException {
115+ private String encodedHeader (final Algorithm algorithm ) throws UnsupportedEncodingException {
116+ Validate .notNull (algorithm );
117+ // create the header
106118 final ObjectNode header = JsonNodeFactory .instance .objectNode ();
107119 header .put ("typ" , "JWT" );
108120 header .put ("alg" , algorithm .name ());
@@ -131,6 +143,8 @@ private String encodedPayload(final Map<String, Object> _claims, final Options o
131143 }
132144
133145 private void processPayloadOptions (final Map <String , Object > claims , final Options options ) {
146+ Validate .notNull (claims );
147+ Validate .notNull (options );
134148 final long now = System .currentTimeMillis () / 1000l ;
135149 if (options .expirySeconds != null )
136150 claims .put ("exp" , now + options .expirySeconds );
@@ -144,6 +158,8 @@ private void processPayloadOptions(final Map<String, Object> claims, final Optio
144158
145159 // consider cleanup
146160 private void enforceIntDate (final Map <String , Object > claims , final String claimName ) {
161+ Validate .notNull (claims );
162+ Validate .notNull (claimName );
147163 final Object value = handleNullValue (claims , claimName );
148164 if (value == null )
149165 return ;
@@ -226,6 +242,8 @@ private String checkStringOrURI(final Object value) {
226242 */
227243 private String encodedSignature (final String signingInput , final Algorithm algorithm ) throws NoSuchAlgorithmException , InvalidKeyException ,
228244 NoSuchProviderException , SignatureException , JWTAlgorithmException {
245+ Validate .notNull (signingInput );
246+ Validate .notNull (algorithm );
229247 switch (algorithm ) {
230248 case HS256 :
231249 case HS384 :
@@ -244,13 +262,17 @@ private String encodedSignature(final String signingInput, final Algorithm algor
244262 * Safe URL encode a byte array to a String
245263 */
246264 private String base64UrlEncode (final byte [] str ) {
265+ Validate .notNull (str );
247266 return new String (Base64 .encodeBase64URLSafe (str ));
248267 }
249268
250269 /**
251270 * Sign an input string using HMAC and return the encrypted bytes
252271 */
253272 private static byte [] signHmac (final Algorithm algorithm , final String msg , final byte [] secret ) throws NoSuchAlgorithmException , InvalidKeyException {
273+ Validate .notNull (algorithm );
274+ Validate .notNull (msg );
275+ Validate .notNull (secret );
254276 final Mac mac = Mac .getInstance (algorithm .getValue ());
255277 mac .init (new SecretKeySpec (secret , algorithm .getValue ()));
256278 return mac .doFinal (msg .getBytes ());
@@ -261,15 +283,20 @@ private static byte[] signHmac(final Algorithm algorithm, final String msg, fina
261283 */
262284 private static byte [] signRs (final Algorithm algorithm , final String msg , final PrivateKey privateKey ) throws NoSuchProviderException ,
263285 NoSuchAlgorithmException , InvalidKeyException , SignatureException {
286+ Validate .notNull (algorithm );
287+ Validate .notNull (msg );
288+ Validate .notNull (privateKey );
264289 final byte [] messageBytes = msg .getBytes ();
265290 final Signature signature = Signature .getInstance (algorithm .getValue (), "BC" );
266291 signature .initSign (privateKey );
267292 signature .update (messageBytes );
268293 return signature .sign ();
269294 }
270295
271- private String join (final List <String > input , final String separator ) {
272- return org .apache .commons .lang .StringUtils .join (input .iterator (), separator );
296+ private String join (final List <String > input , final String separator ) {
297+ Validate .notNull (input );
298+ Validate .notNull (separator );
299+ return StringUtils .join (input .iterator (), separator );
273300 }
274301
275302 /**
0 commit comments