1919
2020package com .auth0 .jwt ;
2121
22- import com .auth0 .jwt .creators .*;
23- import com .auth0 .jwt .impl .JWTParser ;
22+ import com .auth0 .jwt .creators .EncodeType ;
23+ import com .auth0 .jwt .creators .FbJwtCreator ;
24+ import com .auth0 .jwt .creators .GoogleJwtCreator ;
25+ import com .auth0 .jwt .creators .GoogleOrFbJwtCreator ;
2426import com .auth0 .jwt .impl .Claims ;
27+ import com .auth0 .jwt .impl .JWTParser ;
2528import com .auth0 .jwt .interfaces .Claim ;
2629import com .auth0 .jwt .interfaces .DecodedJWT ;
2730import com .auth0 .jwt .interfaces .Header ;
2831import com .auth0 .jwt .interfaces .Payload ;
2932import com .auth0 .jwt .utils .TokenUtils ;
33+ import com .google .common .base .Strings ;
34+ import java .net .URLDecoder ;
35+ import java .nio .charset .StandardCharsets ;
36+ import java .util .Date ;
37+ import java .util .List ;
38+ import java .util .Map ;
3039import org .apache .commons .codec .binary .Base32 ;
3140import org .apache .commons .codec .binary .Base64 ;
3241import org .apache .commons .codec .binary .Hex ;
3342import org .apache .commons .codec .binary .StringUtils ;
3443
35- import java .net .URLDecoder ;
36- import java .util .*;
37-
3844/**
3945 * The JWTDecoder class holds the decode method to parse a given JWT token into it's JWT representation.
4046 */
@@ -44,9 +50,8 @@ public final class JWTDecoder implements DecodedJWT {
4450 private final String [] parts ;
4551 private final Header header ;
4652 private final Payload payload ;
47-
48- private static final String FACEBOOK = "facebook" ;
49- private static final String GOOGLE = "google" ;
53+ private static final String ISSUER_FACEBOOK = "facebook" ;
54+ private static final String ISSUER_GOOGLE = "google" ;
5055
5156 public JWTDecoder (String jwt , EncodeType encodeType ) throws Exception {
5257 parts = TokenUtils .splitToken (jwt );
@@ -55,13 +60,13 @@ public JWTDecoder(String jwt, EncodeType encodeType) throws Exception {
5560 String payloadJson = null ;
5661 switch (encodeType ) {
5762 case Base16 :
58- headerJson = URLDecoder .decode (new String (Hex .decodeHex (parts [0 ])), "UTF-8" );
59- payloadJson = URLDecoder .decode (new String (Hex .decodeHex (parts [1 ])), "UTF-8" );
63+ headerJson = URLDecoder .decode (new String (Hex .decodeHex (parts [0 ])), StandardCharsets . UTF_8 . name () );
64+ payloadJson = URLDecoder .decode (new String (Hex .decodeHex (parts [1 ])), StandardCharsets . UTF_8 . name () );
6065 break ;
6166 case Base32 :
6267 Base32 base32 = new Base32 ();
63- headerJson = URLDecoder .decode (new String (base32 .decode (parts [0 ]), "UTF-8" ));
64- payloadJson = URLDecoder .decode (new String (base32 .decode (parts [1 ]), "UTF-8" ));
68+ headerJson = URLDecoder .decode (new String (base32 .decode (parts [0 ]), StandardCharsets . UTF_8 . name () ));
69+ payloadJson = URLDecoder .decode (new String (base32 .decode (parts [1 ]), StandardCharsets . UTF_8 . name () ));
6570 break ;
6671 case Base64 :
6772 headerJson = StringUtils .newStringUtf8 (Base64 .decodeBase64 (parts [0 ]));
@@ -162,17 +167,24 @@ public String getToken() {
162167 return String .format ("%s.%s.%s" , parts [0 ], parts [1 ], parts [2 ]);
163168 }
164169
165- public static GoogleOrFbJwtCreator decodeJWT (DecodedJWT jwt ) {
170+ public GoogleOrFbJwtCreator decodeJWT (DecodedJWT jwt ) {
166171 Map <String , Claim > claims = jwt .getClaims ();
167- String issuer = claims .get (Claims .ISSUER ).asString ();
172+ Claim issuerClaim = claims .get (Claims .ISSUER );
173+ if (issuerClaim == null ) {
174+ throw new IllegalArgumentException ("null issuer claim" );
175+ }
176+ String issuer = issuerClaim .asString ();
168177 GoogleOrFbJwtCreator googleOrFbJwtCreator = null ;
169- if (issuer .contains (FACEBOOK )) {
178+ if (Strings .isNullOrEmpty (issuer )) {
179+ throw new IllegalArgumentException ("null or empty issuer" );
180+ }
181+ if (ISSUER_FACEBOOK .contains (issuer )) {
170182 googleOrFbJwtCreator = FbJwtCreator .build ()
171183 .withExp (claims .get (Claims .EXPIRES_AT ).asDate ())
172184 .withIat (claims .get (Claims .ISSUED_AT ).asDate ())
173185 .withAppId (claims .get (Claims .APP_ID ).asString ())
174186 .withUserId (claims .get (Claims .USER_ID ).asString ());
175- } else if (issuer .contains (GOOGLE )) {
187+ } else if (ISSUER_GOOGLE .contains (issuer )) {
176188 googleOrFbJwtCreator = GoogleJwtCreator .build ()
177189 .withPicture (claims .get (Claims .PICTURE ).asString ())
178190 .withEmail (claims .get (Claims .EMAIL ).asString ())
@@ -188,4 +200,5 @@ public static GoogleOrFbJwtCreator decodeJWT(DecodedJWT jwt) {
188200
189201 return googleOrFbJwtCreator ;
190202 }
203+
191204}
0 commit comments