Commit 240b3c18 authored by Grzegorz Pietrusza's avatar Grzegorz Pietrusza

refactor

parent 6e7e351f
......@@ -39,9 +39,9 @@ public class HashValidator
return Hex.encodeHexString(messageDigest.digest(combinedStrings.getBytes()));
}
public boolean checkHash(User user, String hashValue, int taskLevel)
public Pair<Task, Flag> checkHash(User user, String hashValue, int taskLevel)
{
Pair<Task, Flag> taskFlagPair = tasksRepository.getTaskFlagPairByHashValue(user, hashValue, taskLevel);
return solutionsRepository.submitSolution(user, taskFlagPair.getKey(), taskFlagPair.getValue(), hashValue);
return tasksRepository.getTaskFlagPairByHashValue(user, hashValue, taskLevel);
//todo: what if null?
}
}
......@@ -72,10 +72,8 @@ public class SolutionsRepository extends Repository<Solution>
.collect(Collectors.toList());
}
public boolean submitSolution(User user, Task task, Flag flag, String hashValue)
public boolean submitSolution(Solution solution)
{
Team team = teamsRepository.getTeamByUser(user);
Solution solution = new Solution(team, task, flag, hashValue);
if (!isAlreadySubmittedSolution(solution)) {
add(solution);
return true;
......
package com.telephoners.krakyournet.ctf.resources;
import com.telephoners.krakyournet.ctf.beans.Flag;
import com.telephoners.krakyournet.ctf.beans.Solution;
import com.telephoners.krakyournet.ctf.beans.Team;
import com.telephoners.krakyournet.ctf.beans.User;
import com.telephoners.krakyournet.ctf.beans.tasks.Task;
import com.telephoners.krakyournet.ctf.core.HashValidator;
import com.telephoners.krakyournet.ctf.repositories.SolutionsRepository;
import com.telephoners.krakyournet.ctf.repositories.TasksRepository;
import com.telephoners.krakyournet.ctf.repositories.TeamsRepository;
import io.dropwizard.auth.Auth;
import javafx.util.Pair;
import javax.inject.Inject;
import javax.inject.Singleton;
......@@ -15,7 +18,6 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
@Singleton
......@@ -42,9 +44,12 @@ public class SolutionsResource
@PathParam("task_level") int taskLevel,
String hash) throws Exception
{
if (hashValidator.checkHash(user, hash, taskLevel)) {
Pair<Task, Flag> taskFlagPair = hashValidator.checkHash(user, hash, taskLevel);
if (taskFlagPair != null) {
if(solutionsRepository.submitSolution(new Solution(teamsRepository.getTeamByUser(user), taskFlagPair.getKey(), taskFlagPair.getValue(), hash))) {
return Response.ok().build();
}
}
return Response.status(Response.Status.NOT_ACCEPTABLE).build();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment