Joaquín Reñé
2025-05-27 89b1c533d1b48b8b339b9c74a59c2ce73e6431af
securis/src/main/java/net/curisit/securis/utils/TokenHelper.java
....@@ -7,8 +7,8 @@
77 import java.security.NoSuchAlgorithmException;
88 import java.util.Date;
99
10
-import javax.enterprise.context.ApplicationScoped;
11
-import javax.inject.Inject;
10
+import jakarta.enterprise.context.ApplicationScoped;
11
+import jakarta.inject.Inject;
1212
1313 import net.curisit.integrity.commons.Utils;
1414 import net.curisit.securis.services.ApiResource;
....@@ -16,7 +16,9 @@
1616 import org.apache.commons.lang3.StringUtils;
1717 import org.apache.logging.log4j.LogManager;
1818 import org.apache.logging.log4j.Logger;
19
-import org.jboss.resteasy.util.Base64;
19
+
20
+import java.util.Base64;
21
+import java.nio.charset.StandardCharsets;
2022
2123 @ApplicationScoped
2224 public class TokenHelper {
....@@ -47,16 +49,21 @@
4749 return generateToken(user, new Date());
4850 }
4951
52
+ ;
53
+
5054 public String generateToken(String user, Date date) {
5155 try {
5256 String secret = generateSecret(user, date);
53
- StringBuffer sb = new StringBuffer();
57
+ StringBuilder sb = new StringBuilder();
5458 sb.append(secret);
5559 sb.append(' ');
5660 sb.append(user);
5761 sb.append(' ');
5862 sb.append(Utils.toIsoFormat(date));
59
- return Base64.encodeBytes(sb.toString().getBytes("utf-8"));
63
+
64
+ // Codificación estándar con UTF-8
65
+ return Base64.getEncoder().encodeToString(sb.toString().getBytes(StandardCharsets.UTF_8));
66
+
6067 } catch (NoSuchAlgorithmException e) {
6168 LOG.error("Error generating SHA-256 hash", e);
6269 } catch (UnsupportedEncodingException e) {
....@@ -86,7 +93,7 @@
8693 */
8794 public boolean isTokenValid(String token) {
8895 try {
89
- String tokenDecoded = new String(Base64.decode(token));
96
+ String tokenDecoded = new String(Base64.getDecoder().decode(token), StandardCharsets.UTF_8);
9097 String[] parts = StringUtils.split(tokenDecoded, ' ');
9198 if (parts == null || parts.length < 3) {
9299 return false;
....@@ -114,14 +121,14 @@
114121 if (token == null) {
115122 return null;
116123 }
117
- String tokenDecoded = new String(Base64.decode(token));
124
+ String tokenDecoded = new String(Base64.getDecoder().decode(token), StandardCharsets.UTF_8);
118125 String[] parts = StringUtils.split(tokenDecoded, ' ');
119126 if (parts == null || parts.length < 3) {
120127 return null;
121128 }
122129 String user = parts[1];
123130 return user;
124
- } catch (IOException e) {
131
+ } catch (Exception e) {
125132 LOG.error("Error decoding Base64 token", e);
126133 }
127134 return null;
....@@ -129,14 +136,14 @@
129136
130137 public Date extractDateCreationFromToken(String token) {
131138 try {
132
- String tokenDecoded = new String(Base64.decode(token));
139
+ String tokenDecoded = new String(Base64.getDecoder().decode(token), StandardCharsets.UTF_8);
133140 String[] parts = StringUtils.split(tokenDecoded, ' ');
134141 if (parts == null || parts.length < 3) {
135142 return null;
136143 }
137144 Date date = Utils.toDateFromIso(parts[2]);
138145 return date;
139
- } catch (IOException e) {
146
+ } catch (Exception e) {
140147 LOG.error("Error decoding Base64 token", e);
141148 }
142149 return null;
....@@ -148,7 +155,7 @@
148155 // OTk3ODRiMzY5NzQ5MWI5NmYyZGQyODRiYjY2ZTU2YzdmMTZjYzM3YTY3N2ExM2M3ODI2MjU5ZTMzOTIyYjUzNSBfY2xpZW50IDE5NzAtMDEtMDFUMDA6NTk6NTkuOTk5KzAxMDA=
149156 String t = new TokenHelper().generateToken("_client", new Date(-1));
150157 System.out.println("client token: " + t);
151
- System.out.println("client token: " + new String(Base64.decode(t)));
158
+ System.out.println("client token: " + new String(Base64.getDecoder().decode(t), StandardCharsets.UTF_8));
152159
153160 System.out.println("is valid client token: " + new TokenHelper().isTokenValid(t));
154161 }