Skip to content

Commit

Permalink
Update TokenService.java
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonardoMeireles55 authored Jan 18, 2025
1 parent 06b55c5 commit 2e7ce51
Showing 1 changed file with 8 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@
import org.springframework.stereotype.Service;

@Service
@ConfigurationProperties(prefix = "api.security")
public class TokenService {

@Value("${API_SECURITY_TOKEN_SECRET}")
private String SECRET;
@Value("${API_SECURITY_ISSUER}")
private String ISSUER;

private String tokenSecret;
private String issuer;

public String generateToken(User user) {
try {
var algorithm = Algorithm.HMAC256(SECRET);
return JWT.create().withIssuer(ISSUER).withSubject(user.getEmail())
var algorithm = Algorithm.HMAC256(tokenSecret);
return JWT.create().withIssuer(issuer).withSubject(user.getEmail())
.withExpiresAt(dateExp()).sign(algorithm);
} catch (JWTCreationException exception) {
throw new RuntimeException("Error generating token", exception);
Expand All @@ -31,8 +30,8 @@ public String generateToken(User user) {

public String getSubject(String tokenJWT) {
try {
var algorithm = Algorithm.HMAC256(SECRET);
return JWT.require(algorithm).withIssuer(ISSUER).build().verify(tokenJWT).getSubject();
var algorithm = Algorithm.HMAC256(tokenSecret);
return JWT.require(algorithm).withIssuer(issuer).build().verify(tokenJWT).getSubject();
} catch (JWTVerificationException exception) {
throw new JWTVerificationException("Invalid token: " + exception.getMessage(),
exception);
Expand Down

0 comments on commit 2e7ce51

Please sign in to comment.