Skip to content

Commit cc1970c

Browse files
committed
refactor: improve JWT token validation with enhanced error handling and logging
1 parent f69cc4b commit cc1970c

File tree

1 file changed

+51
-5
lines changed

1 file changed

+51
-5
lines changed

src/main/java/com/omatheusmesmo/shoppmate/auth/service/service/JwtService.java renamed to src/main/java/com/omatheusmesmo/shoppmate/auth/service/JwtService.java

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.omatheusmesmo.shoppmate.auth.service.service;
1+
package com.omatheusmesmo.shoppmate.auth.service;
22

33
import com.nimbusds.jose.EncryptionMethod;
44
import com.nimbusds.jose.JOSEException;
@@ -21,6 +21,7 @@
2121
import java.security.NoSuchAlgorithmException;
2222
import java.security.interfaces.RSAPrivateKey;
2323
import java.security.interfaces.RSAPublicKey;
24+
import java.text.ParseException;
2425
import java.util.Date;
2526
import java.util.UUID;
2627

@@ -61,14 +62,59 @@ protected EncryptedJWT encryptToken(UserDetails userDetails) {
6162
}
6263

6364
public boolean validateToken(String token) {
65+
try {
66+
JWTClaimsSet claims = decryptTokenInternal(token);
67+
68+
Date expirationTime = claims.getExpirationTime();
69+
if (expirationTime == null || expirationTime.before(new Date())) {
70+
logger.warn("Token has expired or expiration time is missing. Expiration: {}", expirationTime);
71+
return false;
72+
}
73+
74+
Date notBeforeTime = claims.getNotBeforeTime();
75+
if (notBeforeTime != null && notBeforeTime.after(new Date())) {
76+
logger.warn("Token not yet valid (not before time). Not Before: {}", notBeforeTime);
77+
return false;
78+
}
79+
80+
logger.debug("Token validation successful for subject: {}", claims.getSubject());
81+
return true;
82+
83+
} catch (ParseException e) {
84+
85+
logger.error("Failed to parse JWT token string during validation: {}", e.getMessage());
86+
return false;
87+
} catch (JOSEException e) {
88+
89+
logger.error("Failed to decrypt JWT token during validation: {}", e.getMessage());
90+
return false;
91+
} catch (JwtServiceException e) {
92+
93+
logger.error("JWT Service Exception during validation: {}", e.getMessage());
94+
return false;
95+
} catch (Exception e) {
96+
97+
logger.error("Unexpected error during JWT validation: {}", e.getMessage(), e);
98+
return false;
99+
}
100+
}
101+
102+
private JWTClaimsSet decryptTokenInternal(String token) throws ParseException, JOSEException, JwtServiceException {
64103
try {
65104
EncryptedJWT encryptedJWT = EncryptedJWT.parse(token);
66105
encryptedJWT.decrypt(decrypter);
67-
SignedJWT signedJWT = encryptedJWT.getPayload().toSignedJWT();
68-
return signedJWT != null && signedJWT.verify(new RSASSAVerifier(publicKey));
106+
return encryptedJWT.getJWTClaimsSet();
107+
} catch (ParseException e) {
108+
109+
logger.error("Failed to parse token string: {}", e.getMessage());
110+
throw e;
111+
} catch (JOSEException e) {
112+
113+
logger.error("Failed to decrypt token. Check if the correct private key is used and token format is valid. Error: {}", e.getMessage());
114+
throw e;
69115
} catch (Exception e) {
70-
logger.error("Invalid token", e);
71-
throw new JwtServiceException("Invalid token", e);
116+
logger.error("Unexpected error during token decryption: {}", e.getMessage(), e);
117+
throw new JwtServiceException("Unexpected error during token decryption", e);
72118
}
73119
}
74120

0 commit comments

Comments
 (0)