diff --git a/pom.xml b/pom.xml
index 91c7ba9..048bd28 100644
--- a/pom.xml
+++ b/pom.xml
@@ -58,6 +58,11 @@
mysql-connector-j
runtime
+
+ org.springframework.boot
+ spring-boot-starter-oauth2-resource-server
+ 3.3.5
+
diff --git a/src/main/java/dev/naman/productservice/controllers/ProductController.java b/src/main/java/dev/naman/productservice/controllers/ProductController.java
index af619d5..15ee315 100644
--- a/src/main/java/dev/naman/productservice/controllers/ProductController.java
+++ b/src/main/java/dev/naman/productservice/controllers/ProductController.java
@@ -3,13 +3,18 @@
import dev.naman.productservice.dtos.ExceptionDto;
import dev.naman.productservice.dtos.GenericProductDto;
import dev.naman.productservice.exceptions.NotFoundException;
+import dev.naman.productservice.security.JwtObject;
+import dev.naman.productservice.security.TokenValidator;
import dev.naman.productservice.services.ProductService;
+import jakarta.annotation.Nullable;
import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
+import java.util.Optional;
@RestController
@RequestMapping("/products")
@@ -17,6 +22,7 @@ public class ProductController {
// @Autowired
// field injection
private ProductService productService;
+ private TokenValidator tokenValidator;
// ....;
// ...;
@@ -24,8 +30,9 @@ public class ProductController {
// constructor injection
// @Autowired
- public ProductController(ProductService productService) {
+ public ProductController(ProductService productService, TokenValidator tokenValidator) {
this.productService = productService;
+ this.tokenValidator = tokenValidator;
}
//
@@ -44,8 +51,21 @@ public List getAllProducts() {
// localhost:8080/products/{id}
// localhost:8080/products/123
@GetMapping("{id}")
- public GenericProductDto getProductById(@PathVariable("id") Long id) throws NotFoundException {
- return productService.getProductById(id);
+ public GenericProductDto getProductById(@Nullable @RequestHeader(HttpHeaders.AUTHORIZATION)String authToken, @PathVariable("id") Long id) throws NotFoundException {
+
+ System.out.println(authToken+" <-authtoken");
+ Optional jwtObjectOptional;
+ String userId= "351e99b7-3150-49db-ad27-74c793d0aea5";
+ JwtObject jwtObject = null;
+ if(authToken!=null){
+ jwtObjectOptional = tokenValidator.validateToken(authToken,userId);
+ if (jwtObjectOptional.isEmpty()){
+ System.out.println("failinggggg");
+ }
+ jwtObject = jwtObjectOptional.get();
+
+ }
+ return productService.getProductById(id,jwtObject.getUserId());
}
@DeleteMapping("{id}")
diff --git a/src/main/java/dev/naman/productservice/dtos/ValidateRequestDto.java b/src/main/java/dev/naman/productservice/dtos/ValidateRequestDto.java
new file mode 100644
index 0000000..527dcf9
--- /dev/null
+++ b/src/main/java/dev/naman/productservice/dtos/ValidateRequestDto.java
@@ -0,0 +1,9 @@
+package dev.naman.productservice.dtos;
+
+import lombok.Data;
+
+@Data
+public class ValidateRequestDto {
+ String token;
+ String userId;
+}
diff --git a/src/main/java/dev/naman/productservice/enums/SessionStatus.java b/src/main/java/dev/naman/productservice/enums/SessionStatus.java
new file mode 100644
index 0000000..cc798ff
--- /dev/null
+++ b/src/main/java/dev/naman/productservice/enums/SessionStatus.java
@@ -0,0 +1,9 @@
+package dev.naman.productservice.enums;
+
+public enum SessionStatus {
+ ACTIVE,
+ INACTIVE,
+ ENDED
+}
+
+
diff --git a/src/main/java/dev/naman/productservice/repositories/ProductRepository.java b/src/main/java/dev/naman/productservice/repositories/ProductRepository.java
index c2bd45c..cbb4018 100644
--- a/src/main/java/dev/naman/productservice/repositories/ProductRepository.java
+++ b/src/main/java/dev/naman/productservice/repositories/ProductRepository.java
@@ -9,6 +9,7 @@
import org.springframework.stereotype.Repository;
import java.util.List;
+import java.util.Optional;
import java.util.UUID;
import java.util.function.Function;
diff --git a/src/main/java/dev/naman/productservice/repositories/SelfProductRepository.java b/src/main/java/dev/naman/productservice/repositories/SelfProductRepository.java
new file mode 100644
index 0000000..718b4e0
--- /dev/null
+++ b/src/main/java/dev/naman/productservice/repositories/SelfProductRepository.java
@@ -0,0 +1,22 @@
+package dev.naman.productservice.repositories;
+
+import dev.naman.productservice.dtos.GenericProductDto;
+import dev.naman.productservice.models.Product;
+import org.springframework.data.jpa.repository.JpaRepository;
+
+import java.util.List;
+import java.util.Optional;
+
+public interface SelfProductRepository extends JpaRepository {
+
+ @Override
+ Optional findById(Long aLong);
+
+ S save(GenericProductDto entity);
+
+ @Override
+ List findAll();
+
+ @Override
+ void deleteById(Long aLong);
+}
diff --git a/src/main/java/dev/naman/productservice/security/JwtObject.java b/src/main/java/dev/naman/productservice/security/JwtObject.java
new file mode 100644
index 0000000..bb3049c
--- /dev/null
+++ b/src/main/java/dev/naman/productservice/security/JwtObject.java
@@ -0,0 +1,16 @@
+package dev.naman.productservice.security;
+
+import lombok.Data;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+@Data
+public class JwtObject {
+ private String email;
+ private String userId;
+ private Date createdAt;
+ private Date expiryAt;
+ private List roles = new ArrayList<>();
+}
diff --git a/src/main/java/dev/naman/productservice/security/Role.java b/src/main/java/dev/naman/productservice/security/Role.java
new file mode 100644
index 0000000..0972c41
--- /dev/null
+++ b/src/main/java/dev/naman/productservice/security/Role.java
@@ -0,0 +1,11 @@
+package dev.naman.productservice.security;
+
+import lombok.Data;
+
+import java.util.Date;
+
+@Data
+public class Role {
+ private String id;
+ private String role;
+}
diff --git a/src/main/java/dev/naman/productservice/security/TokenValidator.java b/src/main/java/dev/naman/productservice/security/TokenValidator.java
new file mode 100644
index 0000000..64a1efc
--- /dev/null
+++ b/src/main/java/dev/naman/productservice/security/TokenValidator.java
@@ -0,0 +1,38 @@
+package dev.naman.productservice.security;
+
+import dev.naman.productservice.dtos.ValidateRequestDto;
+import dev.naman.productservice.enums.SessionStatus;
+import dev.naman.productservice.thirdpartyclients.productsservice.fakestore.FakeStoreProductDto;
+import org.springframework.boot.web.client.RestTemplateBuilder;
+import org.springframework.http.ResponseEntity;
+import org.springframework.stereotype.Service;
+import org.springframework.web.client.RestTemplate;
+
+import java.util.Optional;
+
+@Service
+public class TokenValidator {
+ /**
+ * call user service validate the token
+ * if token is not vald,optionial is valid
+ * @param token
+ * @return
+ */
+ private RestTemplateBuilder restTemplateBuilder;
+ public TokenValidator(RestTemplateBuilder restTemplateBuilder){
+ this.restTemplateBuilder = restTemplateBuilder;
+ }
+ public Optional validateToken(String token, String userId) {
+
+ RestTemplate restTemplate = restTemplateBuilder.build();
+
+
+ ValidateRequestDto validateRequestDto= new ValidateRequestDto();
+ validateRequestDto.setToken(token);
+ validateRequestDto.setUserId(userId);
+ ResponseEntity response =
+ restTemplate.postForEntity("https://localhost:8081/auth/validate",validateRequestDto,SessionStatus.class);
+
+ return Optional.empty();
+ }
+}
diff --git a/src/main/java/dev/naman/productservice/services/FakeStoreProductService.java b/src/main/java/dev/naman/productservice/services/FakeStoreProductService.java
index ceb58af..bfee2db 100644
--- a/src/main/java/dev/naman/productservice/services/FakeStoreProductService.java
+++ b/src/main/java/dev/naman/productservice/services/FakeStoreProductService.java
@@ -2,6 +2,7 @@
import dev.naman.productservice.dtos.GenericProductDto;
import dev.naman.productservice.exceptions.NotFoundException;
+import dev.naman.productservice.security.JwtObject;
import dev.naman.productservice.thirdpartyclients.productsservice.fakestore.FakeStoreProductDto;
import dev.naman.productservice.thirdpartyclients.productsservice.fakestore.FakeStoryProductServiceClient;
import org.springframework.context.annotation.Primary;
@@ -42,7 +43,7 @@ public GenericProductDto createProduct(GenericProductDto product) {
}
@Override
- public GenericProductDto getProductById(Long id) throws NotFoundException {
+ public GenericProductDto getProductById(Long id, String userIdTryingToAccess) throws NotFoundException {
return convertFakeStoreProductIntoGenericProduct(fakeStoryProductServiceClient.getProductById(id));
}
diff --git a/src/main/java/dev/naman/productservice/services/ProductService.java b/src/main/java/dev/naman/productservice/services/ProductService.java
index bc5eb10..e42dacf 100644
--- a/src/main/java/dev/naman/productservice/services/ProductService.java
+++ b/src/main/java/dev/naman/productservice/services/ProductService.java
@@ -2,6 +2,7 @@
import dev.naman.productservice.dtos.GenericProductDto;
import dev.naman.productservice.exceptions.NotFoundException;
+import dev.naman.productservice.security.JwtObject;
import java.util.List;
@@ -9,7 +10,7 @@ public interface ProductService {
GenericProductDto createProduct(GenericProductDto product);
- GenericProductDto getProductById(Long id) throws NotFoundException;
+ GenericProductDto getProductById(Long id, String userIdTryingToAccess) throws NotFoundException;
List getAllProducts();
diff --git a/src/main/java/dev/naman/productservice/services/SelfProductServiceImpl.java b/src/main/java/dev/naman/productservice/services/SelfProductServiceImpl.java
index 260a45b..1699412 100644
--- a/src/main/java/dev/naman/productservice/services/SelfProductServiceImpl.java
+++ b/src/main/java/dev/naman/productservice/services/SelfProductServiceImpl.java
@@ -3,37 +3,76 @@
import dev.naman.productservice.dtos.GenericProductDto;
import dev.naman.productservice.models.Product;
import dev.naman.productservice.repositories.ProductRepository;
+import dev.naman.productservice.repositories.SelfProductRepository;
+import dev.naman.productservice.security.JwtObject;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
+import java.util.ArrayList;
import java.util.List;
+import java.util.Optional;
+import java.util.UUID;
@Primary
@Service("selfProductServiceImpl")
public class SelfProductServiceImpl implements ProductService {
- private ProductRepository productRepository;
+ private SelfProductRepository selfProductRepository;
- public SelfProductServiceImpl(ProductRepository productRepository) {
- this.productRepository = productRepository;
+ public SelfProductServiceImpl(SelfProductRepository selfProductRepository) {
+ this.selfProductRepository = selfProductRepository;
}
@Override
- public GenericProductDto getProductById(Long id) {
- return null;
+ public GenericProductDto getProductById(Long id, String userIdTryingToAccess) {
+ Optional product = selfProductRepository.findById(id);
+ if(product.isEmpty()){
+ return new GenericProductDto();
+ }
+ GenericProductDto genericProductDto = new GenericProductDto();
+ genericProductDto.setTitle(product.get().getTitle());
+ genericProductDto.setPrice(product.get().getPrice().getPrice());
+ genericProductDto.setDescription(product.get().getDescription());
+ genericProductDto.setImage(product.get().getImage());
+ genericProductDto.setCategory(product.get().getCategory().getName());
+ genericProductDto.setId(id);
+
+ return genericProductDto;
}
@Override
public GenericProductDto createProduct(GenericProductDto product) {
- return null;
+ Product prod = selfProductRepository.save(product);
+ GenericProductDto genericProductDto = new GenericProductDto();
+ genericProductDto.setTitle(prod.getTitle());
+ genericProductDto.setPrice(prod.getPrice().getPrice());
+ genericProductDto.setDescription(prod.getDescription());
+ genericProductDto.setImage(prod.getImage());
+ genericProductDto.setCategory(prod.getCategory().getName());
+ genericProductDto.setId(product.getId());
+ return genericProductDto;
}
@Override
public List getAllProducts() {
- return null;
+ List products = selfProductRepository.findAll();
+ List genericProductDtos = new ArrayList<>();
+ for(Product product: products){
+ GenericProductDto genericProductDto = new GenericProductDto();
+ genericProductDto.setTitle(product.getTitle());
+ genericProductDto.setPrice(product.getPrice().getPrice());
+ genericProductDto.setDescription(product.getDescription());
+ genericProductDto.setImage(product.getImage());
+// genericProductDto.setCategory(product.getCategory().getName());
+ genericProductDto.setId(1L);
+ genericProductDtos.add(genericProductDto);
+ }
+ return genericProductDtos;
}
@Override
public GenericProductDto deleteProduct(Long id) {
- return null;
+ selfProductRepository.deleteById(id);
+ // process is same as getbyID
+ return null;
}
}
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index 1f3ba82..338045b 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -12,9 +12,11 @@ fakestore.api.paths.product=/products
server.error.include-stacktrace=never
spring.jpa.hibernate.ddl-auto=update
-spring.datasource.url=jdbc:mysql://localhost:3306/sep23productservice
-spring.datasource.username=sep23productservice
+spring.datasource.url=jdbc:mysql://localhost:3306/productservice
+spring.datasource.username=productservice
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.show-sql=true
+spring.security.oauth2.resourceserver.jwt.issuer-uri=http://localhost:8081
+spring.security.oauth2.resourceserver.jwt.audiences=productclient
#logging.level.org.hibernate.SQL=DEBUG
#logging.level.org.hibernate.type=TRACE