@@ -51,14 +51,16 @@ private static void initHeader() {
51
51
}
52
52
}
53
53
54
- public static String createAndSignLegacyAccessToken (JsonElement jsonObj , String privateSigningKey , AccessToken .VERSION version )
54
+ public static String createAndSignLegacyAccessToken (JsonElement jsonObj , String privateSigningKey ,
55
+ AccessToken .VERSION version )
55
56
throws InvalidKeyException , NoSuchAlgorithmException , InvalidKeySpecException , SignatureException {
56
57
initHeader ();
57
58
String payload ;
58
59
String header ;
59
60
header = version == AccessToken .VERSION .V1 ? JWT .HEADERv1 : JWT .HEADERv2 ;
60
61
payload = Utils .convertToBase64 (jsonObj .toString ());
61
- String signature = Utils .signWithPrivateKey (header + "." + payload , privateSigningKey , version != AccessToken .VERSION .V1 && version != AccessToken .VERSION .V2 );
62
+ String signature = Utils .signWithPrivateKey (header + "." + payload , privateSigningKey ,
63
+ version != AccessToken .VERSION .V1 && version != AccessToken .VERSION .V2 );
62
64
return header + "." + payload + "." + signature ;
63
65
}
64
66
@@ -79,11 +81,17 @@ public static JWTPreParseInfo preParseJWTInfo(String jwt) throws JWTException {
79
81
80
82
JsonObject parsedHeader = new JsonParser ().parse (Utils .convertFromBase64 (splittedInput [0 ])).getAsJsonObject ();
81
83
84
+ if (parsedHeader .get ("typ" ) == null ) {
85
+ throw new JWTException ("JWT header missing - typ" );
86
+ }
82
87
JsonPrimitive typ = parsedHeader .get ("typ" ).getAsJsonPrimitive ();
83
88
if (!typ .isString () || !typ .getAsString ().equals ("JWT" )) {
84
89
throw new JWTException ("JWT header mismatch - typ" );
85
90
}
86
91
92
+ if (parsedHeader .get ("alg" ) == null ) {
93
+ throw new JWTException ("JWT header missing - alg" );
94
+ }
87
95
JsonPrimitive alg = parsedHeader .get ("alg" ).getAsJsonPrimitive ();
88
96
if (!alg .isString () || !alg .getAsString ().equals ("RS256" )) {
89
97
throw new JWTException ("JWT header mismatch - alg" );
@@ -103,6 +111,9 @@ public static JWTPreParseInfo preParseJWTInfo(String jwt) throws JWTException {
103
111
}
104
112
105
113
JsonPrimitive kid = parsedHeader .get ("kid" ).getAsJsonPrimitive ();
114
+ if (parsedHeader .get ("kid" ) == null ) {
115
+ throw new JWTException ("JWT header missing - kid" );
116
+ }
106
117
if (!kid .isString ()) {
107
118
throw new JWTException ("JWT header mismatch - kid" );
108
119
}
@@ -113,7 +124,8 @@ public static JWTInfo verifyJWTAndGetPayload(JWTPreParseInfo jwt, String publicS
113
124
throws InvalidKeyException , NoSuchAlgorithmException , JWTException {
114
125
115
126
try {
116
- if (!Utils .verifyWithPublicKey (jwt .header + "." + jwt .payload , jwt .signature , publicSigningKey , jwt .version != AccessToken .VERSION .V1 && jwt .version != AccessToken .VERSION .V2 )) {
127
+ if (!Utils .verifyWithPublicKey (jwt .header + "." + jwt .payload , jwt .signature , publicSigningKey ,
128
+ jwt .version != AccessToken .VERSION .V1 && jwt .version != AccessToken .VERSION .V2 )) {
117
129
throw new JWTException ("JWT verification failed" );
118
130
}
119
131
} catch (InvalidKeySpecException | SignatureException e ) {
@@ -124,7 +136,8 @@ public static JWTInfo verifyJWTAndGetPayload(JWTPreParseInfo jwt, String publicS
124
136
125
137
public static JWTInfo getPayloadWithoutVerifying (String jwt ) throws JWTException {
126
138
JWTPreParseInfo jwtInfo = preParseJWTInfo (jwt );
127
- return new JWTInfo (new JsonParser ().parse (Utils .convertFromBase64 (jwtInfo .payload )).getAsJsonObject (), jwtInfo .version );
139
+ return new JWTInfo (new JsonParser ().parse (Utils .convertFromBase64 (jwtInfo .payload )).getAsJsonObject (),
140
+ jwtInfo .version );
128
141
}
129
142
130
143
public static class JWTException extends Exception {
@@ -150,7 +163,7 @@ public static class JWTPreParseInfo {
150
163
@ Nullable
151
164
public final String kid ;
152
165
153
- public JWTPreParseInfo (String [] splittedInput , AccessToken .VERSION version , String kid ) throws JWTException {
166
+ public JWTPreParseInfo (String [] splittedInput , AccessToken .VERSION version , String kid ) throws JWTException {
154
167
if (splittedInput .length != 3 ) {
155
168
throw new JWTException ("Invalid JWT" );
156
169
}
0 commit comments