|
1 |
| -package com.omatheusmesmo.shoppmate.auth.service.service; |
| 1 | +package com.omatheusmesmo.shoppmate.auth.service; |
2 | 2 |
|
3 | 3 | import com.nimbusds.jose.EncryptionMethod;
|
4 | 4 | import com.nimbusds.jose.JOSEException;
|
|
21 | 21 | import java.security.NoSuchAlgorithmException;
|
22 | 22 | import java.security.interfaces.RSAPrivateKey;
|
23 | 23 | import java.security.interfaces.RSAPublicKey;
|
| 24 | +import java.text.ParseException; |
24 | 25 | import java.util.Date;
|
25 | 26 | import java.util.UUID;
|
26 | 27 |
|
@@ -61,14 +62,59 @@ protected EncryptedJWT encryptToken(UserDetails userDetails) {
|
61 | 62 | }
|
62 | 63 |
|
63 | 64 | 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 { |
64 | 103 | try {
|
65 | 104 | EncryptedJWT encryptedJWT = EncryptedJWT.parse(token);
|
66 | 105 | 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; |
69 | 115 | } 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); |
72 | 118 | }
|
73 | 119 | }
|
74 | 120 |
|
|
0 commit comments