| .. | .. |
|---|
| 7 | 7 | import java.security.NoSuchAlgorithmException; |
|---|
| 8 | 8 | import java.util.Date; |
|---|
| 9 | 9 | |
|---|
| 10 | | -import javax.enterprise.context.ApplicationScoped; |
|---|
| 11 | | -import javax.inject.Inject; |
|---|
| 10 | +import jakarta.enterprise.context.ApplicationScoped; |
|---|
| 11 | +import jakarta.inject.Inject; |
|---|
| 12 | 12 | |
|---|
| 13 | 13 | import net.curisit.integrity.commons.Utils; |
|---|
| 14 | 14 | import net.curisit.securis.services.ApiResource; |
|---|
| .. | .. |
|---|
| 16 | 16 | import org.apache.commons.lang3.StringUtils; |
|---|
| 17 | 17 | import org.apache.logging.log4j.LogManager; |
|---|
| 18 | 18 | 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; |
|---|
| 20 | 22 | |
|---|
| 21 | 23 | @ApplicationScoped |
|---|
| 22 | 24 | public class TokenHelper { |
|---|
| .. | .. |
|---|
| 47 | 49 | return generateToken(user, new Date()); |
|---|
| 48 | 50 | } |
|---|
| 49 | 51 | |
|---|
| 52 | + ; |
|---|
| 53 | + |
|---|
| 50 | 54 | public String generateToken(String user, Date date) { |
|---|
| 51 | 55 | try { |
|---|
| 52 | 56 | String secret = generateSecret(user, date); |
|---|
| 53 | | - StringBuffer sb = new StringBuffer(); |
|---|
| 57 | + StringBuilder sb = new StringBuilder(); |
|---|
| 54 | 58 | sb.append(secret); |
|---|
| 55 | 59 | sb.append(' '); |
|---|
| 56 | 60 | sb.append(user); |
|---|
| 57 | 61 | sb.append(' '); |
|---|
| 58 | 62 | 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 | + |
|---|
| 60 | 67 | } catch (NoSuchAlgorithmException e) { |
|---|
| 61 | 68 | LOG.error("Error generating SHA-256 hash", e); |
|---|
| 62 | 69 | } catch (UnsupportedEncodingException e) { |
|---|
| .. | .. |
|---|
| 86 | 93 | */ |
|---|
| 87 | 94 | public boolean isTokenValid(String token) { |
|---|
| 88 | 95 | try { |
|---|
| 89 | | - String tokenDecoded = new String(Base64.decode(token)); |
|---|
| 96 | + String tokenDecoded = new String(Base64.getDecoder().decode(token), StandardCharsets.UTF_8); |
|---|
| 90 | 97 | String[] parts = StringUtils.split(tokenDecoded, ' '); |
|---|
| 91 | 98 | if (parts == null || parts.length < 3) { |
|---|
| 92 | 99 | return false; |
|---|
| .. | .. |
|---|
| 114 | 121 | if (token == null) { |
|---|
| 115 | 122 | return null; |
|---|
| 116 | 123 | } |
|---|
| 117 | | - String tokenDecoded = new String(Base64.decode(token)); |
|---|
| 124 | + String tokenDecoded = new String(Base64.getDecoder().decode(token), StandardCharsets.UTF_8); |
|---|
| 118 | 125 | String[] parts = StringUtils.split(tokenDecoded, ' '); |
|---|
| 119 | 126 | if (parts == null || parts.length < 3) { |
|---|
| 120 | 127 | return null; |
|---|
| 121 | 128 | } |
|---|
| 122 | 129 | String user = parts[1]; |
|---|
| 123 | 130 | return user; |
|---|
| 124 | | - } catch (IOException e) { |
|---|
| 131 | + } catch (Exception e) { |
|---|
| 125 | 132 | LOG.error("Error decoding Base64 token", e); |
|---|
| 126 | 133 | } |
|---|
| 127 | 134 | return null; |
|---|
| .. | .. |
|---|
| 129 | 136 | |
|---|
| 130 | 137 | public Date extractDateCreationFromToken(String token) { |
|---|
| 131 | 138 | try { |
|---|
| 132 | | - String tokenDecoded = new String(Base64.decode(token)); |
|---|
| 139 | + String tokenDecoded = new String(Base64.getDecoder().decode(token), StandardCharsets.UTF_8); |
|---|
| 133 | 140 | String[] parts = StringUtils.split(tokenDecoded, ' '); |
|---|
| 134 | 141 | if (parts == null || parts.length < 3) { |
|---|
| 135 | 142 | return null; |
|---|
| 136 | 143 | } |
|---|
| 137 | 144 | Date date = Utils.toDateFromIso(parts[2]); |
|---|
| 138 | 145 | return date; |
|---|
| 139 | | - } catch (IOException e) { |
|---|
| 146 | + } catch (Exception e) { |
|---|
| 140 | 147 | LOG.error("Error decoding Base64 token", e); |
|---|
| 141 | 148 | } |
|---|
| 142 | 149 | return null; |
|---|
| .. | .. |
|---|
| 148 | 155 | // OTk3ODRiMzY5NzQ5MWI5NmYyZGQyODRiYjY2ZTU2YzdmMTZjYzM3YTY3N2ExM2M3ODI2MjU5ZTMzOTIyYjUzNSBfY2xpZW50IDE5NzAtMDEtMDFUMDA6NTk6NTkuOTk5KzAxMDA= |
|---|
| 149 | 156 | String t = new TokenHelper().generateToken("_client", new Date(-1)); |
|---|
| 150 | 157 | 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)); |
|---|
| 152 | 159 | |
|---|
| 153 | 160 | System.out.println("is valid client token: " + new TokenHelper().isTokenValid(t)); |
|---|
| 154 | 161 | } |
|---|