1- /**
1+ /*
22 * Copyright 2020 Google LLC
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
1212 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
15- *
1615 */
1716package net .oauth .jsontoken ;
1817
2221import com .google .gson .JsonParseException ;
2322import com .google .gson .JsonParser ;
2423import java .security .SignatureException ;
24+ import java .time .Instant ;
2525import java .util .List ;
2626import net .oauth .jsontoken .crypto .AsciiStringVerifier ;
2727import net .oauth .jsontoken .crypto .Verifier ;
2828import net .oauth .jsontoken .exceptions .ErrorCode ;
2929import net .oauth .jsontoken .exceptions .InvalidJsonTokenException ;
3030import org .apache .commons .codec .binary .Base64 ;
31- import org .joda .time .Instant ;
3231
3332/**
34- * Class that provides common functions
35- * used by {@link JsonTokenParser} and {@link AsyncJsonTokenParser}.
33+ * Class that provides common functions used by {@link JsonTokenParser} and {@link
34+ * AsyncJsonTokenParser}.
3635 */
3736abstract class AbstractJsonTokenParser {
3837 private final Clock clock ;
@@ -41,8 +40,7 @@ abstract class AbstractJsonTokenParser {
4140 /**
4241 * Creates a new {@link AbstractJsonTokenParser}.
4342 *
44- * @param clock a clock object that will decide whether a given token is
45- * currently valid or not.
43+ * @param clock a clock object that will decide whether a given token is currently valid or not.
4644 * @param checkers an array of checkers that validates the parameters in the JSON token.
4745 */
4846 AbstractJsonTokenParser (Clock clock , Checker ... checkers ) {
@@ -51,8 +49,8 @@ abstract class AbstractJsonTokenParser {
5149 }
5250
5351 /**
54- * Decodes the JWT token string into a JsonToken object. Does not perform
55- * any validation of headers or claims.
52+ * Decodes the JWT token string into a JsonToken object. Does not perform any validation of
53+ * headers or claims.
5654 *
5755 * @param tokenString The original encoded representation of a JWT
5856 * @return Unverified contents of the JWT as a JsonToken
@@ -64,25 +62,22 @@ final JsonToken deserializeInternal(String tokenString) {
6462 String jwtHeaderSegment = pieces .get (0 );
6563 String jwtPayloadSegment = pieces .get (1 );
6664 JsonParser parser = new JsonParser ();
67- JsonObject header = parser . parse ( JsonTokenUtil . fromBase64ToJsonString ( jwtHeaderSegment ))
68- .getAsJsonObject ();
69- JsonObject payload = parser . parse ( JsonTokenUtil . fromBase64ToJsonString ( jwtPayloadSegment ))
70- .getAsJsonObject ();
65+ JsonObject header =
66+ parser . parse ( JsonTokenUtil . fromBase64ToJsonString ( jwtHeaderSegment )) .getAsJsonObject ();
67+ JsonObject payload =
68+ parser . parse ( JsonTokenUtil . fromBase64ToJsonString ( jwtPayloadSegment )) .getAsJsonObject ();
7169
72- JsonToken jsonToken = new JsonToken (header , payload , clock , tokenString );
73- return jsonToken ;
70+ return new JsonToken (header , payload , clock , tokenString );
7471 }
7572
7673 /**
77- * Verifies that the jsonToken has a valid signature and valid standard claims
78- * (iat, exp). Does not need VerifierProviders because verifiers are passed in
79- * directly.
74+ * Verifies that the jsonToken has a valid signature and valid standard claims (iat, exp). Does
75+ * not need VerifierProviders because verifiers are passed in directly.
8076 *
8177 * @param jsonToken the token to verify
82- * @throws SignatureException when the signature is invalid
83- * or if any of the checkers fail
84- * @throws IllegalStateException when exp or iat are invalid
85- * or if tokenString is not a properly formatted JWT
78+ * @throws SignatureException when the signature is invalid or if any of the checkers fail
79+ * @throws IllegalStateException when exp or iat are invalid or if tokenString is not a properly
80+ * formatted JWT
8681 */
8782 final void verifyInternal (JsonToken jsonToken , List <Verifier > verifiers )
8883 throws SignatureException {
@@ -96,16 +91,19 @@ final void verifyInternal(JsonToken jsonToken, List<Verifier> verifiers)
9691 Instant expiration = jsonToken .getExpiration ();
9792
9893 if (issuedAt == null && expiration != null ) {
99- issuedAt = new Instant ( 0 ) ;
94+ issuedAt = Instant . EPOCH ;
10095 }
10196
10297 if (issuedAt != null && expiration == null ) {
103- expiration = new Instant (Long .MAX_VALUE );
98+ // TODO(kak): Should this be Instant.MAX instead?
99+ expiration = Instant .ofEpochMilli (Long .MAX_VALUE );
104100 }
105101
106102 if (issuedAt != null && expiration != null ) {
107- String errorMessage = String .format ("Invalid iat and/or exp. iat: %s exp: %s now: %s" ,
108- jsonToken .getIssuedAt (), jsonToken .getExpiration (), clock .now ());
103+ String errorMessage =
104+ String .format (
105+ "Invalid iat and/or exp. iat: %s exp: %s now: %s" ,
106+ jsonToken .getIssuedAt (), jsonToken .getExpiration (), clock .now ());
109107
110108 if (issuedAt .isAfter (expiration )) {
111109 throw new IllegalStateException (
@@ -134,8 +132,7 @@ final void verifyInternal(JsonToken jsonToken, List<Verifier> verifiers)
134132 * Verifies that a JSON Web Token's signature is valid.
135133 *
136134 * @param tokenString the encoded and signed JSON Web Token to verify.
137- * @param verifiers used to verify the signature. These usually encapsulate
138- * secret keys.
135+ * @param verifiers used to verify the signature. These usually encapsulate secret keys.
139136 * @throws IllegalStateException if tokenString is not a properly formatted JWT
140137 */
141138 final boolean signatureIsValidInternal (String tokenString , List <Verifier > verifiers ) {
@@ -190,11 +187,13 @@ private List<String> splitTokenString(String tokenString) {
190187 List <String > pieces = Splitter .on (JsonTokenUtil .DELIMITER ).splitToList (tokenString );
191188 if (pieces .size () != 3 ) {
192189 throw new IllegalStateException (
193- "Expected JWT to have 3 segments separated by '" +
194- JsonTokenUtil .DELIMITER + "', but it has " + pieces .size () + " segments" ,
190+ "Expected JWT to have 3 segments separated by '"
191+ + JsonTokenUtil .DELIMITER
192+ + "', but it has "
193+ + pieces .size ()
194+ + " segments" ,
195195 new InvalidJsonTokenException (ErrorCode .MALFORMED_TOKEN_STRING ));
196196 }
197197 return pieces ;
198198 }
199-
200199}
0 commit comments