Roberto Sánchez
2014-01-17 441c660af706fd3c6d0e06b36b8f25a808fcdf5f
securis/src/main/java/net/curisit/securis/utils/TokenHelper.java
....@@ -78,10 +78,12 @@
7878 * @param token
7979 * @return
8080 */
81
- public boolean validateToken(String token) {
81
+ public boolean isTokenValid(String token) {
8282 try {
8383 String tokenDecoded = new String(Base64.decode(token));
8484 String[] parts = StringUtils.split(tokenDecoded, ' ');
85
+ if (parts == null || parts.length < 3)
86
+ return false;
8587 String secret = parts[0];
8688 String user = parts[1];
8789 Date date = Utils.toDateFromIso(parts[2]);
....@@ -101,8 +103,24 @@
101103 try {
102104 String tokenDecoded = new String(Base64.decode(token));
103105 String[] parts = StringUtils.split(tokenDecoded, ' ');
106
+ if (parts == null || parts.length < 3)
107
+ return null;
104108 String user = parts[1];
105109 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;
106124 } catch (IOException e) {
107125 log.error("Error decoding Base64 token", e);
108126 }
....@@ -114,6 +132,6 @@
114132 String token = th.generateToken("pepe");
115133 System.out.println("Token: " + token);
116134 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));
118136 }
119137 }