| .. | .. |
|---|
| 78 | 78 | * @param token |
|---|
| 79 | 79 | * @return |
|---|
| 80 | 80 | */ |
|---|
| 81 | | - public boolean validateToken(String token) { |
|---|
| 81 | + public boolean isTokenValid(String token) { |
|---|
| 82 | 82 | try { |
|---|
| 83 | 83 | String tokenDecoded = new String(Base64.decode(token)); |
|---|
| 84 | 84 | String[] parts = StringUtils.split(tokenDecoded, ' '); |
|---|
| 85 | + if (parts == null || parts.length < 3) |
|---|
| 86 | + return false; |
|---|
| 85 | 87 | String secret = parts[0]; |
|---|
| 86 | 88 | String user = parts[1]; |
|---|
| 87 | 89 | Date date = Utils.toDateFromIso(parts[2]); |
|---|
| .. | .. |
|---|
| 101 | 103 | try { |
|---|
| 102 | 104 | String tokenDecoded = new String(Base64.decode(token)); |
|---|
| 103 | 105 | String[] parts = StringUtils.split(tokenDecoded, ' '); |
|---|
| 106 | + if (parts == null || parts.length < 3) |
|---|
| 107 | + return null; |
|---|
| 104 | 108 | String user = parts[1]; |
|---|
| 105 | 109 | return user; |
|---|
| 110 | + } catch (IOException e) { |
|---|
| 111 | + log.error("Error decoding Base64 token", e); |
|---|
| 112 | + } |
|---|
| 113 | + return null; |
|---|
| 114 | + } |
|---|
| 115 | + |
|---|
| 116 | + public Date extractDateCreationFromToken(String token) { |
|---|
| 117 | + try { |
|---|
| 118 | + String tokenDecoded = new String(Base64.decode(token)); |
|---|
| 119 | + String[] parts = StringUtils.split(tokenDecoded, ' '); |
|---|
| 120 | + if (parts == null || parts.length < 3) |
|---|
| 121 | + return null; |
|---|
| 122 | + Date date = Utils.toDateFromIso(parts[2]); |
|---|
| 123 | + return date; |
|---|
| 106 | 124 | } catch (IOException e) { |
|---|
| 107 | 125 | log.error("Error decoding Base64 token", e); |
|---|
| 108 | 126 | } |
|---|
| .. | .. |
|---|
| 114 | 132 | String token = th.generateToken("pepe"); |
|---|
| 115 | 133 | System.out.println("Token: " + token); |
|---|
| 116 | 134 | System.out.println("Token: " + new String(Base64.decode(token))); |
|---|
| 117 | | - System.out.println("Valid Token: " + th.validateToken(token)); |
|---|
| 135 | + System.out.println("Valid Token: " + th.isTokenValid(token)); |
|---|
| 118 | 136 | } |
|---|
| 119 | 137 | } |
|---|