diff --git a/java/services/auth/src/main/java/com/uva/api/auth/api/UserAPI.java b/java/services/auth/src/main/java/com/uva/api/auth/api/UserAPI.java index 56070b8515299ddcf4f412333247c6053d59f898..57f661eb5b822e0172b7f94e036bd3c1c2ac95b1 100644 --- a/java/services/auth/src/main/java/com/uva/api/auth/api/UserAPI.java +++ b/java/services/auth/src/main/java/com/uva/api/auth/api/UserAPI.java @@ -3,7 +3,6 @@ package com.uva.api.auth.api; import java.util.HashMap; import java.util.Map; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -17,8 +16,11 @@ import com.uva.api.auth.models.remote.User; @Component public class UserAPI { - @Autowired - private RestTemplate restTemplate; + private final RestTemplate restTemplate; + + public UserAPI(RestTemplate restTemplate) { + this.restTemplate = restTemplate; + } @Value("${services.external.users.url}") private String USER_API_URL; diff --git a/java/services/auth/src/main/java/com/uva/api/auth/config/RestTemplateConfig.java b/java/services/auth/src/main/java/com/uva/api/auth/config/RestTemplateConfig.java index 3120f8ea70d18aaa6ce5d3dfe478bd6ba12d31c9..ac5b003b28da4a78e8088e5817258cef07c56175 100644 --- a/java/services/auth/src/main/java/com/uva/api/auth/config/RestTemplateConfig.java +++ b/java/services/auth/src/main/java/com/uva/api/auth/config/RestTemplateConfig.java @@ -2,7 +2,6 @@ package com.uva.api.auth.config; import java.util.List; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @@ -10,14 +9,10 @@ import org.springframework.web.client.RestTemplate; @Configuration public class RestTemplateConfig { - @Autowired - private RestTemplateInterceptor interceptor; - @Bean - RestTemplate restTemplate() { + RestTemplate restTemplate(RestTemplateInterceptor interceptor) { RestTemplate restTemplate = new RestTemplate(); restTemplate.setInterceptors(List.of(interceptor)); return restTemplate; - } } diff --git a/java/services/auth/src/main/java/com/uva/api/auth/config/RestTemplateInterceptor.java b/java/services/auth/src/main/java/com/uva/api/auth/config/RestTemplateInterceptor.java index 86c3b72a95b7ee0e397f794639cffc8c49dd318a..de9441ae228c86aefaf651969dd20e977b4ba996 100644 --- a/java/services/auth/src/main/java/com/uva/api/auth/config/RestTemplateInterceptor.java +++ b/java/services/auth/src/main/java/com/uva/api/auth/config/RestTemplateInterceptor.java @@ -2,7 +2,6 @@ package com.uva.api.auth.config; import org.springframework.http.HttpRequest; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpHeaders; import org.springframework.http.client.ClientHttpRequestExecution; import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpResponse; @@ -22,10 +21,9 @@ public class RestTemplateInterceptor implements ClientHttpRequestInterceptor { public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { - // Añadir el encabezado "Authorization" con el valor "Bearer <token>" - HttpHeaders headers = request.getHeaders(); - headers.add("Authorization", - "Bearer " + jwtUtil.getOwnInternalToken()); + String token = jwtUtil.getOwnInternalToken(); + + request.getHeaders().add("Authorization", "Bearer " + token); // Continuar con la solicitud return execution.execute(request, body); diff --git a/java/services/auth/src/main/java/com/uva/api/auth/models/jwt/JwtData.java b/java/services/auth/src/main/java/com/uva/api/auth/models/jwt/JwtData.java index b6c539570679a874aadb27e034fb6f4cbfff3018..adc3e5919dacdc4cc79610778b3c35eff931b22c 100644 --- a/java/services/auth/src/main/java/com/uva/api/auth/models/jwt/JwtData.java +++ b/java/services/auth/src/main/java/com/uva/api/auth/models/jwt/JwtData.java @@ -44,8 +44,6 @@ public class JwtData { // Verificamos si el campo está en el mapa y asignamos el valor Claim claim = decoded.getClaim(field.getName()); - System.out.println(field.getName() + " => " + claim.isMissing() + " " + claim.isNull() + " " + claim.asString() - + " " + decoded.getClaim("rol").asString()); if (!claim.isNull()) { String value = claim.asString(); try { @@ -63,7 +61,6 @@ public class JwtData { } } } - System.out.println("\n\n\n<-- " + this + " -->"); } public boolean isAdmin() { diff --git a/java/services/auth/src/main/java/com/uva/api/auth/utils/JwtUtil.java b/java/services/auth/src/main/java/com/uva/api/auth/utils/JwtUtil.java index bb05964bada6ddc069f9cdec63432fb85f991f81..b083106acaace81d20f6aee63b34021b90e46635 100644 --- a/java/services/auth/src/main/java/com/uva/api/auth/utils/JwtUtil.java +++ b/java/services/auth/src/main/java/com/uva/api/auth/utils/JwtUtil.java @@ -63,6 +63,7 @@ public class JwtUtil { // DATA .withClaim("service", service) .withClaim("email", email) + // .withClaim("rol", "SERVICE") .sign(algorithm); } @@ -70,8 +71,6 @@ public class JwtUtil { public String generateToken(User user) { Algorithm algorithm = Algorithm.HMAC256(secretKey); - System.out.println("\n\n<-- " + user + " " + user.getId() + " -->"); - return JWT .create() diff --git a/java/services/bookings/src/main/java/com/uva/api/bookings/api/TokenAPI.java b/java/services/bookings/src/main/java/com/uva/api/bookings/api/TokenAPI.java index e32f88a29d3c6218814ac72ad2a74f2c1fa9073f..8dd8237326b628627a99fee9c0b1c063eda4b30a 100644 --- a/java/services/bookings/src/main/java/com/uva/api/bookings/api/TokenAPI.java +++ b/java/services/bookings/src/main/java/com/uva/api/bookings/api/TokenAPI.java @@ -3,7 +3,7 @@ package com.uva.api.bookings.api; import java.util.HashMap; import java.util.Map; -import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; @@ -14,8 +14,11 @@ import com.uva.api.bookings.models.external.JwtData; @Component public class TokenAPI { - @Autowired - private RestTemplate restTemplate; + private final RestTemplate restTemplate; + + public TokenAPI(@Qualifier("simpleRestTemplate") RestTemplate restTemplate) { + this.restTemplate = restTemplate; + } @Value("${spring.application.name}") private String service; diff --git a/java/services/bookings/src/main/java/com/uva/api/bookings/config/RestTemplateConfig.java b/java/services/bookings/src/main/java/com/uva/api/bookings/config/RestTemplateConfig.java index 311a06210acbb276aa6ee850bfa8838e00bb55f3..c4f1c01ac7d43fd4226ae9bf992f5b6ef20bd2ae 100644 --- a/java/services/bookings/src/main/java/com/uva/api/bookings/config/RestTemplateConfig.java +++ b/java/services/bookings/src/main/java/com/uva/api/bookings/config/RestTemplateConfig.java @@ -1,5 +1,7 @@ package com.uva.api.bookings.config; +import java.util.List; + import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @@ -7,8 +9,15 @@ import org.springframework.web.client.RestTemplate; @Configuration public class RestTemplateConfig { - @Bean - RestTemplate restTemplate() { + @Bean("simpleRestTemplate") + RestTemplate simpleRestTemplate() { return new RestTemplate(); } + + @Bean + RestTemplate restTemplate(RestTemplateInterceptor interceptor) { + RestTemplate restTemplate = new RestTemplate(); + restTemplate.setInterceptors(List.of(interceptor)); + return restTemplate; + } } diff --git a/java/services/bookings/src/main/java/com/uva/api/bookings/config/RestTemplateInterceptor.java b/java/services/bookings/src/main/java/com/uva/api/bookings/config/RestTemplateInterceptor.java index 8c14c825473c45cff9ced07024be394bbb716099..ce5836245a2b73af9f1aa4ba323710d4938b2b2e 100644 --- a/java/services/bookings/src/main/java/com/uva/api/bookings/config/RestTemplateInterceptor.java +++ b/java/services/bookings/src/main/java/com/uva/api/bookings/config/RestTemplateInterceptor.java @@ -22,7 +22,6 @@ public class RestTemplateInterceptor implements ClientHttpRequestInterceptor { throws IOException { String jwtToken = service.getServiceToken(); - System.out.println("Using token " + jwtToken); request.getHeaders().add("Authorization", "Bearer " + jwtToken); diff --git a/java/services/hotels/src/main/java/com/uva/api/hotels/apis/TokenAPI.java b/java/services/hotels/src/main/java/com/uva/api/hotels/apis/TokenAPI.java index 36f8ca87239bcbe93e7a3e3d7d382f6f5368e357..4e5c5dc2cc9f4f0168a5f98aac469bd9b09e8f50 100644 --- a/java/services/hotels/src/main/java/com/uva/api/hotels/apis/TokenAPI.java +++ b/java/services/hotels/src/main/java/com/uva/api/hotels/apis/TokenAPI.java @@ -3,7 +3,7 @@ package com.uva.api.hotels.apis; import java.util.HashMap; import java.util.Map; -import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; @@ -14,8 +14,11 @@ import com.uva.api.hotels.models.external.JwtData; @Component public class TokenAPI { - @Autowired - private RestTemplate restTemplate; + private final RestTemplate restTemplate; + + public TokenAPI(@Qualifier("simpleRestTemplate") RestTemplate restTemplate) { + this.restTemplate = restTemplate; + } @Value("${spring.application.name}") private String service; diff --git a/java/services/hotels/src/main/java/com/uva/api/hotels/config/RestTemplateConfig.java b/java/services/hotels/src/main/java/com/uva/api/hotels/config/RestTemplateConfig.java index ba682dea6422ed62bf2cff6ec468c302d7ec9d13..211cd5e2a97b49f802d2578e8a35f77b0a88d1ec 100644 --- a/java/services/hotels/src/main/java/com/uva/api/hotels/config/RestTemplateConfig.java +++ b/java/services/hotels/src/main/java/com/uva/api/hotels/config/RestTemplateConfig.java @@ -1,13 +1,23 @@ package com.uva.api.hotels.config; +import java.util.List; + import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class RestTemplateConfig { - @Bean - RestTemplate restTemplate() { + + @Bean("simpleRestTemplate") + RestTemplate simpleRestTemplate() { return new RestTemplate(); } + + @Bean + RestTemplate restTemplate(RestTemplateInterceptor interceptor) { + RestTemplate restTemplate = new RestTemplate(); + restTemplate.setInterceptors(List.of(interceptor)); + return restTemplate; + } } diff --git a/java/services/users/src/main/java/com/uva/api/users/api/TokenAPI.java b/java/services/users/src/main/java/com/uva/api/users/api/TokenAPI.java index 66215f63fc629247a7d59e0eef57c3fe6089824f..fcb72f8090897e5302c8544fda28a9011876ce54 100644 --- a/java/services/users/src/main/java/com/uva/api/users/api/TokenAPI.java +++ b/java/services/users/src/main/java/com/uva/api/users/api/TokenAPI.java @@ -3,7 +3,7 @@ package com.uva.api.users.api; import java.util.HashMap; import java.util.Map; -import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; @@ -14,8 +14,11 @@ import com.uva.api.users.models.remote.JwtData; @Component public class TokenAPI { - @Autowired - private RestTemplate restTemplate; + private final RestTemplate restTemplate; + + public TokenAPI(@Qualifier("simpleRestTemplate") RestTemplate restTemplate) { + this.restTemplate = restTemplate; + } @Value("${spring.application.name}") private String service; diff --git a/java/services/users/src/main/java/com/uva/api/users/config/RestTemplateConfig.java b/java/services/users/src/main/java/com/uva/api/users/config/RestTemplateConfig.java index 973ba41dcc1f515f30bbee321df2c7955af5c9d7..6b9efac7e5516734157e71d98ede467922bd3a83 100644 --- a/java/services/users/src/main/java/com/uva/api/users/config/RestTemplateConfig.java +++ b/java/services/users/src/main/java/com/uva/api/users/config/RestTemplateConfig.java @@ -1,5 +1,7 @@ package com.uva.api.users.config; +import java.util.List; + import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @@ -7,8 +9,14 @@ import org.springframework.web.client.RestTemplate; @Configuration public class RestTemplateConfig { - @Bean - RestTemplate restTemplate() { + @Bean("simpleRestTemplate") + RestTemplate simpleRestTemplate() { return new RestTemplate(); } + + public RestTemplate restTemplate(RestTemplateInterceptor interceptor) { + RestTemplate restTemplate = new RestTemplate(); + restTemplate.setInterceptors(List.of(interceptor)); + return restTemplate; + } } diff --git a/java/services/users/src/main/java/com/uva/api/users/config/RestTemplateInterceptor.java b/java/services/users/src/main/java/com/uva/api/users/config/RestTemplateInterceptor.java index eae1ed2957c822bc08c2e7d99759b71e56dfa64d..1c861eca3ab2195e1939146cae6c439f770d14a7 100644 --- a/java/services/users/src/main/java/com/uva/api/users/config/RestTemplateInterceptor.java +++ b/java/services/users/src/main/java/com/uva/api/users/config/RestTemplateInterceptor.java @@ -1,6 +1,5 @@ package com.uva.api.users.config; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpRequest; import org.springframework.http.client.ClientHttpRequestExecution; import org.springframework.http.client.ClientHttpResponse; @@ -14,15 +13,17 @@ import java.io.IOException; @Component public class RestTemplateInterceptor implements ClientHttpRequestInterceptor { - @Autowired - private TokenService service; + private final TokenService service; + + public RestTemplateInterceptor(TokenService service) { + this.service = service; + } @Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { String jwtToken = service.getServiceToken(); - System.out.println("Using token " + jwtToken); request.getHeaders().add("Authorization", "Bearer " + jwtToken); diff --git a/java/services/users/src/main/java/com/uva/api/users/config/SecurityConfig.java b/java/services/users/src/main/java/com/uva/api/users/config/SecurityConfig.java index 10b87150725c794692d25dd27e239a71dbf8e905..6428cf4979d1718179f3d8de99ffda5b2ee37f09 100644 --- a/java/services/users/src/main/java/com/uva/api/users/config/SecurityConfig.java +++ b/java/services/users/src/main/java/com/uva/api/users/config/SecurityConfig.java @@ -8,8 +8,10 @@ import org.springframework.security.config.annotation.web.configuration.EnableWe import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; +import com.uva.api.users.api.TokenAPI; import com.uva.api.users.filter.JwtAuthenticationFilter; import com.uva.api.users.models.UserRol; +import com.uva.api.users.services.TokenService; @Configuration @EnableWebSecurity diff --git a/java/services/users/src/main/java/com/uva/api/users/filter/JwtAuthenticationFilter.java b/java/services/users/src/main/java/com/uva/api/users/filter/JwtAuthenticationFilter.java index c95fa0a53d63fad9f4ee79c53e8df358b66aeae8..1a297c7c09dca05e5ec1fbdc233ba3a7bdc524ac 100644 --- a/java/services/users/src/main/java/com/uva/api/users/filter/JwtAuthenticationFilter.java +++ b/java/services/users/src/main/java/com/uva/api/users/filter/JwtAuthenticationFilter.java @@ -28,8 +28,11 @@ import java.util.Map; @Component public class JwtAuthenticationFilter implements Filter { - @Autowired - private TokenService service; + private final TokenService service; + + public JwtAuthenticationFilter(TokenService service) { + this.service = service; + } private String getTokenFromRequest(HttpServletRequest request) { String authHeader = request.getHeader("Authorization"); diff --git a/java/services/users/src/main/java/com/uva/api/users/services/TokenService.java b/java/services/users/src/main/java/com/uva/api/users/services/TokenService.java index c543abec77b172775ab23b7b6423771613549a59..f5887b926b9404308c5dd64dd429863b36c4503d 100644 --- a/java/services/users/src/main/java/com/uva/api/users/services/TokenService.java +++ b/java/services/users/src/main/java/com/uva/api/users/services/TokenService.java @@ -3,7 +3,6 @@ package com.uva.api.users.services; import java.util.HashMap; import java.util.Map; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.uva.api.users.api.TokenAPI; @@ -12,8 +11,11 @@ import com.uva.api.users.models.remote.JwtData; @Service public class TokenService { - @Autowired - private TokenAPI api; + private final TokenAPI api; + + public TokenService(TokenAPI api) { + this.api = api; + } private JwtData ownToken; private Map<String, JwtData> cache = new HashMap<>(); diff --git a/poblate/index.js b/poblate/index.js index f51768aa8dd76c90bafaa5bb08bcd2eade6b0231..1a3b3396858e487e66561cbf14ac74935a11ecb1 100644 --- a/poblate/index.js +++ b/poblate/index.js @@ -5,12 +5,13 @@ const { jwtDecode } = require("jwt-decode"); const dev = require("./environments/env"); const prod = require("./environments/env.production"); -// Modo +// Environments consts const args = process.argv; const isProduction = args.includes("--prod"); -const DEBUG = args.includes("--debug"); +const DEBUG = args.includes("--debug") || args.includes("-d"); +const FORCE = args.includes("--force") || args.includes("-f"); -const env = isProduction ? prod : dev; +const env = (isProduction ? prod : dev).env; const { authApi, hotelsApi, bookingsApi } = env; const debug = (...values) => { @@ -58,7 +59,11 @@ const savePost = async (data, first, second = "") => { try { return await axios.post(first, data); } catch (error) { - console.error("ERROR Al REGISTRO"); + console.error("ERROR Al REGISTRO, SE PROCEDE A INTENTAR ACCEDER"); + if (!FORCE) { + console.log("Parece que ya hay datos en el sistema"); + process.exit(0); + } return await axios.post(second, data); } } catch (error) { @@ -76,6 +81,7 @@ async function register(user) { ); const decoded = jwtDecode(data.token); user.id = decoded.id; + user.token = data.token; return user; } @@ -93,11 +99,25 @@ const addUsers = async () => { }; const insertHotel = async ({ manager, hotel }) => { - const { data } = await axios.post(hotelsApi, { - ...hotel, - managerId: manager.id, - }); - return data; + try { + const { data } = await axios.post( + hotelsApi, + { + ...hotel, + managerId: manager.id, + }, + { + headers: { + Authorization: `Bearer ${manager.token}`, + }, + } + ); + return data; + } catch (e) { + // console.error(e); + console.error("ERROR Al INSERTAR HOTEL"); + process.exit(-1); + } }; async function addHotels(managers) { @@ -109,9 +129,19 @@ async function addHotels(managers) { return hotels; } -const insertBookings = async (booking) => { - const { data } = await axios.post(bookingsApi, booking); - return data; +const insertBookings = async (booking, token) => { + try { + const { data } = await axios.post(bookingsApi, booking, { + headers: { + Authorization: `Bearer ${token}`, + }, + }); + return data; + } catch (e) { + // console.error(e); + console.error("ERROR Al INSERTAR HOTEL"); + process.exit(-1); + } }; async function addBookings(clients, hotels) { @@ -119,27 +149,43 @@ async function addBookings(clients, hotels) { const t = hotels.length; for await (const hotel of hotels) { const roomId = getRandomItem(hotel.rooms.filter((r) => r.available)).id; - const userId = getRandomItem(clients).id; + const client = getRandomItem(clients); const des = (i - t / 2) * 15; const date = new Date(); date.setDate(date.getDate() + des); for await (const dates of genDates(date)) { - await insertBookings({ - managerId: hotel.managerId, - userId, - hotelId: hotel.id, - roomId, - ...dates, - }); + await insertBookings( + { + managerId: hotel.managerId, + userId: client.id, + hotelId: hotel.id, + roomId, + ...dates, + }, + client.token + ); } } } +function sleep(ms) { + return new Promise((resolve) => setTimeout(resolve, ms)); +} + async function init() { debug("MODE:", isProduction ? "PRODUCTION" : "DEVELOPMENT"); - debug("ENV:", env.env, "\n"); + debug("ENV:", env, "\n"); const { managers, clients } = await addUsers(); + const time = 2; + debug("USUARIOS REGISTRADOS O IDENTIFICADOS"); + if (DEBUG) { + await sleep(time * 1000); + } const hotels = await addHotels(managers, 3); + debug("HOTELES REGISTRADOS"); + if (DEBUG) { + await sleep(time * 1000); + } await addBookings(clients, hotels); console.log("POBLACIÓN COMPLETADA EXITOSAMENTE"); }