From 5b33f20716813b283ee301d1a3f2e3891a97b89c Mon Sep 17 00:00:00 2001 From: "iann.agasen" Date: Fri, 23 Aug 2024 22:09:25 +0800 Subject: [PATCH] mix of authentication, backend, frontend works --- src/backend/api/build.gradle | 6 +- .../core/order/rest/OrderRestController.java | 7 +- .../agasen/auth_server/SecurityConfig.java | 4 +- .../InventoryServiceApplication.java | 4 +- .../resourceserver/EcomAuthentication.java | 31 ++ .../EcomJwtAuthenticationConverter.java | 24 ++ .../rest/InventoryControllerTest.java | 19 +- .../agasen/ecom/order/OrderApplication.java | 2 +- .../resourceserver/EcomAuthentication.java | 2 +- .../EcomJwtAuthenticationConverter.java | 2 +- .../ecom/order/rest/OrderController.java | 4 +- .../ecom/order/security/SecurityConfig.java | 75 +++- .../order/service/DefaultOrderService.java | 9 +- src/backend/util/build.gradle | 2 +- .../util/security/AuthenticationFacade.java | 22 +- .../app/all-orders/all-orders.component.ts | 19 +- .../app/all-orders/all-orders.service.spec.ts | 16 + .../src/app/all-orders/all-orders.service.ts | 21 + .../src/app/all-orders/model/order-item.ts | 6 + .../app/all-orders/model/purchase-order.ts | 7 + .../api/orderservice/.openapi-generator/FILES | 20 - .../orderservice/.openapi-generator/VERSION | 1 - .../app/api/orderservice/api-configuration.ts | 20 - .../src/app/api/orderservice/api.module.ts | 48 --- .../src/app/api/orderservice/api/api.ts | 5 - .../api/orderController.service.ts | 296 -------------- .../src/app/api/orderservice/base-service.ts | 34 -- .../order-controller/get-order-inventory.ts | 31 -- .../fn/order-controller/get-orders.ts | 29 -- .../fn/order-controller/place-order.ts | 32 -- .../orderservice/fn/test-controller/test.ts | 32 -- .../orderservice/model/createOrderRequest.ts | 19 - .../api/orderservice/model/httpErrorInfo.ts | 21 - .../app/api/orderservice/model/inventory.ts | 21 - .../api/orderservice/model/inventoryUpdate.ts | 34 -- .../src/app/api/orderservice/model/models.ts | 6 - .../app/api/orderservice/model/orderItem.ts | 19 - .../api/orderservice/model/purchaseOrder.ts | 30 -- .../src/app/api/orderservice/models.ts | 8 - .../models/create-order-request.ts | 7 - .../orderservice/models/http-error-info.ts | 9 - .../orderservice/models/inventory-update.ts | 10 - .../app/api/orderservice/models/inventory.ts | 9 - .../app/api/orderservice/models/order-item.ts | 7 - .../api/orderservice/models/purchase-order.ts | 9 - .../app/api/orderservice/request-builder.ts | 368 ------------------ .../src/app/api/orderservice/services.ts | 2 - .../services/order-controller.service.ts | 102 ----- .../services/test-controller.service.ts | 54 --- .../api/orderservice/strict-http-response.ts | 10 - .../angular-client/src/app/app.component.ts | 6 + .../angular-client/src/app/app.config.ts | 5 +- .../angular-client/src/app/app.routes.ts | 8 +- .../interceptor/authentication.interceptor.ts | 61 +++ .../auth/service/authentication.service.ts | 1 + .../app/auth/service/permission.service.ts | 29 ++ .../src/app/home/public/public.component.ts | 3 + src/frontend/angular-client/src/index.html | 2 +- 58 files changed, 330 insertions(+), 1360 deletions(-) create mode 100644 src/backend/microservices/inventory-service/src/main/java/dev/agasen/ecom/inventory/auth/resourceserver/EcomAuthentication.java create mode 100644 src/backend/microservices/inventory-service/src/main/java/dev/agasen/ecom/inventory/auth/resourceserver/EcomJwtAuthenticationConverter.java rename src/backend/{api/src/main/java/dev/agasen/ecom/api => microservices/order-service/src/main/java/dev/agasen/ecom/order}/auth/resourceserver/EcomAuthentication.java (94%) rename src/backend/{api/src/main/java/dev/agasen/ecom/api => microservices/order-service/src/main/java/dev/agasen/ecom/order}/auth/resourceserver/EcomJwtAuthenticationConverter.java (93%) create mode 100644 src/frontend/angular-client/src/app/all-orders/all-orders.service.spec.ts create mode 100644 src/frontend/angular-client/src/app/all-orders/all-orders.service.ts create mode 100644 src/frontend/angular-client/src/app/all-orders/model/order-item.ts create mode 100644 src/frontend/angular-client/src/app/all-orders/model/purchase-order.ts delete mode 100644 src/frontend/angular-client/src/app/api/orderservice/.openapi-generator/FILES delete mode 100644 src/frontend/angular-client/src/app/api/orderservice/.openapi-generator/VERSION delete mode 100644 src/frontend/angular-client/src/app/api/orderservice/api-configuration.ts delete mode 100644 src/frontend/angular-client/src/app/api/orderservice/api.module.ts delete mode 100644 src/frontend/angular-client/src/app/api/orderservice/api/api.ts delete mode 100644 src/frontend/angular-client/src/app/api/orderservice/api/orderController.service.ts delete mode 100644 src/frontend/angular-client/src/app/api/orderservice/base-service.ts delete mode 100644 src/frontend/angular-client/src/app/api/orderservice/fn/order-controller/get-order-inventory.ts delete mode 100644 src/frontend/angular-client/src/app/api/orderservice/fn/order-controller/get-orders.ts delete mode 100644 src/frontend/angular-client/src/app/api/orderservice/fn/order-controller/place-order.ts delete mode 100644 src/frontend/angular-client/src/app/api/orderservice/fn/test-controller/test.ts delete mode 100644 src/frontend/angular-client/src/app/api/orderservice/model/createOrderRequest.ts delete mode 100644 src/frontend/angular-client/src/app/api/orderservice/model/httpErrorInfo.ts delete mode 100644 src/frontend/angular-client/src/app/api/orderservice/model/inventory.ts delete mode 100644 src/frontend/angular-client/src/app/api/orderservice/model/inventoryUpdate.ts delete mode 100644 src/frontend/angular-client/src/app/api/orderservice/model/models.ts delete mode 100644 src/frontend/angular-client/src/app/api/orderservice/model/orderItem.ts delete mode 100644 src/frontend/angular-client/src/app/api/orderservice/model/purchaseOrder.ts delete mode 100644 src/frontend/angular-client/src/app/api/orderservice/models.ts delete mode 100644 src/frontend/angular-client/src/app/api/orderservice/models/create-order-request.ts delete mode 100644 src/frontend/angular-client/src/app/api/orderservice/models/http-error-info.ts delete mode 100644 src/frontend/angular-client/src/app/api/orderservice/models/inventory-update.ts delete mode 100644 src/frontend/angular-client/src/app/api/orderservice/models/inventory.ts delete mode 100644 src/frontend/angular-client/src/app/api/orderservice/models/order-item.ts delete mode 100644 src/frontend/angular-client/src/app/api/orderservice/models/purchase-order.ts delete mode 100644 src/frontend/angular-client/src/app/api/orderservice/request-builder.ts delete mode 100644 src/frontend/angular-client/src/app/api/orderservice/services.ts delete mode 100644 src/frontend/angular-client/src/app/api/orderservice/services/order-controller.service.ts delete mode 100644 src/frontend/angular-client/src/app/api/orderservice/services/test-controller.service.ts delete mode 100644 src/frontend/angular-client/src/app/api/orderservice/strict-http-response.ts create mode 100644 src/frontend/angular-client/src/app/auth/interceptor/authentication.interceptor.ts create mode 100644 src/frontend/angular-client/src/app/auth/service/permission.service.ts diff --git a/src/backend/api/build.gradle b/src/backend/api/build.gradle index 02240a2..eb70933 100644 --- a/src/backend/api/build.gradle +++ b/src/backend/api/build.gradle @@ -33,9 +33,9 @@ dependencies { testCompileOnly 'org.projectlombok:lombok:1.18.30' testAnnotationProcessor 'org.projectlombok:lombok:1.18.30' - implementation 'org.springframework.boot:spring-boot-starter-security' - implementation 'org.springframework.security:spring-security-oauth2-resource-server' - implementation 'org.springframework.security:spring-security-oauth2-jose' + // implementation 'org.springframework.boot:spring-boot-starter-security' + // implementation 'org.springframework.security:spring-security-oauth2-resource-server' + // implementation 'org.springframework.security:spring-security-oauth2-jose' } diff --git a/src/backend/api/src/main/java/dev/agasen/ecom/api/core/order/rest/OrderRestController.java b/src/backend/api/src/main/java/dev/agasen/ecom/api/core/order/rest/OrderRestController.java index e396ea5..9e32fd0 100644 --- a/src/backend/api/src/main/java/dev/agasen/ecom/api/core/order/rest/OrderRestController.java +++ b/src/backend/api/src/main/java/dev/agasen/ecom/api/core/order/rest/OrderRestController.java @@ -3,8 +3,9 @@ import java.util.List; import org.springframework.boot.autoconfigure.security.oauth2.resource.OAuth2ResourceServerProperties.Jwt; -import org.springframework.security.core.annotation.AuthenticationPrincipal; -import org.springframework.security.core.userdetails.UserDetails; +// import org.springframework.security.core.annotation.AuthenticationPrincipal; +// import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; @@ -27,6 +28,6 @@ public interface OrderRestController { Mono getOrderInventory(@PathVariable(value = "productId") Long productId); @GetMapping(value = "/orders", produces = "application/json") - Mono> getOrders(@AuthenticationPrincipal Jwt userDetails, @RequestParam(name = "type", defaultValue = "ALL") GetOrdersType type); + Mono> getOrders(@RequestParam(name = "type", defaultValue = "ALL") GetOrdersType type); } diff --git a/src/backend/infrastructure/auth-server/src/main/java/dev/agasen/auth_server/SecurityConfig.java b/src/backend/infrastructure/auth-server/src/main/java/dev/agasen/auth_server/SecurityConfig.java index d48f07f..d483ea3 100644 --- a/src/backend/infrastructure/auth-server/src/main/java/dev/agasen/auth_server/SecurityConfig.java +++ b/src/backend/infrastructure/auth-server/src/main/java/dev/agasen/auth_server/SecurityConfig.java @@ -337,8 +337,8 @@ public WebMvcConfigurer corsConfigurer() { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") - .allowedOrigins("http://localhost:4200") - .allowedMethods("GET", "POST", "PUT", "DELETE") + .allowedOrigins("*") + .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") .allowedHeaders("*"); } }; diff --git a/src/backend/microservices/inventory-service/src/main/java/dev/agasen/ecom/inventory/InventoryServiceApplication.java b/src/backend/microservices/inventory-service/src/main/java/dev/agasen/ecom/inventory/InventoryServiceApplication.java index b9ce3ac..e9c60a8 100644 --- a/src/backend/microservices/inventory-service/src/main/java/dev/agasen/ecom/inventory/InventoryServiceApplication.java +++ b/src/backend/microservices/inventory-service/src/main/java/dev/agasen/ecom/inventory/InventoryServiceApplication.java @@ -7,7 +7,9 @@ import org.springframework.security.config.web.server.ServerHttpSecurity; import org.springframework.security.web.server.SecurityWebFilterChain; -import dev.agasen.ecom.api.auth.resourceserver.EcomJwtAuthenticationConverter; +import dev.agasen.ecom.inventory.auth.resourceserver.EcomJwtAuthenticationConverter; + + @SpringBootApplication(scanBasePackages = "dev.agasen") public class InventoryServiceApplication { diff --git a/src/backend/microservices/inventory-service/src/main/java/dev/agasen/ecom/inventory/auth/resourceserver/EcomAuthentication.java b/src/backend/microservices/inventory-service/src/main/java/dev/agasen/ecom/inventory/auth/resourceserver/EcomAuthentication.java new file mode 100644 index 0000000..4e95e2d --- /dev/null +++ b/src/backend/microservices/inventory-service/src/main/java/dev/agasen/ecom/inventory/auth/resourceserver/EcomAuthentication.java @@ -0,0 +1,31 @@ +package dev.agasen.ecom.inventory.auth.resourceserver; + +import java.util.Collection; + +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.oauth2.jwt.Jwt; +import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken; + + + +public class EcomAuthentication extends JwtAuthenticationToken { + /** + * + * A Custom Authentication object used to reshape the JWT token + * + */ + + private final String priority; + + public EcomAuthentication(Jwt jwt, Collection authorities, String priority) { + super(jwt, authorities); + + // we are adding a new field to the JwtAuthenticationToken class + // this priority field is in a claim in the JWT token + this.priority = priority; + } + + public String getPriority() { + return priority; + } +} diff --git a/src/backend/microservices/inventory-service/src/main/java/dev/agasen/ecom/inventory/auth/resourceserver/EcomJwtAuthenticationConverter.java b/src/backend/microservices/inventory-service/src/main/java/dev/agasen/ecom/inventory/auth/resourceserver/EcomJwtAuthenticationConverter.java new file mode 100644 index 0000000..7361638 --- /dev/null +++ b/src/backend/microservices/inventory-service/src/main/java/dev/agasen/ecom/inventory/auth/resourceserver/EcomJwtAuthenticationConverter.java @@ -0,0 +1,24 @@ +package dev.agasen.ecom.inventory.auth.resourceserver; + +import java.util.List; + +import org.springframework.core.convert.converter.Converter; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.oauth2.jwt.Jwt; +import org.springframework.stereotype.Component; + +import reactor.core.publisher.Mono; + +@Component +public class EcomJwtAuthenticationConverter implements Converter>{ + + @Override + public Mono convert(Jwt source) { + List authorities = List.of(() -> "read"); + + String priority = String.valueOf(source.getClaims().get("priority")); + + return Mono.just(new EcomAuthentication(source, authorities, priority)); + } + +} \ No newline at end of file diff --git a/src/backend/microservices/inventory-service/src/test/java/dev/agasen/ecom/inventory/rest/InventoryControllerTest.java b/src/backend/microservices/inventory-service/src/test/java/dev/agasen/ecom/inventory/rest/InventoryControllerTest.java index 6ededb3..d689534 100644 --- a/src/backend/microservices/inventory-service/src/test/java/dev/agasen/ecom/inventory/rest/InventoryControllerTest.java +++ b/src/backend/microservices/inventory-service/src/test/java/dev/agasen/ecom/inventory/rest/InventoryControllerTest.java @@ -3,38 +3,23 @@ import static org.mockito.Mockito.when; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest; -import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.context.annotation.Import; -import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.reactive.server.WebTestClient; -import dev.agasen.ecom.inventory.BaseIntegrationTest; -import dev.agasen.ecom.inventory.messaging.OrderEventRouterConfig; -import dev.agasen.ecom.inventory.repository.InventoryRepository; -import dev.agasen.ecom.inventory.repository.InventoryUpdateRepository; import dev.agasen.ecom.inventory.rest.service.InventoryQueryService; -import dev.agasen.ecom.util.mongo.SequenceGeneratorService; @WebFluxTest(controllers = InventoryController.class) -// @Import(InventoryQueryService.class) public class InventoryControllerTest /* extends BaseIntegrationTest */ { @Autowired WebTestClient webTestClient; @MockBean InventoryQueryService queryService; - // @MockBean InventoryUpdateRepository repository; - // @MockBean OrderEventRouterConfig orderEventRouterConfig; - // @MockBean InventoryRepository inventoryRepository; - // @MockBean SequenceGeneratorService sequenceGeneratorService; @Test void testDeduct() { - } + } @Test void testGetInventories() { @@ -56,4 +41,4 @@ void testGetInventory() { void testGetInventoryHistory() { } -} +} \ No newline at end of file diff --git a/src/backend/microservices/order-service/src/main/java/dev/agasen/ecom/order/OrderApplication.java b/src/backend/microservices/order-service/src/main/java/dev/agasen/ecom/order/OrderApplication.java index e7f3ba6..c81db8e 100644 --- a/src/backend/microservices/order-service/src/main/java/dev/agasen/ecom/order/OrderApplication.java +++ b/src/backend/microservices/order-service/src/main/java/dev/agasen/ecom/order/OrderApplication.java @@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; -import dev.agasen.ecom.api.auth.resourceserver.EcomAuthentication; +import dev.agasen.ecom.order.auth.resourceserver.EcomAuthentication; import lombok.extern.slf4j.Slf4j; diff --git a/src/backend/api/src/main/java/dev/agasen/ecom/api/auth/resourceserver/EcomAuthentication.java b/src/backend/microservices/order-service/src/main/java/dev/agasen/ecom/order/auth/resourceserver/EcomAuthentication.java similarity index 94% rename from src/backend/api/src/main/java/dev/agasen/ecom/api/auth/resourceserver/EcomAuthentication.java rename to src/backend/microservices/order-service/src/main/java/dev/agasen/ecom/order/auth/resourceserver/EcomAuthentication.java index 8faec01..0c4b8e1 100644 --- a/src/backend/api/src/main/java/dev/agasen/ecom/api/auth/resourceserver/EcomAuthentication.java +++ b/src/backend/microservices/order-service/src/main/java/dev/agasen/ecom/order/auth/resourceserver/EcomAuthentication.java @@ -1,4 +1,4 @@ -package dev.agasen.ecom.api.auth.resourceserver; +package dev.agasen.ecom.order.auth.resourceserver; import java.util.Collection; diff --git a/src/backend/api/src/main/java/dev/agasen/ecom/api/auth/resourceserver/EcomJwtAuthenticationConverter.java b/src/backend/microservices/order-service/src/main/java/dev/agasen/ecom/order/auth/resourceserver/EcomJwtAuthenticationConverter.java similarity index 93% rename from src/backend/api/src/main/java/dev/agasen/ecom/api/auth/resourceserver/EcomJwtAuthenticationConverter.java rename to src/backend/microservices/order-service/src/main/java/dev/agasen/ecom/order/auth/resourceserver/EcomJwtAuthenticationConverter.java index 9ec8473..7e0ae34 100644 --- a/src/backend/api/src/main/java/dev/agasen/ecom/api/auth/resourceserver/EcomJwtAuthenticationConverter.java +++ b/src/backend/microservices/order-service/src/main/java/dev/agasen/ecom/order/auth/resourceserver/EcomJwtAuthenticationConverter.java @@ -1,4 +1,4 @@ -package dev.agasen.ecom.api.auth.resourceserver; +package dev.agasen.ecom.order.auth.resourceserver; import java.util.List; diff --git a/src/backend/microservices/order-service/src/main/java/dev/agasen/ecom/order/rest/OrderController.java b/src/backend/microservices/order-service/src/main/java/dev/agasen/ecom/order/rest/OrderController.java index a1e3154..97a6624 100644 --- a/src/backend/microservices/order-service/src/main/java/dev/agasen/ecom/order/rest/OrderController.java +++ b/src/backend/microservices/order-service/src/main/java/dev/agasen/ecom/order/rest/OrderController.java @@ -42,8 +42,8 @@ public Mono getOrderInventory(Long productId) { } @Override - public Mono> getOrders(@AuthenticationPrincipal Jwt jwt, GetOrdersType type) { - log.info("Jwt is {}", jwt); + public Mono> getOrders(GetOrdersType type) { + // log.info("Jwt is {}", jwt); return orderService.getOrders(type) .map(PurchaseOrderEntity::toRestModel) .collectList(); diff --git a/src/backend/microservices/order-service/src/main/java/dev/agasen/ecom/order/security/SecurityConfig.java b/src/backend/microservices/order-service/src/main/java/dev/agasen/ecom/order/security/SecurityConfig.java index 0a0b0ea..71b9ab4 100644 --- a/src/backend/microservices/order-service/src/main/java/dev/agasen/ecom/order/security/SecurityConfig.java +++ b/src/backend/microservices/order-service/src/main/java/dev/agasen/ecom/order/security/SecurityConfig.java @@ -2,18 +2,39 @@ import java.util.Collection; import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.server.reactive.ServerHttpRequest; +import org.springframework.http.server.reactive.ServerHttpResponse; +import org.springframework.security.config.Customizer; import org.springframework.security.config.web.server.ServerHttpSecurity; +import org.springframework.security.config.web.server.ServerHttpSecurity.CorsSpec; import org.springframework.security.core.Authentication; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.web.server.SecurityWebFilterChain; +import org.springframework.web.cors.CorsConfiguration; +import org.springframework.web.filter.CorsFilter; import org.springframework.web.reactive.config.CorsRegistry; import org.springframework.web.reactive.config.WebFluxConfigurer; -import dev.agasen.ecom.api.auth.resourceserver.EcomJwtAuthenticationConverter; +import dev.agasen.ecom.order.auth.resourceserver.EcomJwtAuthenticationConverter; import dev.agasen.ecom.util.security.AuthenticationFacade; import lombok.extern.slf4j.Slf4j; +import reactor.core.publisher.Mono; + +import org.springframework.web.cors.reactive.CorsUtils; +import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource; +import org.springframework.web.server.ServerWebExchange; +import org.springframework.web.server.WebFilter; +import org.springframework.web.server.WebFilterChain; + + @Configuration @Slf4j @@ -45,8 +66,7 @@ public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) th http.oauth2ResourceServer( // configure the app as resoruce server c -> c.jwt( // configure the app to use JWT j -> j.jwkSetUri(keySsetUri) // configure the public key set URL that the reesource server will use to validate JWTs - .jwtAuthenticationConverter(new EcomJwtAuthenticationConverter()) - ) + .jwtAuthenticationConverter(new EcomJwtAuthenticationConverter())) ); /** @@ -66,21 +86,52 @@ public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) th .anyExchange().authenticated() ); + // http.cors((Customizer) CorsSpec::disable); + return http.build(); } + // @Bean + // public WebFluxConfigurer corsConfigurer() { + // return new WebFluxConfigurer() { + // @Override + // public void addCorsMappings(CorsRegistry registry) { + // registry.addMapping("/**") + // // .allowCredentials(true) + // .allowedOrigins("*") + // .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") + // .allowedHeaders("*"); + + // } + // }; + // } + @Bean - public WebFluxConfigurer corsConfigurer() { - return new WebFluxConfigurer() { - @Override - public void addCorsMappings(CorsRegistry registry) { - registry.addMapping("/**") - .allowedOrigins("http://localhost:4200") - .allowedMethods("GET", "POST", "PUT", "DELETE") - .allowedHeaders("*"); + @Order(Ordered.HIGHEST_PRECEDENCE) + public WebFilter corsFilter() { + + final String ALLOWED_HEADERS = "*"; + final String ALLOWED_METHODS = "GET, PUT, POST, DELETE, OPTIONS"; + final String ALLOWED_ORIGIN = "*"; + final String MAX_AGE = "3600"; + + return (ServerWebExchange ctx, WebFilterChain chain) -> { + ServerHttpRequest request = ctx.getRequest(); + if (CorsUtils.isCorsRequest(request)) { + ServerHttpResponse response = ctx.getResponse(); + HttpHeaders headers = response.getHeaders(); + headers.add("Access-Control-Allow-Origin", ALLOWED_ORIGIN); + headers.add("Access-Control-Allow-Methods", ALLOWED_METHODS); + headers.add("Access-Control-Max-Age", MAX_AGE); + headers.add("Access-Control-Allow-Headers",ALLOWED_HEADERS); + if (request.getMethod() == HttpMethod.OPTIONS) { + response.setStatusCode(HttpStatus.OK); + return Mono.empty(); + } } + return chain.filter(ctx); }; } - + } diff --git a/src/backend/microservices/order-service/src/main/java/dev/agasen/ecom/order/service/DefaultOrderService.java b/src/backend/microservices/order-service/src/main/java/dev/agasen/ecom/order/service/DefaultOrderService.java index 0fffdab..cc49abf 100644 --- a/src/backend/microservices/order-service/src/main/java/dev/agasen/ecom/order/service/DefaultOrderService.java +++ b/src/backend/microservices/order-service/src/main/java/dev/agasen/ecom/order/service/DefaultOrderService.java @@ -1,5 +1,10 @@ package dev.agasen.ecom.order.service; +import java.util.function.Function; + +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.ReactiveSecurityContextHolder; +import org.springframework.security.core.context.SecurityContext; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -52,7 +57,9 @@ public Mono placeOrder(CreateOrderRequest req) { @Override public Flux getOrders(GetOrdersType type) { - return authFacade.getAuthentication().doOnNext(auth -> log.info("Auth: {}", auth)).thenMany(purchaseOrderRepo.findAll()); + return ReactiveSecurityContextHolder.getContext() + .map((Function) SecurityContext::getAuthentication) + .doOnNext(auth -> log.info("Auth: {}", auth)).thenMany(purchaseOrderRepo.findAll()); } } diff --git a/src/backend/util/build.gradle b/src/backend/util/build.gradle index 9bff9b9..2f099ef 100644 --- a/src/backend/util/build.gradle +++ b/src/backend/util/build.gradle @@ -37,7 +37,7 @@ dependencies { testCompileOnly 'org.projectlombok:lombok:1.18.30' testAnnotationProcessor 'org.projectlombok:lombok:1.18.30' - implementation 'org.springframework.boot:spring-boot-starter-security' + // implementation 'org.springframework.boot:spring-boot-starter-security' } dependencyManagement { diff --git a/src/backend/util/src/main/java/dev/agasen/ecom/util/security/AuthenticationFacade.java b/src/backend/util/src/main/java/dev/agasen/ecom/util/security/AuthenticationFacade.java index d9bfec5..a0d3e76 100644 --- a/src/backend/util/src/main/java/dev/agasen/ecom/util/security/AuthenticationFacade.java +++ b/src/backend/util/src/main/java/dev/agasen/ecom/util/security/AuthenticationFacade.java @@ -2,10 +2,10 @@ import java.util.function.Function; -import org.springframework.security.core.Authentication; -import org.springframework.security.core.context.ReactiveSecurityContextHolder; -import org.springframework.security.core.context.SecurityContext; -import org.springframework.security.core.context.SecurityContextHolder; +// import org.springframework.security.core.Authentication; +// import org.springframework.security.core.context.ReactiveSecurityContextHolder; +// import org.springframework.security.core.context.SecurityContext; +// import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Component; import lombok.extern.slf4j.Slf4j; @@ -15,13 +15,13 @@ @Slf4j public class AuthenticationFacade { - public Mono getAuthentication() { - log.info("context: {}", SecurityContextHolder.getContext()); - return ReactiveSecurityContextHolder.getContext().map((Function) SecurityContext::getAuthentication); - } + // public Mono getAuthentication() { + // log.info("context: {}", SecurityContextHolder.getContext()); + // return ReactiveSecurityContextHolder.getContext().map((Function) SecurityContext::getAuthentication); + // } - public void setAuthentication(Authentication authentication) { - SecurityContextHolder.getContext().setAuthentication(authentication); - } + // public void setAuthentication(Authentication authentication) { + // SecurityContextHolder.getContext().setAuthentication(authentication); + // } } diff --git a/src/frontend/angular-client/src/app/all-orders/all-orders.component.ts b/src/frontend/angular-client/src/app/all-orders/all-orders.component.ts index 4c515fe..9a0d4fa 100644 --- a/src/frontend/angular-client/src/app/all-orders/all-orders.component.ts +++ b/src/frontend/angular-client/src/app/all-orders/all-orders.component.ts @@ -1,8 +1,8 @@ import { Component } from '@angular/core'; import { Observable } from 'rxjs'; import { CommonModule } from '@angular/common'; -import { OrderControllerService } from '../api/orderservice/services'; -import { PurchaseOrder } from '../api/orderservice/models'; +import { AllOrdersService } from './all-orders.service'; +import { PurchaseOrder } from './model/purchase-order'; @Component({ selector: 'app-all-orders', @@ -11,11 +11,11 @@ import { PurchaseOrder } from '../api/orderservice/models'; template: `

All Orders

Here are all the orders

+
    -
  • - +
` }) @@ -24,11 +24,16 @@ export class AllOrdersComponent { allOrders$!: Observable>; constructor( - private _orderControllerService: OrderControllerService + private _orderControllerService: AllOrdersService + ) { } ngOnInit(): void { - this.allOrders$ = this._orderControllerService.getOrders(); + // this._orderControllerService.getAllOrders().subscribe(console.log); + } + + public getAllOrders() { + this._orderControllerService.getAllOrders().subscribe(console.log); } } diff --git a/src/frontend/angular-client/src/app/all-orders/all-orders.service.spec.ts b/src/frontend/angular-client/src/app/all-orders/all-orders.service.spec.ts new file mode 100644 index 0000000..b873bae --- /dev/null +++ b/src/frontend/angular-client/src/app/all-orders/all-orders.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { AllOrdersService } from './all-orders.service'; + +describe('AllOrdersService', () => { + let service: AllOrdersService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(AllOrdersService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/frontend/angular-client/src/app/all-orders/all-orders.service.ts b/src/frontend/angular-client/src/app/all-orders/all-orders.service.ts new file mode 100644 index 0000000..00b334d --- /dev/null +++ b/src/frontend/angular-client/src/app/all-orders/all-orders.service.ts @@ -0,0 +1,21 @@ +import { HttpClient } from '@angular/common/http'; +import { Injectable } from '@angular/core'; +import { PurchaseOrder } from './model/purchase-order'; +import { ACCESS_TOKEN } from '../auth/service/authentication.service'; + +@Injectable({ + providedIn: 'root' +}) +export class AllOrdersService { + + constructor( + private _http: HttpClient, + ) { } + + getAllOrders() { + const accessToken = localStorage.getItem(ACCESS_TOKEN); + console.log("Access Token" + accessToken); + + return this._http.get('http://localhost:8103/orders'); + } +} diff --git a/src/frontend/angular-client/src/app/all-orders/model/order-item.ts b/src/frontend/angular-client/src/app/all-orders/model/order-item.ts new file mode 100644 index 0000000..3dd0c44 --- /dev/null +++ b/src/frontend/angular-client/src/app/all-orders/model/order-item.ts @@ -0,0 +1,6 @@ + +export interface OrderItem { + productId: number; + quantity: number; + price: number; +} \ No newline at end of file diff --git a/src/frontend/angular-client/src/app/all-orders/model/purchase-order.ts b/src/frontend/angular-client/src/app/all-orders/model/purchase-order.ts new file mode 100644 index 0000000..652836b --- /dev/null +++ b/src/frontend/angular-client/src/app/all-orders/model/purchase-order.ts @@ -0,0 +1,7 @@ +import { OrderItem } from "./order-item"; + +export interface PurchaseOrder { + orderId: number; + customerId: number; + orderItems: Array; +} diff --git a/src/frontend/angular-client/src/app/api/orderservice/.openapi-generator/FILES b/src/frontend/angular-client/src/app/api/orderservice/.openapi-generator/FILES deleted file mode 100644 index 55db664..0000000 --- a/src/frontend/angular-client/src/app/api/orderservice/.openapi-generator/FILES +++ /dev/null @@ -1,20 +0,0 @@ -.gitignore -.openapi-generator-ignore -README.md -api.module.ts -api/api.ts -api/orderController.service.ts -api/testController.service.ts -configuration.ts -encoder.ts -git_push.sh -index.ts -model/createOrderRequest.ts -model/httpErrorInfo.ts -model/inventory.ts -model/inventoryUpdate.ts -model/models.ts -model/orderItem.ts -model/purchaseOrder.ts -param.ts -variables.ts diff --git a/src/frontend/angular-client/src/app/api/orderservice/.openapi-generator/VERSION b/src/frontend/angular-client/src/app/api/orderservice/.openapi-generator/VERSION deleted file mode 100644 index 1985849..0000000 --- a/src/frontend/angular-client/src/app/api/orderservice/.openapi-generator/VERSION +++ /dev/null @@ -1 +0,0 @@ -7.7.0 diff --git a/src/frontend/angular-client/src/app/api/orderservice/api-configuration.ts b/src/frontend/angular-client/src/app/api/orderservice/api-configuration.ts deleted file mode 100644 index 5c97934..0000000 --- a/src/frontend/angular-client/src/app/api/orderservice/api-configuration.ts +++ /dev/null @@ -1,20 +0,0 @@ -/* tslint:disable */ -/* eslint-disable */ -import { Injectable } from '@angular/core'; - -/** - * Global configuration - */ -@Injectable({ - providedIn: 'root', -}) -export class ApiConfiguration { - rootUrl: string = 'http://localhost:8103'; -} - -/** - * Parameters for `ApiModule.forRoot()` - */ -export interface ApiConfigurationParams { - rootUrl?: string; -} diff --git a/src/frontend/angular-client/src/app/api/orderservice/api.module.ts b/src/frontend/angular-client/src/app/api/orderservice/api.module.ts deleted file mode 100644 index b63091f..0000000 --- a/src/frontend/angular-client/src/app/api/orderservice/api.module.ts +++ /dev/null @@ -1,48 +0,0 @@ -/* tslint:disable */ -/* eslint-disable */ -import { NgModule, ModuleWithProviders, SkipSelf, Optional } from '@angular/core'; -import { HttpClient } from '@angular/common/http'; -import { ApiConfiguration, ApiConfigurationParams } from './api-configuration'; - -import { OrderControllerService } from './services/order-controller.service'; -import { TestControllerService } from './services/test-controller.service'; - -/** - * Module that provides all services and configuration. - */ -@NgModule({ - imports: [], - exports: [], - declarations: [], - providers: [ - OrderControllerService, - TestControllerService, - ApiConfiguration - ], -}) -export class ApiModule { - static forRoot(params: ApiConfigurationParams): ModuleWithProviders { - return { - ngModule: ApiModule, - providers: [ - { - provide: ApiConfiguration, - useValue: params - } - ] - } - } - - constructor( - @Optional() @SkipSelf() parentModule: ApiModule, - @Optional() http: HttpClient - ) { - if (parentModule) { - throw new Error('ApiModule is already loaded. Import in your base AppModule only.'); - } - if (!http) { - throw new Error('You need to import the HttpClientModule in your AppModule! \n' + - 'See also https://github.com/angular/angular/issues/20575'); - } - } -} diff --git a/src/frontend/angular-client/src/app/api/orderservice/api/api.ts b/src/frontend/angular-client/src/app/api/orderservice/api/api.ts deleted file mode 100644 index 6fcce20..0000000 --- a/src/frontend/angular-client/src/app/api/orderservice/api/api.ts +++ /dev/null @@ -1,5 +0,0 @@ -export * from './orderController.service'; -import { OrderControllerService } from './orderController.service'; -export * from './testController.service'; -import { TestControllerService } from './testController.service'; -export const APIS = [OrderControllerService, TestControllerService]; diff --git a/src/frontend/angular-client/src/app/api/orderservice/api/orderController.service.ts b/src/frontend/angular-client/src/app/api/orderservice/api/orderController.service.ts deleted file mode 100644 index b8433bb..0000000 --- a/src/frontend/angular-client/src/app/api/orderservice/api/orderController.service.ts +++ /dev/null @@ -1,296 +0,0 @@ -/** - * OpenAPI definition - * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) - * - * The version of the OpenAPI document: v0 - * - * - * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). - * https://openapi-generator.tech - * Do not edit the class manually. - */ -/* tslint:disable:no-unused-variable member-ordering */ - -import { Inject, Injectable, Optional } from '@angular/core'; -import { HttpClient, HttpHeaders, HttpParams, - HttpResponse, HttpEvent, HttpParameterCodec, HttpContext - } from '@angular/common/http'; -import { CustomHttpParameterCodec } from '../encoder'; -import { Observable } from 'rxjs'; - -// @ts-ignore -import { CreateOrderRequest } from '../model/createOrderRequest'; -// @ts-ignore -import { HttpErrorInfo } from '../model/httpErrorInfo'; -// @ts-ignore -import { Inventory } from '../model/inventory'; -// @ts-ignore -import { PurchaseOrder } from '../model/purchaseOrder'; - -// @ts-ignore -import { BASE_PATH, COLLECTION_FORMATS } from '../variables'; -import { Configuration } from '../configuration'; - - - -@Injectable({ - providedIn: 'root' -}) -export class OrderControllerService { - - protected basePath = 'http://localhost:8103'; - public defaultHeaders = new HttpHeaders(); - public configuration = new Configuration(); - public encoder: HttpParameterCodec; - - constructor(protected httpClient: HttpClient, @Optional()@Inject(BASE_PATH) basePath: string|string[], @Optional() configuration: Configuration) { - if (configuration) { - this.configuration = configuration; - } - if (typeof this.configuration.basePath !== 'string') { - const firstBasePath = Array.isArray(basePath) ? basePath[0] : undefined; - if (firstBasePath != undefined) { - basePath = firstBasePath; - } - - if (typeof basePath !== 'string') { - basePath = this.basePath; - } - this.configuration.basePath = basePath; - } - this.encoder = this.configuration.encoder || new CustomHttpParameterCodec(); - } - - - // @ts-ignore - private addToHttpParams(httpParams: HttpParams, value: any, key?: string): HttpParams { - if (typeof value === "object" && value instanceof Date === false) { - httpParams = this.addToHttpParamsRecursive(httpParams, value); - } else { - httpParams = this.addToHttpParamsRecursive(httpParams, value, key); - } - return httpParams; - } - - private addToHttpParamsRecursive(httpParams: HttpParams, value?: any, key?: string): HttpParams { - if (value == null) { - return httpParams; - } - - if (typeof value === "object") { - if (Array.isArray(value)) { - (value as any[]).forEach( elem => httpParams = this.addToHttpParamsRecursive(httpParams, elem, key)); - } else if (value instanceof Date) { - if (key != null) { - httpParams = httpParams.append(key, (value as Date).toISOString().substring(0, 10)); - } else { - throw Error("key may not be null if value is Date"); - } - } else { - Object.keys(value).forEach( k => httpParams = this.addToHttpParamsRecursive( - httpParams, value[k], key != null ? `${key}.${k}` : k)); - } - } else if (key != null) { - httpParams = httpParams.append(key, value); - } else { - throw Error("key may not be null if value is not object or array"); - } - return httpParams; - } - - /** - * @param productId - * @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body. - * @param reportProgress flag to report request and response progress. - */ - public getOrderInventory(productId: number, observe?: 'body', reportProgress?: boolean, options?: {httpHeaderAccept?: '*/*', context?: HttpContext, transferCache?: boolean}): Observable; - public getOrderInventory(productId: number, observe?: 'response', reportProgress?: boolean, options?: {httpHeaderAccept?: '*/*', context?: HttpContext, transferCache?: boolean}): Observable>; - public getOrderInventory(productId: number, observe?: 'events', reportProgress?: boolean, options?: {httpHeaderAccept?: '*/*', context?: HttpContext, transferCache?: boolean}): Observable>; - public getOrderInventory(productId: number, observe: any = 'body', reportProgress: boolean = false, options?: {httpHeaderAccept?: '*/*', context?: HttpContext, transferCache?: boolean}): Observable { - if (productId === null || productId === undefined) { - throw new Error('Required parameter productId was null or undefined when calling getOrderInventory.'); - } - - let localVarHeaders = this.defaultHeaders; - - let localVarHttpHeaderAcceptSelected: string | undefined = options && options.httpHeaderAccept; - if (localVarHttpHeaderAcceptSelected === undefined) { - // to determine the Accept header - const httpHeaderAccepts: string[] = [ - '*/*' - ]; - localVarHttpHeaderAcceptSelected = this.configuration.selectHeaderAccept(httpHeaderAccepts); - } - if (localVarHttpHeaderAcceptSelected !== undefined) { - localVarHeaders = localVarHeaders.set('Accept', localVarHttpHeaderAcceptSelected); - } - - let localVarHttpContext: HttpContext | undefined = options && options.context; - if (localVarHttpContext === undefined) { - localVarHttpContext = new HttpContext(); - } - - let localVarTransferCache: boolean | undefined = options && options.transferCache; - if (localVarTransferCache === undefined) { - localVarTransferCache = true; - } - - - let responseType_: 'text' | 'json' | 'blob' = 'json'; - if (localVarHttpHeaderAcceptSelected) { - if (localVarHttpHeaderAcceptSelected.startsWith('text')) { - responseType_ = 'text'; - } else if (this.configuration.isJsonMime(localVarHttpHeaderAcceptSelected)) { - responseType_ = 'json'; - } else { - responseType_ = 'blob'; - } - } - - let localVarPath = `/order/inventory/${this.configuration.encodeParam({name: "productId", value: productId, in: "path", style: "simple", explode: false, dataType: "number", dataFormat: "int64"})}`; - return this.httpClient.request('get', `${this.configuration.basePath}${localVarPath}`, - { - context: localVarHttpContext, - responseType: responseType_, - withCredentials: this.configuration.withCredentials, - headers: localVarHeaders, - observe: observe, - transferCache: localVarTransferCache, - reportProgress: reportProgress - } - ); - } - - /** - * @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body. - * @param reportProgress flag to report request and response progress. - */ - public getOrders(observe?: 'body', reportProgress?: boolean, options?: {httpHeaderAccept?: '*/*', context?: HttpContext, transferCache?: boolean}): Observable>; - public getOrders(observe?: 'response', reportProgress?: boolean, options?: {httpHeaderAccept?: '*/*', context?: HttpContext, transferCache?: boolean}): Observable>>; - public getOrders(observe?: 'events', reportProgress?: boolean, options?: {httpHeaderAccept?: '*/*', context?: HttpContext, transferCache?: boolean}): Observable>>; - public getOrders(observe: any = 'body', reportProgress: boolean = false, options?: {httpHeaderAccept?: '*/*', context?: HttpContext, transferCache?: boolean}): Observable { - - let localVarHeaders = this.defaultHeaders; - - let localVarHttpHeaderAcceptSelected: string | undefined = options && options.httpHeaderAccept; - if (localVarHttpHeaderAcceptSelected === undefined) { - // to determine the Accept header - const httpHeaderAccepts: string[] = [ - '*/*' - ]; - localVarHttpHeaderAcceptSelected = this.configuration.selectHeaderAccept(httpHeaderAccepts); - } - if (localVarHttpHeaderAcceptSelected !== undefined) { - localVarHeaders = localVarHeaders.set('Accept', localVarHttpHeaderAcceptSelected); - } - - let localVarHttpContext: HttpContext | undefined = options && options.context; - if (localVarHttpContext === undefined) { - localVarHttpContext = new HttpContext(); - } - - let localVarTransferCache: boolean | undefined = options && options.transferCache; - if (localVarTransferCache === undefined) { - localVarTransferCache = true; - } - - - let responseType_: 'text' | 'json' | 'blob' = 'json'; - if (localVarHttpHeaderAcceptSelected) { - if (localVarHttpHeaderAcceptSelected.startsWith('text')) { - responseType_ = 'text'; - } else if (this.configuration.isJsonMime(localVarHttpHeaderAcceptSelected)) { - responseType_ = 'json'; - } else { - responseType_ = 'blob'; - } - } - - let localVarPath = `/orders`; - return this.httpClient.request>('get', `${this.configuration.basePath}${localVarPath}`, - { - context: localVarHttpContext, - responseType: responseType_, - withCredentials: this.configuration.withCredentials, - headers: localVarHeaders, - observe: observe, - transferCache: localVarTransferCache, - reportProgress: reportProgress - } - ); - } - - /** - * @param createOrderRequest - * @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body. - * @param reportProgress flag to report request and response progress. - */ - public placeOrder(createOrderRequest: CreateOrderRequest, observe?: 'body', reportProgress?: boolean, options?: {httpHeaderAccept?: '*/*', context?: HttpContext, transferCache?: boolean}): Observable; - public placeOrder(createOrderRequest: CreateOrderRequest, observe?: 'response', reportProgress?: boolean, options?: {httpHeaderAccept?: '*/*', context?: HttpContext, transferCache?: boolean}): Observable>; - public placeOrder(createOrderRequest: CreateOrderRequest, observe?: 'events', reportProgress?: boolean, options?: {httpHeaderAccept?: '*/*', context?: HttpContext, transferCache?: boolean}): Observable>; - public placeOrder(createOrderRequest: CreateOrderRequest, observe: any = 'body', reportProgress: boolean = false, options?: {httpHeaderAccept?: '*/*', context?: HttpContext, transferCache?: boolean}): Observable { - if (createOrderRequest === null || createOrderRequest === undefined) { - throw new Error('Required parameter createOrderRequest was null or undefined when calling placeOrder.'); - } - - let localVarHeaders = this.defaultHeaders; - - let localVarHttpHeaderAcceptSelected: string | undefined = options && options.httpHeaderAccept; - if (localVarHttpHeaderAcceptSelected === undefined) { - // to determine the Accept header - const httpHeaderAccepts: string[] = [ - '*/*' - ]; - localVarHttpHeaderAcceptSelected = this.configuration.selectHeaderAccept(httpHeaderAccepts); - } - if (localVarHttpHeaderAcceptSelected !== undefined) { - localVarHeaders = localVarHeaders.set('Accept', localVarHttpHeaderAcceptSelected); - } - - let localVarHttpContext: HttpContext | undefined = options && options.context; - if (localVarHttpContext === undefined) { - localVarHttpContext = new HttpContext(); - } - - let localVarTransferCache: boolean | undefined = options && options.transferCache; - if (localVarTransferCache === undefined) { - localVarTransferCache = true; - } - - - // to determine the Content-Type header - const consumes: string[] = [ - 'application/json' - ]; - const httpContentTypeSelected: string | undefined = this.configuration.selectHeaderContentType(consumes); - if (httpContentTypeSelected !== undefined) { - localVarHeaders = localVarHeaders.set('Content-Type', httpContentTypeSelected); - } - - let responseType_: 'text' | 'json' | 'blob' = 'json'; - if (localVarHttpHeaderAcceptSelected) { - if (localVarHttpHeaderAcceptSelected.startsWith('text')) { - responseType_ = 'text'; - } else if (this.configuration.isJsonMime(localVarHttpHeaderAcceptSelected)) { - responseType_ = 'json'; - } else { - responseType_ = 'blob'; - } - } - - let localVarPath = `/order`; - return this.httpClient.request('post', `${this.configuration.basePath}${localVarPath}`, - { - context: localVarHttpContext, - body: createOrderRequest, - responseType: responseType_, - withCredentials: this.configuration.withCredentials, - headers: localVarHeaders, - observe: observe, - transferCache: localVarTransferCache, - reportProgress: reportProgress - } - ); - } - -} diff --git a/src/frontend/angular-client/src/app/api/orderservice/base-service.ts b/src/frontend/angular-client/src/app/api/orderservice/base-service.ts deleted file mode 100644 index 24c2c72..0000000 --- a/src/frontend/angular-client/src/app/api/orderservice/base-service.ts +++ /dev/null @@ -1,34 +0,0 @@ -/* tslint:disable */ -/* eslint-disable */ -import { Injectable } from '@angular/core'; -import { HttpClient } from '@angular/common/http'; -import { ApiConfiguration } from './api-configuration'; - -/** - * Base class for services - */ -@Injectable() -export class BaseService { - constructor( - protected config: ApiConfiguration, - protected http: HttpClient - ) { - } - - private _rootUrl?: string; - - /** - * Returns the root url for all operations in this service. If not set directly in this - * service, will fallback to `ApiConfiguration.rootUrl`. - */ - get rootUrl(): string { - return this._rootUrl || this.config.rootUrl; - } - - /** - * Sets the root URL for API operations in this service. - */ - set rootUrl(rootUrl: string) { - this._rootUrl = rootUrl; - } -} diff --git a/src/frontend/angular-client/src/app/api/orderservice/fn/order-controller/get-order-inventory.ts b/src/frontend/angular-client/src/app/api/orderservice/fn/order-controller/get-order-inventory.ts deleted file mode 100644 index 4ac9bb3..0000000 --- a/src/frontend/angular-client/src/app/api/orderservice/fn/order-controller/get-order-inventory.ts +++ /dev/null @@ -1,31 +0,0 @@ -/* tslint:disable */ -/* eslint-disable */ -import { HttpClient, HttpContext, HttpResponse } from '@angular/common/http'; -import { Observable } from 'rxjs'; -import { filter, map } from 'rxjs/operators'; -import { StrictHttpResponse } from '../../strict-http-response'; -import { RequestBuilder } from '../../request-builder'; - -import { Inventory } from '../../models/inventory'; - -export interface GetOrderInventory$Params { - productId: number; -} - -export function getOrderInventory(http: HttpClient, rootUrl: string, params: GetOrderInventory$Params, context?: HttpContext): Observable> { - const rb = new RequestBuilder(rootUrl, getOrderInventory.PATH, 'get'); - if (params) { - rb.path('productId', params.productId, {}); - } - - return http.request( - rb.build({ responseType: 'json', accept: 'application/json', context }) - ).pipe( - filter((r: any): r is HttpResponse => r instanceof HttpResponse), - map((r: HttpResponse) => { - return r as StrictHttpResponse; - }) - ); -} - -getOrderInventory.PATH = '/order/inventory/{productId}'; diff --git a/src/frontend/angular-client/src/app/api/orderservice/fn/order-controller/get-orders.ts b/src/frontend/angular-client/src/app/api/orderservice/fn/order-controller/get-orders.ts deleted file mode 100644 index 1e5b8e0..0000000 --- a/src/frontend/angular-client/src/app/api/orderservice/fn/order-controller/get-orders.ts +++ /dev/null @@ -1,29 +0,0 @@ -/* tslint:disable */ -/* eslint-disable */ -import { HttpClient, HttpContext, HttpResponse } from '@angular/common/http'; -import { Observable } from 'rxjs'; -import { filter, map } from 'rxjs/operators'; -import { StrictHttpResponse } from '../../strict-http-response'; -import { RequestBuilder } from '../../request-builder'; - -import { PurchaseOrder } from '../../models/purchase-order'; - -export interface GetOrders$Params { -} - -export function getOrders(http: HttpClient, rootUrl: string, params?: GetOrders$Params, context?: HttpContext): Observable>> { - const rb = new RequestBuilder(rootUrl, getOrders.PATH, 'get'); - if (params) { - } - - return http.request( - rb.build({ responseType: 'json', accept: 'application/json', context }) - ).pipe( - filter((r: any): r is HttpResponse => r instanceof HttpResponse), - map((r: HttpResponse) => { - return r as StrictHttpResponse>; - }) - ); -} - -getOrders.PATH = '/orders'; diff --git a/src/frontend/angular-client/src/app/api/orderservice/fn/order-controller/place-order.ts b/src/frontend/angular-client/src/app/api/orderservice/fn/order-controller/place-order.ts deleted file mode 100644 index 2e1a6ab..0000000 --- a/src/frontend/angular-client/src/app/api/orderservice/fn/order-controller/place-order.ts +++ /dev/null @@ -1,32 +0,0 @@ -/* tslint:disable */ -/* eslint-disable */ -import { HttpClient, HttpContext, HttpResponse } from '@angular/common/http'; -import { Observable } from 'rxjs'; -import { filter, map } from 'rxjs/operators'; -import { StrictHttpResponse } from '../../strict-http-response'; -import { RequestBuilder } from '../../request-builder'; - -import { CreateOrderRequest } from '../../models/create-order-request'; -import { PurchaseOrder } from '../../models/purchase-order'; - -export interface PlaceOrder$Params { - body: CreateOrderRequest -} - -export function placeOrder(http: HttpClient, rootUrl: string, params: PlaceOrder$Params, context?: HttpContext): Observable> { - const rb = new RequestBuilder(rootUrl, placeOrder.PATH, 'post'); - if (params) { - rb.body(params.body, 'application/json'); - } - - return http.request( - rb.build({ responseType: 'json', accept: 'application/json', context }) - ).pipe( - filter((r: any): r is HttpResponse => r instanceof HttpResponse), - map((r: HttpResponse) => { - return r as StrictHttpResponse; - }) - ); -} - -placeOrder.PATH = '/order'; diff --git a/src/frontend/angular-client/src/app/api/orderservice/fn/test-controller/test.ts b/src/frontend/angular-client/src/app/api/orderservice/fn/test-controller/test.ts deleted file mode 100644 index 7ab9461..0000000 --- a/src/frontend/angular-client/src/app/api/orderservice/fn/test-controller/test.ts +++ /dev/null @@ -1,32 +0,0 @@ -/* tslint:disable */ -/* eslint-disable */ -import { HttpClient, HttpContext, HttpResponse } from '@angular/common/http'; -import { Observable } from 'rxjs'; -import { filter, map } from 'rxjs/operators'; -import { StrictHttpResponse } from '../../strict-http-response'; -import { RequestBuilder } from '../../request-builder'; - - -export interface Test$Params { -} - -export function test(http: HttpClient, rootUrl: string, params?: Test$Params, context?: HttpContext): Observable> { - const rb = new RequestBuilder(rootUrl, test.PATH, 'get'); - if (params) { - } - - return http.request( - rb.build({ responseType: 'blob', accept: '*/*', context }) - ).pipe( - filter((r: any): r is HttpResponse => r instanceof HttpResponse), - map((r: HttpResponse) => { - return r as StrictHttpResponse<{ - [key: string]: string; - }>; - }) - ); -} - -test.PATH = '/test'; diff --git a/src/frontend/angular-client/src/app/api/orderservice/model/createOrderRequest.ts b/src/frontend/angular-client/src/app/api/orderservice/model/createOrderRequest.ts deleted file mode 100644 index 7cc0f24..0000000 --- a/src/frontend/angular-client/src/app/api/orderservice/model/createOrderRequest.ts +++ /dev/null @@ -1,19 +0,0 @@ -/** - * OpenAPI definition - * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) - * - * The version of the OpenAPI document: v0 - * - * - * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). - * https://openapi-generator.tech - * Do not edit the class manually. - */ -import { OrderItem } from './orderItem'; - - -export interface CreateOrderRequest { - customerId?: number; - items?: Array; -} - diff --git a/src/frontend/angular-client/src/app/api/orderservice/model/httpErrorInfo.ts b/src/frontend/angular-client/src/app/api/orderservice/model/httpErrorInfo.ts deleted file mode 100644 index c4556b8..0000000 --- a/src/frontend/angular-client/src/app/api/orderservice/model/httpErrorInfo.ts +++ /dev/null @@ -1,21 +0,0 @@ -/** - * OpenAPI definition - * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) - * - * The version of the OpenAPI document: v0 - * - * - * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). - * https://openapi-generator.tech - * Do not edit the class manually. - */ - - -export interface HttpErrorInfo { - timestamp?: string; - path?: string; - message?: string; - error?: string; - status?: number; -} - diff --git a/src/frontend/angular-client/src/app/api/orderservice/model/inventory.ts b/src/frontend/angular-client/src/app/api/orderservice/model/inventory.ts deleted file mode 100644 index 3f059b2..0000000 --- a/src/frontend/angular-client/src/app/api/orderservice/model/inventory.ts +++ /dev/null @@ -1,21 +0,0 @@ -/** - * OpenAPI definition - * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) - * - * The version of the OpenAPI document: v0 - * - * - * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). - * https://openapi-generator.tech - * Do not edit the class manually. - */ -import { InventoryUpdate } from './inventoryUpdate'; - - -export interface Inventory { - inventoryId?: number; - productId?: number; - stock?: number; - history?: Array; -} - diff --git a/src/frontend/angular-client/src/app/api/orderservice/model/inventoryUpdate.ts b/src/frontend/angular-client/src/app/api/orderservice/model/inventoryUpdate.ts deleted file mode 100644 index 3af7c4e..0000000 --- a/src/frontend/angular-client/src/app/api/orderservice/model/inventoryUpdate.ts +++ /dev/null @@ -1,34 +0,0 @@ -/** - * OpenAPI definition - * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) - * - * The version of the OpenAPI document: v0 - * - * - * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). - * https://openapi-generator.tech - * Do not edit the class manually. - */ - - -export interface InventoryUpdate { - updateId?: number; - inventoryId?: number; - orderId?: number; - type?: InventoryUpdate.TypeEnum; - quantity?: number; - createdAt?: string; -} -export namespace InventoryUpdate { - export type TypeEnum = 'PURCHASE' | 'RESTOCK' | 'CUSTOMER_RETURN' | 'DAMAGED' | 'INVENTORY_ADJUSTMENT' | 'SUPPLIER_RETURN'; - export const TypeEnum = { - Purchase: 'PURCHASE' as TypeEnum, - Restock: 'RESTOCK' as TypeEnum, - CustomerReturn: 'CUSTOMER_RETURN' as TypeEnum, - Damaged: 'DAMAGED' as TypeEnum, - InventoryAdjustment: 'INVENTORY_ADJUSTMENT' as TypeEnum, - SupplierReturn: 'SUPPLIER_RETURN' as TypeEnum - }; -} - - diff --git a/src/frontend/angular-client/src/app/api/orderservice/model/models.ts b/src/frontend/angular-client/src/app/api/orderservice/model/models.ts deleted file mode 100644 index f3a3c92..0000000 --- a/src/frontend/angular-client/src/app/api/orderservice/model/models.ts +++ /dev/null @@ -1,6 +0,0 @@ -export * from './createOrderRequest'; -export * from './httpErrorInfo'; -export * from './inventory'; -export * from './inventoryUpdate'; -export * from './orderItem'; -export * from './purchaseOrder'; diff --git a/src/frontend/angular-client/src/app/api/orderservice/model/orderItem.ts b/src/frontend/angular-client/src/app/api/orderservice/model/orderItem.ts deleted file mode 100644 index 5669671..0000000 --- a/src/frontend/angular-client/src/app/api/orderservice/model/orderItem.ts +++ /dev/null @@ -1,19 +0,0 @@ -/** - * OpenAPI definition - * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) - * - * The version of the OpenAPI document: v0 - * - * - * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). - * https://openapi-generator.tech - * Do not edit the class manually. - */ - - -export interface OrderItem { - productId?: number; - quantity?: number; - price?: number; -} - diff --git a/src/frontend/angular-client/src/app/api/orderservice/model/purchaseOrder.ts b/src/frontend/angular-client/src/app/api/orderservice/model/purchaseOrder.ts deleted file mode 100644 index 2a60f11..0000000 --- a/src/frontend/angular-client/src/app/api/orderservice/model/purchaseOrder.ts +++ /dev/null @@ -1,30 +0,0 @@ -/** - * OpenAPI definition - * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) - * - * The version of the OpenAPI document: v0 - * - * - * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). - * https://openapi-generator.tech - * Do not edit the class manually. - */ -import { OrderItem } from './orderItem'; - - -export interface PurchaseOrder { - orderId?: number; - customerId?: number; - items?: Array; - orderStatus?: PurchaseOrder.OrderStatusEnum; -} -export namespace PurchaseOrder { - export type OrderStatusEnum = 'PENDING' | 'COMPLETED' | 'CANCELLED'; - export const OrderStatusEnum = { - Pending: 'PENDING' as OrderStatusEnum, - Completed: 'COMPLETED' as OrderStatusEnum, - Cancelled: 'CANCELLED' as OrderStatusEnum - }; -} - - diff --git a/src/frontend/angular-client/src/app/api/orderservice/models.ts b/src/frontend/angular-client/src/app/api/orderservice/models.ts deleted file mode 100644 index f395081..0000000 --- a/src/frontend/angular-client/src/app/api/orderservice/models.ts +++ /dev/null @@ -1,8 +0,0 @@ -/* tslint:disable */ -/* eslint-disable */ -export { CreateOrderRequest } from './models/create-order-request'; -export { HttpErrorInfo } from './models/http-error-info'; -export { Inventory } from './models/inventory'; -export { InventoryUpdate } from './models/inventory-update'; -export { OrderItem } from './models/order-item'; -export { PurchaseOrder } from './models/purchase-order'; diff --git a/src/frontend/angular-client/src/app/api/orderservice/models/create-order-request.ts b/src/frontend/angular-client/src/app/api/orderservice/models/create-order-request.ts deleted file mode 100644 index 283bc68..0000000 --- a/src/frontend/angular-client/src/app/api/orderservice/models/create-order-request.ts +++ /dev/null @@ -1,7 +0,0 @@ -/* tslint:disable */ -/* eslint-disable */ -import { OrderItem } from '../models/order-item'; -export interface CreateOrderRequest { - customerId?: number; - items?: Array; -} diff --git a/src/frontend/angular-client/src/app/api/orderservice/models/http-error-info.ts b/src/frontend/angular-client/src/app/api/orderservice/models/http-error-info.ts deleted file mode 100644 index cc2a42b..0000000 --- a/src/frontend/angular-client/src/app/api/orderservice/models/http-error-info.ts +++ /dev/null @@ -1,9 +0,0 @@ -/* tslint:disable */ -/* eslint-disable */ -export interface HttpErrorInfo { - error?: string; - message?: string; - path?: string; - status?: number; - timestamp?: string; -} diff --git a/src/frontend/angular-client/src/app/api/orderservice/models/inventory-update.ts b/src/frontend/angular-client/src/app/api/orderservice/models/inventory-update.ts deleted file mode 100644 index 535206f..0000000 --- a/src/frontend/angular-client/src/app/api/orderservice/models/inventory-update.ts +++ /dev/null @@ -1,10 +0,0 @@ -/* tslint:disable */ -/* eslint-disable */ -export interface InventoryUpdate { - createdAt?: string; - inventoryId?: number; - orderId?: number; - quantity?: number; - type?: 'PURCHASE' | 'RESTOCK' | 'CUSTOMER_RETURN' | 'DAMAGED' | 'INVENTORY_ADJUSTMENT' | 'SUPPLIER_RETURN'; - updateId?: number; -} diff --git a/src/frontend/angular-client/src/app/api/orderservice/models/inventory.ts b/src/frontend/angular-client/src/app/api/orderservice/models/inventory.ts deleted file mode 100644 index fca7a69..0000000 --- a/src/frontend/angular-client/src/app/api/orderservice/models/inventory.ts +++ /dev/null @@ -1,9 +0,0 @@ -/* tslint:disable */ -/* eslint-disable */ -import { InventoryUpdate } from '../models/inventory-update'; -export interface Inventory { - history?: Array; - inventoryId?: number; - productId?: number; - stock?: number; -} diff --git a/src/frontend/angular-client/src/app/api/orderservice/models/order-item.ts b/src/frontend/angular-client/src/app/api/orderservice/models/order-item.ts deleted file mode 100644 index d229b22..0000000 --- a/src/frontend/angular-client/src/app/api/orderservice/models/order-item.ts +++ /dev/null @@ -1,7 +0,0 @@ -/* tslint:disable */ -/* eslint-disable */ -export interface OrderItem { - price?: number; - productId?: number; - quantity?: number; -} diff --git a/src/frontend/angular-client/src/app/api/orderservice/models/purchase-order.ts b/src/frontend/angular-client/src/app/api/orderservice/models/purchase-order.ts deleted file mode 100644 index cd05b66..0000000 --- a/src/frontend/angular-client/src/app/api/orderservice/models/purchase-order.ts +++ /dev/null @@ -1,9 +0,0 @@ -/* tslint:disable */ -/* eslint-disable */ -import { OrderItem } from '../models/order-item'; -export interface PurchaseOrder { - customerId?: number; - items?: Array; - orderId?: number; - orderStatus?: 'PENDING' | 'COMPLETED' | 'CANCELLED'; -} diff --git a/src/frontend/angular-client/src/app/api/orderservice/request-builder.ts b/src/frontend/angular-client/src/app/api/orderservice/request-builder.ts deleted file mode 100644 index 5b7a63b..0000000 --- a/src/frontend/angular-client/src/app/api/orderservice/request-builder.ts +++ /dev/null @@ -1,368 +0,0 @@ -/* tslint:disable */ -/* eslint-disable */ -import { HttpRequest, HttpParameterCodec, HttpParams, HttpHeaders, HttpContext } from '@angular/common/http'; - -/** - * Custom parameter codec to correctly handle the plus sign in parameter - * values. See https://github.com/angular/angular/issues/18261 - */ -class ParameterCodec implements HttpParameterCodec { - encodeKey(key: string): string { - return encodeURIComponent(key); - } - - encodeValue(value: string): string { - return encodeURIComponent(value); - } - - decodeKey(key: string): string { - return decodeURIComponent(key); - } - - decodeValue(value: string): string { - return decodeURIComponent(value); - } -} -const ParameterCodecInstance = new ParameterCodec(); - -/** - * Defines the options for appending a parameter - */ -interface ParameterOptions { - style?: string; - explode?: boolean; -} - -/** - * Base class for a parameter - */ -abstract class Parameter { - constructor(public name: string, public value: any, public options: ParameterOptions, defaultStyle: string, defaultExplode: boolean) { - this.options = options || {}; - if (this.options.style === null || this.options.style === undefined) { - this.options.style = defaultStyle; - } - if (this.options.explode === null || this.options.explode === undefined) { - this.options.explode = defaultExplode; - } - } - - serializeValue(value: any, separator = ','): string { - if (value === null || value === undefined) { - return ''; - } else if (value instanceof Array) { - return value.map(v => this.serializeValue(v).split(separator).join(encodeURIComponent(separator))).join(separator); - } else if (typeof value === 'object') { - const array: string[] = []; - for (const key of Object.keys(value)) { - let propVal = value[key]; - if (propVal !== null && propVal !== undefined) { - propVal = this.serializeValue(propVal).split(separator).join(encodeURIComponent(separator)); - if (this.options.explode) { - array.push(`${key}=${propVal}`); - } else { - array.push(key); - array.push(propVal); - } - } - } - return array.join(separator); - } else { - return String(value); - } - } -} - -/** - * A parameter in the operation path - */ -class PathParameter extends Parameter { - constructor(name: string, value: any, options: ParameterOptions) { - super(name, value, options, 'simple', false); - } - - append(path: string): string { - let value = this.value; - if (value === null || value === undefined) { - value = ''; - } - let prefix = this.options.style === 'label' ? '.' : ''; - let separator = this.options.explode ? prefix === '' ? ',' : prefix : ','; - let alreadySerialized = false; - if (this.options.style === 'matrix') { - // The parameter name is just used as prefix, except in some cases... - prefix = `;${this.name}=`; - if (this.options.explode && typeof value === 'object') { - prefix = ';'; - if (value instanceof Array) { - // For arrays we have to repeat the name for each element - value = value.map(v => `${this.name}=${this.serializeValue(v, ';')}`); - value = value.join(';'); - alreadySerialized = true; - } else { - // For objects we have to put each the key / value pairs - value = this.serializeValue(value, ';'); - alreadySerialized = true - } - } - } - value = prefix + (alreadySerialized ? value : this.serializeValue(value, separator)); - // Replace both the plain variable and the corresponding variant taking in the prefix and explode into account - path = path.replace(`{${this.name}}`, value); - path = path.replace(`{${prefix}${this.name}${this.options.explode ? '*' : ''}}`, value); - return path; - } - - // @ts-ignore - serializeValue(value: any, separator = ','): string { - var result = typeof value === 'string' ? encodeURIComponent(value) : super.serializeValue(value, separator); - result = result.replace(/%3D/g, '='); - result = result.replace(/%3B/g, ';'); - result = result.replace(/%2C/g, ','); - return result; - } -} - -/** - * A parameter in the query - */ -class QueryParameter extends Parameter { - constructor(name: string, value: any, options: ParameterOptions) { - super(name, value, options, 'form', true); - } - - append(params: HttpParams): HttpParams { - if (this.value instanceof Array) { - // Array serialization - if (this.options.explode) { - for (const v of this.value) { - params = params.append(this.name, this.serializeValue(v)); - } - } else { - const separator = this.options.style === 'spaceDelimited' - ? ' ' : this.options.style === 'pipeDelimited' - ? '|' : ','; - return params.append(this.name, this.serializeValue(this.value, separator)); - } - } else if (this.value !== null && typeof this.value === 'object') { - // Object serialization - if (this.options.style === 'deepObject') { - // Append a parameter for each key, in the form `name[key]` - for (const key of Object.keys(this.value)) { - const propVal = this.value[key]; - if (propVal !== null && propVal !== undefined) { - params = params.append(`${this.name}[${key}]`, this.serializeValue(propVal)); - } - } - } else if (this.options.explode) { - // Append a parameter for each key without using the parameter name - for (const key of Object.keys(this.value)) { - const propVal = this.value[key]; - if (propVal !== null && propVal !== undefined) { - params = params.append(key, this.serializeValue(propVal)); - } - } - } else { - // Append a single parameter whose values are a comma-separated list of key,value,key,value... - const array: any[] = []; - for (const key of Object.keys(this.value)) { - const propVal = this.value[key]; - if (propVal !== null && propVal !== undefined) { - array.push(key); - array.push(propVal); - } - } - params = params.append(this.name, this.serializeValue(array)); - } - } else if (this.value !== null && this.value !== undefined) { - // Plain value - params = params.append(this.name, this.serializeValue(this.value)); - } - return params; - } -} - -/** - * A parameter in the HTTP request header - */ -class HeaderParameter extends Parameter { - constructor(name: string, value: any, options: ParameterOptions) { - super(name, value, options, 'simple', false); - } - - append(headers: HttpHeaders): HttpHeaders { - if (this.value !== null && this.value !== undefined) { - if (this.value instanceof Array) { - for (const v of this.value) { - headers = headers.append(this.name, this.serializeValue(v)); - } - } else { - headers = headers.append(this.name, this.serializeValue(this.value)); - } - } - return headers; - } -} - -/** - * Helper to build http requests from parameters - */ -export class RequestBuilder { - - private _path = new Map(); - private _query = new Map(); - private _header = new Map(); - _bodyContent: any | null; - _bodyContentType?: string; - - constructor( - public rootUrl: string, - public operationPath: string, - public method: string) { - } - - /** - * Sets a path parameter - */ - path(name: string, value: any, options?: ParameterOptions): void { - this._path.set(name, new PathParameter(name, value, options || {})); - } - - /** - * Sets a query parameter - */ - query(name: string, value: any, options?: ParameterOptions): void { - this._query.set(name, new QueryParameter(name, value, options || {})); - } - - /** - * Sets a header parameter - */ - header(name: string, value: any, options?: ParameterOptions): void { - this._header.set(name, new HeaderParameter(name, value, options || {})); - } - - /** - * Sets the body content, along with the content type - */ - body(value: any, contentType = 'application/json'): void { - if (value instanceof Blob) { - this._bodyContentType = value.type; - } else { - this._bodyContentType = contentType; - } - if (this._bodyContentType === 'application/x-www-form-urlencoded' && value !== null && typeof value === 'object') { - // Handle URL-encoded data - const pairs: Array<[string, string]> = []; - for (const key of Object.keys(value)) { - let val = value[key]; - if (!(val instanceof Array)) { - val = [val]; - } - for (const v of val) { - const formValue = this.formDataValue(v); - if (formValue !== null) { - pairs.push([key, formValue]); - } - } - } - this._bodyContent = pairs.map(p => `${encodeURIComponent(p[0])}=${encodeURIComponent(p[1])}`).join('&'); - } else if (this._bodyContentType === 'multipart/form-data') { - // Handle multipart form data - const formData = new FormData(); - if (value !== null && value !== undefined) { - for (const key of Object.keys(value)) { - const val = value[key]; - if (val instanceof Array) { - for (const v of val) { - const toAppend = this.formDataValue(v); - if (toAppend !== null) { - formData.append(key, toAppend); - } - } - } else { - const toAppend = this.formDataValue(val); - if (toAppend !== null) { - formData.set(key, toAppend); - } - } - } - } - this._bodyContent = formData; - } else { - // The body is the plain content - this._bodyContent = value; - } - } - - private formDataValue(value: any): any { - if (value === null || value === undefined) { - return null; - } - if (value instanceof Blob) { - return value; - } - if (typeof value === 'object') { - return new Blob([JSON.stringify(value)], {type: 'application/json'}) - } - return String(value); - } - - /** - * Builds the request with the current set parameters - */ - build(options?: { - /** Which content types to accept */ - accept?: string; - - /** The expected response type */ - responseType?: 'json' | 'text' | 'blob' | 'arraybuffer'; - - /** Whether to report progress on uploads / downloads */ - reportProgress?: boolean; - - /** Allow passing HttpContext for HttpClient */ - context?: HttpContext; - }): HttpRequest { - - options = options || {}; - - // Path parameters - let path = this.operationPath; - for (const pathParam of this._path.values()) { - path = pathParam.append(path); - } - const url = this.rootUrl + path; - - // Query parameters - let httpParams = new HttpParams({ - encoder: ParameterCodecInstance - }); - for (const queryParam of this._query.values()) { - httpParams = queryParam.append(httpParams); - } - - // Header parameters - let httpHeaders = new HttpHeaders(); - if (options.accept) { - httpHeaders = httpHeaders.append('Accept', options.accept); - } - for (const headerParam of this._header.values()) { - httpHeaders = headerParam.append(httpHeaders); - } - - // Request content headers - if (this._bodyContentType && !(this._bodyContent instanceof FormData)) { - httpHeaders = httpHeaders.set('Content-Type', this._bodyContentType); - } - - // Perform the request - return new HttpRequest(this.method.toUpperCase(), url, this._bodyContent, { - params: httpParams, - headers: httpHeaders, - responseType: options.responseType, - reportProgress: options.reportProgress, - context: options.context - }); - } -} diff --git a/src/frontend/angular-client/src/app/api/orderservice/services.ts b/src/frontend/angular-client/src/app/api/orderservice/services.ts deleted file mode 100644 index c0e830c..0000000 --- a/src/frontend/angular-client/src/app/api/orderservice/services.ts +++ /dev/null @@ -1,2 +0,0 @@ -export { OrderControllerService } from './services/order-controller.service'; -export { TestControllerService } from './services/test-controller.service'; diff --git a/src/frontend/angular-client/src/app/api/orderservice/services/order-controller.service.ts b/src/frontend/angular-client/src/app/api/orderservice/services/order-controller.service.ts deleted file mode 100644 index bcfc87b..0000000 --- a/src/frontend/angular-client/src/app/api/orderservice/services/order-controller.service.ts +++ /dev/null @@ -1,102 +0,0 @@ -/* tslint:disable */ -/* eslint-disable */ -import { HttpClient, HttpContext } from '@angular/common/http'; -import { Injectable } from '@angular/core'; -import { Observable } from 'rxjs'; -import { map } from 'rxjs/operators'; - -import { BaseService } from '../base-service'; -import { ApiConfiguration } from '../api-configuration'; -import { StrictHttpResponse } from '../strict-http-response'; - -import { getOrderInventory } from '../fn/order-controller/get-order-inventory'; -import { GetOrderInventory$Params } from '../fn/order-controller/get-order-inventory'; -import { getOrders } from '../fn/order-controller/get-orders'; -import { GetOrders$Params } from '../fn/order-controller/get-orders'; -import { Inventory } from '../models/inventory'; -import { placeOrder } from '../fn/order-controller/place-order'; -import { PlaceOrder$Params } from '../fn/order-controller/place-order'; -import { PurchaseOrder } from '../models/purchase-order'; - -@Injectable({ providedIn: 'root' }) -export class OrderControllerService extends BaseService { - constructor(config: ApiConfiguration, http: HttpClient) { - super(config, http); - } - - /** Path part for operation `placeOrder()` */ - static readonly PlaceOrderPath = '/order'; - - /** - * This method provides access to the full `HttpResponse`, allowing access to response headers. - * To access only the response body, use `placeOrder()` instead. - * - * This method sends `application/json` and handles request body of type `application/json`. - */ - placeOrder$Response(params: PlaceOrder$Params, context?: HttpContext): Observable> { - return placeOrder(this.http, this.rootUrl, params, context); - } - - /** - * This method provides access only to the response body. - * To access the full response (for headers, for example), `placeOrder$Response()` instead. - * - * This method sends `application/json` and handles request body of type `application/json`. - */ - placeOrder(params: PlaceOrder$Params, context?: HttpContext): Observable { - return this.placeOrder$Response(params, context).pipe( - map((r: StrictHttpResponse): PurchaseOrder => r.body) - ); - } - - /** Path part for operation `getOrders()` */ - static readonly GetOrdersPath = '/orders'; - - /** - * This method provides access to the full `HttpResponse`, allowing access to response headers. - * To access only the response body, use `getOrders()` instead. - * - * This method doesn't expect any request body. - */ - getOrders$Response(params?: GetOrders$Params, context?: HttpContext): Observable>> { - return getOrders(this.http, this.rootUrl, params, context); - } - - /** - * This method provides access only to the response body. - * To access the full response (for headers, for example), `getOrders$Response()` instead. - * - * This method doesn't expect any request body. - */ - getOrders(params?: GetOrders$Params, context?: HttpContext): Observable> { - return this.getOrders$Response(params, context).pipe( - map((r: StrictHttpResponse>): Array => r.body) - ); - } - - /** Path part for operation `getOrderInventory()` */ - static readonly GetOrderInventoryPath = '/order/inventory/{productId}'; - - /** - * This method provides access to the full `HttpResponse`, allowing access to response headers. - * To access only the response body, use `getOrderInventory()` instead. - * - * This method doesn't expect any request body. - */ - getOrderInventory$Response(params: GetOrderInventory$Params, context?: HttpContext): Observable> { - return getOrderInventory(this.http, this.rootUrl, params, context); - } - - /** - * This method provides access only to the response body. - * To access the full response (for headers, for example), `getOrderInventory$Response()` instead. - * - * This method doesn't expect any request body. - */ - getOrderInventory(params: GetOrderInventory$Params, context?: HttpContext): Observable { - return this.getOrderInventory$Response(params, context).pipe( - map((r: StrictHttpResponse): Inventory => r.body) - ); - } - -} diff --git a/src/frontend/angular-client/src/app/api/orderservice/services/test-controller.service.ts b/src/frontend/angular-client/src/app/api/orderservice/services/test-controller.service.ts deleted file mode 100644 index 8f57dd7..0000000 --- a/src/frontend/angular-client/src/app/api/orderservice/services/test-controller.service.ts +++ /dev/null @@ -1,54 +0,0 @@ -/* tslint:disable */ -/* eslint-disable */ -import { HttpClient, HttpContext } from '@angular/common/http'; -import { Injectable } from '@angular/core'; -import { Observable } from 'rxjs'; -import { map } from 'rxjs/operators'; - -import { BaseService } from '../base-service'; -import { ApiConfiguration } from '../api-configuration'; -import { StrictHttpResponse } from '../strict-http-response'; - -import { test } from '../fn/test-controller/test'; -import { Test$Params } from '../fn/test-controller/test'; - -@Injectable({ providedIn: 'root' }) -export class TestControllerService extends BaseService { - constructor(config: ApiConfiguration, http: HttpClient) { - super(config, http); - } - - /** Path part for operation `test()` */ - static readonly TestPath = '/test'; - - /** - * This method provides access to the full `HttpResponse`, allowing access to response headers. - * To access only the response body, use `test()` instead. - * - * This method doesn't expect any request body. - */ - test$Response(params?: Test$Params, context?: HttpContext): Observable> { - return test(this.http, this.rootUrl, params, context); - } - - /** - * This method provides access only to the response body. - * To access the full response (for headers, for example), `test$Response()` instead. - * - * This method doesn't expect any request body. - */ - test(params?: Test$Params, context?: HttpContext): Observable<{ -[key: string]: string; -}> { - return this.test$Response(params, context).pipe( - map((r: StrictHttpResponse<{ -[key: string]: string; -}>): { -[key: string]: string; -} => r.body) - ); - } - -} diff --git a/src/frontend/angular-client/src/app/api/orderservice/strict-http-response.ts b/src/frontend/angular-client/src/app/api/orderservice/strict-http-response.ts deleted file mode 100644 index 42589fa..0000000 --- a/src/frontend/angular-client/src/app/api/orderservice/strict-http-response.ts +++ /dev/null @@ -1,10 +0,0 @@ -/* tslint:disable */ -/* eslint-disable */ -import { HttpResponse } from '@angular/common/http'; - -/** - * Constrains the http response to not have the body defined as `T | null`, but `T` only. - */ -export type StrictHttpResponse = HttpResponse & { - readonly body: T; -} diff --git a/src/frontend/angular-client/src/app/app.component.ts b/src/frontend/angular-client/src/app/app.component.ts index 329bf3d..9dbab71 100644 --- a/src/frontend/angular-client/src/app/app.component.ts +++ b/src/frontend/angular-client/src/app/app.component.ts @@ -32,6 +32,7 @@ import { AuthStatus } from './auth/model/auth-status';
+
{{userInfo.sub}}
@@ -61,6 +62,11 @@ export class AppComponent implements OnInit { } + public goToOrders() { + console.log('go to orders'); + this.router.navigate(['/orders']); + } + loginViaOauth2() { this.auth.redirectToOauthLogin(); } diff --git a/src/frontend/angular-client/src/app/app.config.ts b/src/frontend/angular-client/src/app/app.config.ts index 9b14e21..67ce249 100644 --- a/src/frontend/angular-client/src/app/app.config.ts +++ b/src/frontend/angular-client/src/app/app.config.ts @@ -3,14 +3,15 @@ import { provideRouter } from '@angular/router'; import { routes } from './app.routes'; import { provideClientHydration } from '@angular/platform-browser'; -import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http'; +import { provideHttpClient, withInterceptors, withInterceptorsFromDi } from '@angular/common/http'; +import { AuthenticationInterceptor } from './auth/interceptor/authentication.interceptor'; export const appConfig: ApplicationConfig = { providers: [ provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(routes), provideClientHydration(), - provideHttpClient(withInterceptorsFromDi()) + provideHttpClient(withInterceptors([AuthenticationInterceptor])) ] }; diff --git a/src/frontend/angular-client/src/app/app.routes.ts b/src/frontend/angular-client/src/app/app.routes.ts index 06312cc..54971aa 100644 --- a/src/frontend/angular-client/src/app/app.routes.ts +++ b/src/frontend/angular-client/src/app/app.routes.ts @@ -19,7 +19,13 @@ export const routes: Routes = [ title: 'Shop by Category', path: 'public/category/:id', component: PublicCategoryComponent - } + }, + { + title: 'Orders', + path: 'orders', + component: AllOrdersComponent + + }, ]; diff --git a/src/frontend/angular-client/src/app/auth/interceptor/authentication.interceptor.ts b/src/frontend/angular-client/src/app/auth/interceptor/authentication.interceptor.ts new file mode 100644 index 0000000..4d523e7 --- /dev/null +++ b/src/frontend/angular-client/src/app/auth/interceptor/authentication.interceptor.ts @@ -0,0 +1,61 @@ +import { HttpErrorResponse, HttpHandlerFn, HttpHeaders, HttpInterceptorFn, HttpRequest } from '@angular/common/http'; +import { ACCESS_TOKEN, REFRESH_TOKEN } from '../service/authentication.service'; +import { Injectable, inject } from '@angular/core'; +import { Router } from '@angular/router'; +import { catchError, throwError } from 'rxjs'; + +@Injectable({ + providedIn: 'root' +}) +export class HomeRerouterService { + + constructor( + private _router: Router + ) { } + + routeToHome(req: HttpRequest, next: HttpHandlerFn) { + console.log('Intercepting request: ' + req.url); + // insert other requests to be filter + const excluding: boolean = + req.url.includes('/oauth2') || req.url.includes('/public') ; // /oauth2 request have other authentication methods + + if (excluding) return next(req); + + let baseHeaders: { [key: string]: string } = { + // 'Accept': 'application/json', + // 'Content-Type': 'application/json', + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Headers': 'Content-Type', + 'Access-Control-Allow-Methods': 'GET,POST,OPTIONS,DELETE,PUT' + }; + + // TODO: maybe call /oauth2/introspect to check validity of token + const isStillAuthenticated = typeof localStorage !== 'undefined' + && localStorage.getItem(ACCESS_TOKEN) !== null + && localStorage.getItem(ACCESS_TOKEN) !== ''; + + if (isStillAuthenticated) { + baseHeaders['Authorization'] = `Bearer ${localStorage.getItem(ACCESS_TOKEN)}`; + } + + // update the headers of the request + req = req.clone({ + setHeaders: baseHeaders + }) + + return next(req).pipe( + catchError((error: HttpErrorResponse) => { + if (error.status === 401) { + localStorage?.removeItem(ACCESS_TOKEN) + localStorage?.removeItem(REFRESH_TOKEN) + this._router.navigate(['/']) + } + return throwError(() => error); + })); + } + +} + +export const AuthenticationInterceptor: HttpInterceptorFn = (req, next, ) => { + return inject(HomeRerouterService).routeToHome(req, next); +}; \ No newline at end of file diff --git a/src/frontend/angular-client/src/app/auth/service/authentication.service.ts b/src/frontend/angular-client/src/app/auth/service/authentication.service.ts index 6541eeb..9ec4f4d 100644 --- a/src/frontend/angular-client/src/app/auth/service/authentication.service.ts +++ b/src/frontend/angular-client/src/app/auth/service/authentication.service.ts @@ -89,6 +89,7 @@ export class AuthenticationService { private saveToken(token: AuthenticationResponse) { localStorage.setItem(REFRESH_TOKEN, token.refresh_token) + localStorage.setItem(ACCESS_TOKEN, token.access_token) console.log('Obtained access token') } diff --git a/src/frontend/angular-client/src/app/auth/service/permission.service.ts b/src/frontend/angular-client/src/app/auth/service/permission.service.ts new file mode 100644 index 0000000..67edfcf --- /dev/null +++ b/src/frontend/angular-client/src/app/auth/service/permission.service.ts @@ -0,0 +1,29 @@ +import { inject, Injectable } from '@angular/core'; +import { ActivatedRouteSnapshot, CanActivateFn, Router, RouterStateSnapshot } from '@angular/router'; +import { ACCESS_TOKEN } from './authentication.service'; + +@Injectable({ + providedIn: 'root' +}) +export class PermissionService { + + constructor( + private route: Router, + ) { } + + canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { + console.log('Checking if user is logged in') + const accessToken = localStorage.getItem(ACCESS_TOKEN); + if (accessToken === null || accessToken === '') { + this.route.navigate(['/']); + return false; + } else { + return true; + } + + } +} + +// export const AuthGuard: CanActivateFn = (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => { +// return inject(PermissionService).canActivate(route, state) +// } \ No newline at end of file diff --git a/src/frontend/angular-client/src/app/home/public/public.component.ts b/src/frontend/angular-client/src/app/home/public/public.component.ts index 9abf8ac..ddf0cec 100644 --- a/src/frontend/angular-client/src/app/home/public/public.component.ts +++ b/src/frontend/angular-client/src/app/home/public/public.component.ts @@ -4,6 +4,7 @@ import { Observable, of } from 'rxjs'; import { Category } from './model/category'; import { DUMMY_CATEGORIES } from './dummy'; import { Router } from '@angular/router'; +import { HttpClient } from '@angular/common/http'; @Component({ selector: 'app-public', @@ -35,10 +36,12 @@ export class PublicComponent implements OnInit { constructor( private _router: Router, + private _http: HttpClient ) { } ngOnInit(): void { this.categories$ = of(DUMMY_CATEGORIES) + this._http.get('http://localhost:8103/public').subscribe(console.log) } handleClickCategory(category: Category) { diff --git a/src/frontend/angular-client/src/index.html b/src/frontend/angular-client/src/index.html index ec8daed..ed43911 100644 --- a/src/frontend/angular-client/src/index.html +++ b/src/frontend/angular-client/src/index.html @@ -7,7 +7,7 @@ - +