Skip to content
Snippets Groups Projects
Commit a4ff2f16 authored by Hamza Remmal's avatar Hamza Remmal
Browse files

Change the integrity check to use the @PreAuthorize instead of writing the full logic

parent 3b02c4ab
Branches
Tags
No related merge requests found
...@@ -7,6 +7,7 @@ import lombok.extern.slf4j.Slf4j; ...@@ -7,6 +7,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
/** /**
...@@ -18,22 +19,17 @@ import org.springframework.web.bind.annotation.*; ...@@ -18,22 +19,17 @@ import org.springframework.web.bind.annotation.*;
@Slf4j @Slf4j
@RestController @RestController
@RequestMapping("/api/v1/feedback") @RequestMapping("/api/v1/feedback")
public final class FeedbackController { public class FeedbackController {
/** Service to generate and check the integrity of the requests */
private final IntegrityService integrity;
/** Service to communicate with Moodle */ /** Service to communicate with Moodle */
private final MoodleWebService moodle; private final MoodleWebService moodle;
/** /**
* @param moodle service to use to communicate with Moodle. * @param moodle service to use to communicate with Moodle.
* @param integrity service to use for the integrity checks.
*/ */
@Autowired @Autowired
public FeedbackController(MoodleWebService moodle, IntegrityService integrity) { public FeedbackController(MoodleWebService moodle) {
this.moodle = moodle; this.moodle = moodle;
this.integrity = integrity;
} }
/** /**
...@@ -46,19 +42,12 @@ public final class FeedbackController { ...@@ -46,19 +42,12 @@ public final class FeedbackController {
* @apiNote This end point should only be called by the autograde-service itself * @apiNote This end point should only be called by the autograde-service itself
*/ */
@PostMapping("/upload") @PostMapping("/upload")
@PreAuthorize("@integrityService.check(#signature, #id)")
public ResponseEntity<?> submitGrade(@RequestParam int id, public ResponseEntity<?> submitGrade(@RequestParam int id,
@RequestParam String signature, @RequestParam String signature,
@RequestBody UploadFeedbackDTO feedback) @RequestBody UploadFeedbackDTO feedback)
{ {
log.info("Received an 'upload feedback' request for submission with id {}", id); log.info("Received an 'upload feedback' request for submission with id {}", id);
// HR : Check the integrity of the request
if (!integrity.check(signature, id)) {
log.error("Integrity check failed for 'upload feedback' request for submission {}", id);
return ResponseEntity
.status(HttpStatus.UNAUTHORIZED)
.body("The provided signature is not correct !");
}
log.info("Integrity check for 'upload feedback' request for submission {} was successful", id);
// HR : Upload the grade and feedback to Moodle // HR : Upload the grade and feedback to Moodle
try { try {
var res = moodle.upload_feedback(id, feedback.getGrade(), feedback.getFeedback().toJson()); var res = moodle.upload_feedback(id, feedback.getGrade(), feedback.getFeedback().toJson());
......
package ch.epfl.autograde.api.v1.controller; package ch.epfl.autograde.api.v1.controller;
import ch.epfl.autograde.api.v1.service.IntegrityService;
import ch.epfl.autograde.api.v1.service.MoodleWebService; import ch.epfl.autograde.api.v1.service.MoodleWebService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource; import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
...@@ -25,22 +24,17 @@ import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM_VALUE; ...@@ -25,22 +24,17 @@ import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM_VALUE;
@Slf4j @Slf4j
@RestController @RestController
@RequestMapping("/api/v1/submission") @RequestMapping("/api/v1/submission")
public final class SubmissionController { public class SubmissionController {
/** Service to communicate with Moodle */ /** Service to communicate with Moodle */
private final MoodleWebService moodle; private final MoodleWebService moodle;
/** Service to generate and check the integrity of the requests */
private final IntegrityService integrity;
/** /**
* @param moodle service to use to communicate with Moodle. * @param moodle service to use to communicate with Moodle.
* @param integrity service to use for the integrity checks.
*/ */
@Autowired @Autowired
public SubmissionController(MoodleWebService moodle, IntegrityService integrity) { public SubmissionController(MoodleWebService moodle) {
this.moodle = moodle; this.moodle = moodle;
this.integrity = integrity;
} }
/** /**
...@@ -55,17 +49,9 @@ public final class SubmissionController { ...@@ -55,17 +49,9 @@ public final class SubmissionController {
* @apiNote This end point should only be called by the autograde-service itself * @apiNote This end point should only be called by the autograde-service itself
*/ */
@GetMapping("/download") @GetMapping("/download")
@PreAuthorize("@integrityService.check(#signature, #id)")
public ResponseEntity<?> download(@RequestParam int id, @RequestParam String signature) { public ResponseEntity<?> download(@RequestParam int id, @RequestParam String signature) {
log.info("Received a 'download submission' request for submission {}", id); log.info("Received a 'download submission' request for submission {}", id);
// HR : Check the integrity of the signature
if (!integrity.check(signature, id)){
log.error("Integrity check failed for 'download submission' request for submission {}", id);
return ResponseEntity
.status(HttpStatus.UNAUTHORIZED)
.body("The provided signature is not correct !");
}
log.info("Integrity check for 'download submission' request for submission {} was successful", id);
// HR : Serve the actual file, the integrity of the signature was checked // HR : Serve the actual file, the integrity of the signature was checked
try (var file = moodle.download_submission(id)) { try (var file = moodle.download_submission(id)) {
// HR : Prepare the headers // HR : Prepare the headers
......
...@@ -4,6 +4,7 @@ import ch.epfl.autograde.auth.filter.ApiKeyAuthenticationFilter; ...@@ -4,6 +4,7 @@ import ch.epfl.autograde.auth.filter.ApiKeyAuthenticationFilter;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
...@@ -18,6 +19,7 @@ import org.springframework.security.web.authentication.www.BasicAuthenticationFi ...@@ -18,6 +19,7 @@ import org.springframework.security.web.authentication.www.BasicAuthenticationFi
*/ */
@Configuration @Configuration
@EnableWebSecurity @EnableWebSecurity
@EnableMethodSecurity
public class SecurityConfig { public class SecurityConfig {
/** autograde custom API-KEY authentication filter */ /** autograde custom API-KEY authentication filter */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment