lti-provider/src/main/java/ru/oa2/lti/controller/ResultController.java

67 lines
2.2 KiB
Java

package ru.oa2.lti.controller;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.web.client.RestClientBuilderConfigurer;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClient;
import ru.oa2.lti.ApplicationService;
import ru.oa2.lti.model.LtiLogin;
import ru.oa2.lti.service.jwt.JwtService;
import ru.oa2.lti.service.jwt.TokenService;
import ru.oa2.lti.service.results.ResultService;
import java.util.UUID;
@Slf4j
@RestController
@RequestMapping("/tool/lti/result")
public class ResultController {
final ApplicationService service;
final ResultService resultService;
final TokenService tokenService;
final JwtService jwtService;
public ResultController(ApplicationService applicationService,
ResultService resultService,
TokenService tokenService,
JwtService jwtService) {
this.service = applicationService;
this.resultService = resultService;
this.tokenService = tokenService;
this.jwtService = jwtService;
}
@PostMapping("/docker")
public ResponseEntity result(@RequestBody String body,
HttpServletRequest req) {
log.info("RESULT: {}", body);
HttpSession session = req.getSession(false);
if (session == null) return ResponseEntity.status(401).build();
var ltiLogin = (LtiLogin) session.getAttribute("lti_login");
var assessToken = tokenService.exchangeForAccessToken(
ltiLogin.getClientId());
String idToken = (String) session.getAttribute("id_token");
resultService.setResult(
assessToken,
jwtService.getTokenPayload(idToken)
);
//TODO возвращать json
return ResponseEntity
.accepted()
.body(service.saveResult(body));
}
}