diff --git a/.gitignore b/.gitignore index 73cfdb62c0d09ddc5671811621d994cd2271da64..b82c94fbe7dec8073b73fe9914d08b263803dfaf 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,8 @@ taller *.pdf **/target/ -**/.vscode \ No newline at end of file +**/.vscode +*.ln +*.tmp +**/tmp +*.log \ No newline at end of file diff --git a/java/roomBooking/src/main/java/com/uva/monolith/services/auth/SecurityConfig.java b/java/roomBooking/src/main/java/com/uva/monolith/services/auth/SecurityConfig.java index 38076923f1608d17dac97dbefe4b635caa317eab..e115ffbaeb43990960790c4a49dbbe4d5b237e88 100644 --- a/java/roomBooking/src/main/java/com/uva/monolith/services/auth/SecurityConfig.java +++ b/java/roomBooking/src/main/java/com/uva/monolith/services/auth/SecurityConfig.java @@ -2,7 +2,6 @@ package com.uva.monolith.services.auth; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.security.authorization.AuthorizationDecision; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.web.SecurityFilterChain; @@ -24,23 +23,19 @@ public class SecurityConfig { public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http.csrf(csrf -> csrf.disable()) .authorizeHttpRequests(authorize -> authorize - .requestMatchers("/users").access((authentication, context) -> { - String method = context.getRequest().getMethod(); - String email = context.getRequest().getParameter("email"); - // Permitir POST a /users solo al rol ADMIN - boolean register = method.equals("POST") && authentication.get().getAuthorities().stream() - .anyMatch(auth -> auth.getAuthority().equals("ROLE_ADMIN")); - // Permitir GET a /users con parámetro email solo al rol ADMIN - boolean access = method.equals("GET") && email != null && !email.isEmpty() && - authentication.get().getAuthorities().stream() - .anyMatch(auth -> auth.getAuthority().equals("ROLE_ADMIN")); - return new AuthorizationDecision(register || access); - }) - .requestMatchers("/users/**").hasRole(UserRol.CLIENT.toString()) - .requestMatchers("/hotels/**", "/booking/**").permitAll() // - // .requestMatchers("/users/**", "/hotels/**", "/booking/**").authenticated() // - // Protegidas - ) + // Permitir todas las conexiones + .requestMatchers("").permitAll() + // Acceso restringido a usuarios y administradores + .requestMatchers("users", "users/**") + .hasAnyRole(UserRol.ADMIN.toString(), UserRol.CLIENT.toString()) + // Acceso restringido a gestores de hoteles y administradores + .requestMatchers("hotels", "hotels/**") + .hasAnyRole(UserRol.ADMIN.toString(), UserRol.HOTEL_ADMIN.toString()) + // Acceso restringido a cualquier usuario del sistema + .requestMatchers("bookings", "bookings/**") + .hasAnyRole(UserRol.ADMIN.toString(), UserRol.HOTEL_ADMIN.toString(), UserRol.CLIENT.toString()) + // Rechazar el resto + .anyRequest().denyAll()) // Registra el filtro antes del filtro estándar de autenticación .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class); diff --git a/java/roomBooking/src/main/java/com/uva/monolith/services/users/controllers/UserService.java b/java/roomBooking/src/main/java/com/uva/monolith/services/users/controllers/UserService.java index 136875b5f6ad32296af1ea8d960be2ae499d8dfc..bdaf5acec3769e9c13893c9fd83f4ad17173a3d0 100644 --- a/java/roomBooking/src/main/java/com/uva/monolith/services/users/controllers/UserService.java +++ b/java/roomBooking/src/main/java/com/uva/monolith/services/users/controllers/UserService.java @@ -29,7 +29,6 @@ public class UserService { return passwordEncoder.encode(password); } - @GetMapping("/users") public User findByEmail(@RequestParam String email) { return userRepository.findByEmail(email) .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Usuario no encontrado")); diff --git a/java/services/auth/src/main/java/com/uva/authentication/api/UserAPI.java b/java/services/auth/src/main/java/com/uva/authentication/api/UserAPI.java index fe5bfc6e51342d65e300961b85844e57c5384d96..a2df673233a774d4afc0228b798f0e1b6ba295ac 100644 --- a/java/services/auth/src/main/java/com/uva/authentication/api/UserAPI.java +++ b/java/services/auth/src/main/java/com/uva/authentication/api/UserAPI.java @@ -1,3 +1,4 @@ +// TODO eliminar si realmente no necesitamos comunicar un servicio con otro package com.uva.authentication.api; import org.springframework.beans.factory.annotation.Autowired; @@ -47,8 +48,8 @@ public class UserAPI { String url = USER_API_URL + "?email={" + email + "}"; try { - ResponseEntity<User> userResponse = // restTemplate.getForEntity(url, User.class, email, headers); - restTemplate.exchange(url, HttpMethod.GET, entity, User.class); + ResponseEntity<User> userResponse = restTemplate.getForEntity(url, User.class, email); + // restTemplate.exchange(url, HttpMethod.GET, entity, User.class); return userResponse.getBody(); } catch (HttpClientErrorException e) { if (e.getStatusCode() != HttpStatus.NOT_FOUND) diff --git a/java/services/auth/src/main/java/com/uva/authentication/controllers/AuthController.java b/java/services/auth/src/main/java/com/uva/authentication/controllers/AuthController.java index 7cee57e1387c0dc00f0616f13bc8b0f37f8a1d1a..b9434db06eb6b092331f29353b8d9079139f4c42 100644 --- a/java/services/auth/src/main/java/com/uva/authentication/controllers/AuthController.java +++ b/java/services/auth/src/main/java/com/uva/authentication/controllers/AuthController.java @@ -9,6 +9,7 @@ import com.uva.authentication.models.*; import com.uva.authentication.services.AuthService; @RestController +@CrossOrigin(origins = "*") public class AuthController { @Autowired diff --git a/java/services/auth/src/main/java/com/uva/authentication/services/AuthService.java b/java/services/auth/src/main/java/com/uva/authentication/services/AuthService.java index 5476bf8fb0c370a6da14d3875a0b13fcfa195612..0ed199965af0f0423236ee79ae4bcae406d3f643 100644 --- a/java/services/auth/src/main/java/com/uva/authentication/services/AuthService.java +++ b/java/services/auth/src/main/java/com/uva/authentication/services/AuthService.java @@ -27,9 +27,6 @@ public class AuthService { @Autowired private JwtUtil jwtUtil; - @Autowired - private UserAPI userAPI; - @Autowired private HotelManagerRepository hotelManagerRepository; @@ -37,30 +34,22 @@ public class AuthService { private ClientRepository clientRepository; @Autowired - private UserRepository userRepository; + private UserAPI userAPI; - private String hashPass(String password) { - // return String.valueOf(Objects.hashCode(password)); - String hash = SecurityUtils.encrypt(password); - System.out.println(password + " --> " + hash); - return hash; - } + @Autowired + private UserRepository userRepository; private boolean authenticateUser(LoginRequest request, User user) { - System.err.println(user); if (user == null) return false; - String hashPass = hashPass(request.getPassword()); - System.err.println("PASSWORD: " + request.getPassword() + "\n" + hashPass + "\n" + - user.getPassword()); - // return hashPass.equals(user.getPassword()); return SecurityUtils.checkPassword(request.getPassword(), user.getPassword()); } public String login(LoginRequest loginRequest) { // User user = userAPI.getUserByEmail(loginRequest.getEmail()); User user = userRepository.findByEmail(loginRequest.getEmail()) - .orElseThrow(() -> new HttpClientErrorException(HttpStatus.FORBIDDEN, "Invalid credentials")); + .orElseThrow(() -> new HttpClientErrorException(HttpStatus.FORBIDDEN, + "Invalid credentials")); boolean isAuthenticated = authenticateUser(loginRequest, user); if (!isAuthenticated) { @@ -75,29 +64,30 @@ public class AuthService { public User register(RegisterRequest registerRequest) { // User user = userAPI.getUserByEmail(registerRequest.getEmail()); - Optional<User> user = userRepository.findByEmail(null); + Optional<User> user = userRepository.findByEmail(registerRequest.getEmail()); if (user.isPresent()) throw new HttpClientErrorException(HttpStatus.CONFLICT, "Email already in use"); - String hashPass = hashPass(registerRequest.getPassword()); + String hashPass = SecurityUtils.encrypt(registerRequest.getPassword()); // return userAPI.registerUser(registerRequest); User newUser; if (registerRequest.getRol() == UserRol.HOTEL_ADMIN) { HotelManager hm = new HotelManager(); - hm.setName(registerRequest.getName()); - hm.setEmail(registerRequest.getEmail()); - hm.setRol(registerRequest.getRol()); + // hm.setName(registerRequest.getName()); + // hm.setEmail(registerRequest.getEmail()); + // hm.setRol(registerRequest.getRol()); + BeanUtils.copyProperties(registerRequest, hm); hm.setPassword(hashPass); newUser = hotelManagerRepository.save(hm); } else { Client client = new Client(); - client.setName(registerRequest.getName()); - client.setEmail(registerRequest.getEmail()); + // client.setName(registerRequest.getName()); + // client.setEmail(registerRequest.getEmail()); + BeanUtils.copyProperties(registerRequest, client); client.setRol(UserRol.CLIENT); client.setPassword(hashPass); newUser = clientRepository.save(client); } - System.out.println("<--\n" + hashPass + "\n" + newUser.getPassword() + "\n-->"); return newUser; }