-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add authentication step at customer order form
- Loading branch information
1 parent
6aff9be
commit d8fafb1
Showing
32 changed files
with
665 additions
and
279 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
backend/app/src/main/java/eu/viandeendirect/service/AuthenticationProducerService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package eu.viandeendirect.service; | ||
|
||
import eu.viandeendirect.api.ProducersApiDelegate; | ||
import eu.viandeendirect.model.Producer; | ||
import eu.viandeendirect.model.Sale; | ||
import eu.viandeendirect.repository.ProducerRepository; | ||
import eu.viandeendirect.repository.SaleRepository; | ||
import eu.viandeendirect.service.specs.AuthenticationProducerServiceSpecs; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken; | ||
import org.springframework.stereotype.Service; | ||
|
||
import static org.springframework.http.HttpStatus.CREATED; | ||
|
||
@Service | ||
@Profile("!test") | ||
public class AuthenticationProducerService implements AuthenticationProducerServiceSpecs { | ||
|
||
@Autowired | ||
ProducerRepository producerRepository; | ||
|
||
@Override | ||
public Producer getAuthenticatedProducer() { | ||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
String email = ((JwtAuthenticationToken)authentication).getToken().getClaimAsString("email"); | ||
Producer producer = producerRepository.findByEmail(email).orElseThrow(); | ||
return producer; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 40 additions & 11 deletions
51
backend/app/src/main/java/eu/viandeendirect/service/ProducerService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,56 @@ | ||
package eu.viandeendirect.service; | ||
|
||
import eu.viandeendirect.api.ProducersApiDelegate; | ||
import eu.viandeendirect.model.Producer; | ||
import eu.viandeendirect.model.Sale; | ||
import eu.viandeendirect.repository.ProducerRepository; | ||
import eu.viandeendirect.service.specs.ProducerServiceSpecs; | ||
import eu.viandeendirect.repository.SaleRepository; | ||
import eu.viandeendirect.service.specs.AuthenticationProducerServiceSpecs; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.springframework.http.HttpStatus.*; | ||
|
||
@Service | ||
@Profile("!test") | ||
public class ProducerService implements ProducerServiceSpecs { | ||
public class ProducerService implements ProducersApiDelegate { | ||
|
||
@Autowired | ||
ProducerRepository producerRepository; | ||
|
||
@Autowired | ||
SaleRepository saleRepository; | ||
|
||
@Autowired | ||
AuthenticationProducerServiceSpecs producerService; | ||
|
||
@Override | ||
public Producer getAuthenticatedProducer() { | ||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
String email = ((JwtAuthenticationToken)authentication).getToken().getClaimAsString("email"); | ||
public ResponseEntity<Sale> createProducerSale(Integer producerId, Sale sale) { | ||
Producer producer = producerService.getAuthenticatedProducer(); | ||
if (!producer.getId().equals(producerId)) { | ||
return new ResponseEntity<>(FORBIDDEN); | ||
} | ||
sale.setSeller(producer); | ||
return new ResponseEntity<>(saleRepository.save(sale), CREATED); | ||
} | ||
|
||
@Override | ||
public ResponseEntity<Producer> getProducer(String email) { | ||
Producer producer = producerRepository.findByEmail(email).orElseThrow(); | ||
return producer; | ||
return new ResponseEntity<>(producer, OK); | ||
} | ||
|
||
@Override | ||
public ResponseEntity<List<Sale>> getProducerSales(Integer producerId) { | ||
Producer producer = producerService.getAuthenticatedProducer(); | ||
if (!producer.getId().equals(producerId)) { | ||
return new ResponseEntity<>(FORBIDDEN); | ||
} | ||
List<Sale> sales = new ArrayList<>(); | ||
saleRepository.findBySeller(producer).forEach(sales::add);; | ||
return new ResponseEntity<>(sales, OK); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...rect/service/ProducerServiceForTests.java → ...uthenticationProducerServiceForTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.