Skip to content

Commit

Permalink
feat: enhance token generation to include expiration date
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonardoMeireles55 committed Jan 27, 2025
1 parent b31befc commit d18394c
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 192 deletions.
30 changes: 17 additions & 13 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ services:
- mysql-volume:/var/lib/mysql
- ./database:/docker-entrypoint-initdb.d
networks:
- app-network
- qualitylab-net

quality-lab-pro:
restart: always
Expand All @@ -37,22 +37,26 @@ services:
EMAIL_TO_SEND_LIST: ${EMAIL_TO_SEND_LIST}
command: [ "java", "-jar", "-Dspring.profiles.active=local", "app.jar" ]
networks:
- app-network
- qualitylab-net

nginx:
restart: always
image: nginx:latest
volumes:
- ./nginx/dev:/etc/nginx:ro
ports:
- '80:80'
- '443:443'
networks:
- app-network
# nginx:
# restart: always
# image: nginx:latest
# volumes:
# - ./nginx/dev:/etc/nginx:ro
# ports:
# - '80:80'
# - '443:443'
# networks:
# - qualitylab-net

volumes:
mysql-volume:

networks:
app-network:
qualitylab-net:
name: qualitylab-net
driver: bridge
attachable: true
driver_opts:
com.docker.network.bridge.name: qualitylab-net
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package leonardo.labutilities.qualitylabpro.dtos.authentication;

public record TokenJwtDTO(String tokenJWT) {
import java.time.Instant;

public record TokenJwtDTO(String tokenJWT, Instant dateExp) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,45 @@
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTCreationException;
import com.auth0.jwt.exceptions.JWTVerificationException;
import leonardo.labutilities.qualitylabpro.dtos.authentication.TokenJwtDTO;
import leonardo.labutilities.qualitylabpro.entities.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZoneId;

@Service
public class TokenService {

@Value("${api.security.token.secret}")
private String SECRET;

@Value("${api.security.issuer}")
private String ISSUER;

public String generateToken(User user) {
try {
var algorithm = Algorithm.HMAC256(SECRET);
return JWT.create().withIssuer(ISSUER).withSubject(user.getEmail())
.withExpiresAt(dateExp()).sign(algorithm);
} catch (JWTCreationException exception) {
throw new RuntimeException("Error generating token", exception);
}
}

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

private Instant dateExp() {
return LocalDateTime.now().plusHours(1).toInstant(ZoneOffset.of("-03:00"));
}
@Value("${api.security.token.secret}")
private String SECRET;

@Value("${api.security.issuer}")
private String ISSUER;

public TokenJwtDTO generateToken(User user) {
try {
var algorithm = Algorithm.HMAC256(SECRET);
return new TokenJwtDTO(JWT.create().withIssuer(ISSUER).withSubject(user.getEmail())
.withExpiresAt(dateExp()).sign(algorithm), dateExp());
} catch (JWTCreationException exception) {
throw new RuntimeException("Error generating token", exception);
}
}

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

private Instant dateExp() {
return LocalDateTime.now().plusHours(1).atZone(ZoneId.systemDefault()).toInstant();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ private void sendRecoveryEmail(RecoveryEmailDTO recoveryEmailDTO) {
+ "\nYour Team",
recoveryEmailDTO.temporaryPassword());
log.info("Sending recovery email to: {}", recoveryEmailDTO.email());
emailService
.sendPlainTextEmail(new EmailDTO(recoveryEmailDTO.email(), subject, message));
emailService.sendPlainTextEmail(new EmailDTO(recoveryEmailDTO.email(), subject, message));
}

public void recoverPassword(String username, String email) {
Expand Down Expand Up @@ -85,7 +84,7 @@ public TokenJwtDTO signIn(String email, String password) {
emailService.notifyFailedUserLogin(user.getUsername(), user.getEmail(),
LocalDateTime.now());
}
return new TokenJwtDTO(tokenService.generateToken(user));
return tokenService.generateToken(user);
}

public void updateUserPassword(String name, String email, String password, String newPassword) {
Expand Down
Loading

0 comments on commit d18394c

Please sign in to comment.