Public Access
Added arch unit tests
This commit is contained in:
1 parent
5aee23f00e
commit
f4b7ae75a9
6 files changed
+151
-13
No files matched your search
@@ -1,6 +1,5 @@
|
||||
package auth_service.configuration.security;
|
||||
|
||||
import auth_service.domain.login.model.User;
|
||||
import io.jsonwebtoken.Jwts;
|
||||
import io.jsonwebtoken.security.Keys;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -29,9 +28,9 @@ public class JwtService {
|
||||
*
|
||||
* @return A signed JWT string
|
||||
*/
|
||||
public String generateToken(User user) {
|
||||
public String generateToken(String email) {
|
||||
return Jwts.builder()
|
||||
.subject(user.getEmail())
|
||||
.subject(email)
|
||||
.issuedAt(new Date(System.currentTimeMillis()))
|
||||
.expiration(new Date(System.currentTimeMillis() + JWT_EXPIRATION_MS))
|
||||
.signWith(getSigningKey())
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
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;
|
||||
@@ -21,6 +22,7 @@ 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.
|
||||
@@ -33,7 +35,9 @@ public class LoginController {
|
||||
public ResponseEntity<LoginResponse> login(@RequestBody LoginRequest request) {
|
||||
|
||||
var user = ApiMapper.toModel(request);
|
||||
var jwt = loginService.login(user);
|
||||
return ResponseEntity.ok(new LoginResponse("Login successful", jwt));
|
||||
var authenticatedUser = loginService.login(user);
|
||||
var token = jwtService.generateToken(authenticatedUser.getEmail());
|
||||
|
||||
return ResponseEntity.ok(new LoginResponse("Login successful", token));
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
package auth_service.domain.login.service;
|
||||
|
||||
import auth_service.configuration.security.JwtService;
|
||||
import auth_service.domain.login.exception.InvalidCredentialsException;
|
||||
import auth_service.domain.login.exception.UserNotFoundException;
|
||||
import auth_service.domain.login.model.User;
|
||||
@@ -16,15 +15,19 @@ import org.springframework.stereotype.Service;
|
||||
public class LoginService {
|
||||
private final MinimalUserRepository minimalUserRepository;
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
private final JwtService jwtService;
|
||||
|
||||
public String login(User userToLogin) {
|
||||
/**
|
||||
* Authenticates a user based on email and raw password.
|
||||
*
|
||||
* @return The authenticated User model
|
||||
*/
|
||||
public User login(User userToLogin) {
|
||||
var userFromDatabase = minimalUserRepository.findByEmail(userToLogin.getEmail()).orElseThrow(() -> new UserNotFoundException("User not found with given email"));
|
||||
var loginSuccessful = passwordEncoder.matches(userToLogin.getPassword(), userFromDatabase.getPassword());
|
||||
if (!loginSuccessful) {
|
||||
throw new InvalidCredentialsException("Invalid credentials");
|
||||
}
|
||||
|
||||
return jwtService.generateToken(userToLogin);
|
||||
return userFromDatabase;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user