Added new tests fixed some problem

This commit is contained in:
lobo committed 2026-04-18 11:28:06 +02:00
1 parent 3f80777e37
commit d63fbd4155
33 files changed
+11125 -79

No files matched your search

@@ -1,5 +1,6 @@
package auth_service.configuration.security;
import auth_service.domain.login.service.TokenGenerator;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;
import org.springframework.stereotype.Service;
@@ -12,7 +13,7 @@ import java.util.Date;
* Service responsible for generating and validating JSON Web Tokens (JWT).
*/
@Service
public class JwtService {
public class JwtService implements TokenGenerator {
// Token validity: 24 hours in milliseconds
private static final long JWT_EXPIRATION_MS = 86400000;
@@ -24,10 +25,10 @@ public class JwtService {
/**
* Generates a secure JWT for the authenticated user.
* * @param user The authenticated user entity
*
* @return A signed JWT string
*/
@Override
public String generateToken(String userId, String email) {
return Jwts.builder()
.subject(email)
@@ -1,6 +1,5 @@
package auth_service.domain.login.controller;
import auth_service.configuration.security.JwtService;
import auth_service.domain.login.controller.dto.request.LoginRequest;
import auth_service.domain.login.controller.dto.response.LoginResponse;
import auth_service.domain.login.controller.mapper.ApiMapper;
@@ -22,12 +21,11 @@ import org.springframework.web.bind.annotation.RestController;
public class LoginController {
private final LoginService loginService;
private final JwtService jwtService;
/**
* Authenticates a user based on email and password.
* * @param request The login credentials
*
* @param request The login credentials
* @return HTTP 200 on success, HTTP 401 on failure
*/
@PostMapping("/login")
@@ -35,8 +33,7 @@ public class LoginController {
public ResponseEntity<LoginResponse> login(@RequestBody LoginRequest request) {
var user = ApiMapper.toModel(request);
var authenticatedUser = loginService.login(user);
var token = jwtService.generateToken(authenticatedUser.getId() + "", authenticatedUser.getEmail());
var token = loginService.login(user);
return ResponseEntity.ok(new LoginResponse("Login successful", token));
}
}
@@ -1,6 +1,6 @@
package auth_service.domain.login.controller;
import auth_service.domain.login.controller.dto.response.ErrorResponse;
import auth_service.shared.controller.dto.response.ErrorResponse;
import auth_service.domain.login.exception.InvalidCredentialsException;
import auth_service.domain.login.exception.UserNotFoundException;
import lombok.NonNull;
@@ -1,4 +0,0 @@
package auth_service.domain.login.controller.dto.response;
public record ErrorResponse(String code, String message) {
}
@@ -15,19 +15,21 @@ import org.springframework.stereotype.Service;
public class LoginService {
private final MinimalUserRepository minimalUserRepository;
private final PasswordEncoder passwordEncoder;
private final TokenGenerator tokenGenerator;
/**
* Authenticates a user based on email and raw password.
* Authenticates a user and returns a token.
*
* @return The authenticated User model
* @return The generated token
*/
public User login(User userToLogin) {
var foundUser = minimalUserRepository.findByEmail(userToLogin.getEmail()).orElseThrow(() -> new UserNotFoundException("User not found with given email"));
public String login(User userToLogin) {
var foundUser = minimalUserRepository.findByEmail(userToLogin.getEmail())
.orElseThrow(() -> new UserNotFoundException("User not found with given email"));
var loginSuccessful = passwordEncoder.matches(userToLogin.getPassword(), foundUser.getPassword());
if (!loginSuccessful) {
throw new InvalidCredentialsException("Invalid credentials");
}
minimalUserRepository.updateLoginDate(foundUser.getId());
return foundUser;
return tokenGenerator.generateToken(String.valueOf(foundUser.getId()), foundUser.getEmail());
}
}
@@ -0,0 +1,16 @@
package auth_service.domain.login.service;
/**
* Port interface for token generation.
* Decouples the login service from specific token implementations (e.g. JWT).
*/
public interface TokenGenerator {
/**
* Generates a token for the given user.
*
* @param userId The unique identifier of the user.
* @param email The email address of the user.
* @return The generated token string.
*/
String generateToken(String userId, String email);
}
@@ -1,6 +1,6 @@
package auth_service.domain.registration.controller;
import auth_service.domain.registration.controller.dto.response.ErrorResponse;
import auth_service.shared.controller.dto.response.ErrorResponse;
import auth_service.domain.registration.exception.EmailAlreadyInUseException;
import auth_service.domain.registration.exception.InvalidInvitationCodeException;
import auth_service.domain.registration.exception.SteamIdAlreadyRegisteredException;
@@ -1,4 +0,0 @@
package auth_service.domain.registration.controller.dto.response;
public record ErrorResponse(String code, String message) {
}
@@ -1,19 +1,19 @@
package auth_service.domain.registration.service;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class CodeValidationService {
private static final String RANDOM_INVITATION_CODE = "Us*kksUuRpTE2CQRsv+^k0n)ksR}X}?#Fo2D4d8ZKG";
@Value("${auth.registration.invitation-code}")
private String invitationCode;
public boolean isCodeValid(String code) {
if (code == null || code.isEmpty()) {
return false;
}
return code.equals(RANDOM_INVITATION_CODE);
return code.equals(invitationCode);
}
}
@@ -1,6 +1,6 @@
package auth_service.domain.user.controller;
import auth_service.domain.user.controller.dto.response.ErrorResponse;
import auth_service.shared.controller.dto.response.ErrorResponse;
import auth_service.domain.user.exception.UserNotFoundException;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
@@ -1,4 +0,0 @@
package auth_service.domain.user.controller.dto.response;
public record ErrorResponse(String code, String message) {
}
@@ -0,0 +1,12 @@
package auth_service.shared.controller.dto.response;
import org.jspecify.annotations.NonNull;
/**
* Shared error response record for all domains.
*
* @param code A machine-readable error code.
* @param message A human-readable error message.
*/
public record ErrorResponse(@NonNull String code, @NonNull String message) {
}
+5 -1
View File
@@ -15,4 +15,8 @@ spring:
security:
jwt:
secret: "NzC9tQP1eTp496SxoVYslH1bqn81Tbfz74NjkFqdcVe7wayjcRN8IeZvJWd1s6NEWgwEXe9rdccvOn2HtJ4fdq"
secret: "NzC9tQP1eTp496SxoVYslH1bqn81Tbfz74NjkFqdcVe7wayjcRN8IeZvJWd1s6NEWgwEXe9rdccvOn2HtJ4fdq"
auth:
registration:
invitation-code: "Us*kksUuRpTE2CQRsv+^k0n)ksR}X}?#Fo2D4d8ZKG"
+6 -1
View File
@@ -50,4 +50,9 @@ security:
app:
cors:
# Production default: strictly allow only the frontend domain
allowed-origins: "https://tbp.andreas-dahm.eu"
allowed-origins: "https://tbp.andreas-dahm.eu"
# Registration settings
auth:
registration:
invitation-code: ${AUTH_INVITATION_CODE}