Commit b7a9072c authored by Grzegorz Pietrusza's avatar Grzegorz Pietrusza

add flag finding by hash

parent b9e30363
...@@ -5,6 +5,7 @@ server: ...@@ -5,6 +5,7 @@ server:
port: 8080 port: 8080
dbHost: 46.4.242.141 dbHost: 46.4.242.141
#dbHost: 127.0.0.1
dbPort: 27017 dbPort: 27017
dbName: db dbName: db
...@@ -66,7 +67,7 @@ teams: ...@@ -66,7 +67,7 @@ teams:
textTasks: textTasks:
- name: "Szyfro1" - name: "Szyfro1"
text: "Odszyfruj1" text: "Odszyfruj1"
level: 2 level: 1
flags: flags:
- value: "sdfg1f" - value: "sdfg1f"
description: "flaga 1" description: "flaga 1"
...@@ -100,9 +101,9 @@ webTasks: ...@@ -100,9 +101,9 @@ webTasks:
url: "http://wikipedia.pl" url: "http://wikipedia.pl"
level: 4 level: 4
flags: flags:
- value: "sdfg1fs" - value: "sdfffg1fs"
description: "flaga 11" description: "flaga 11"
- value: "sdfdg1f" - value: "sffdfdg1f"
description: "flaga 22" description: "flaga 22"
- value: "sfdfgf1" - value: "sdfassfdfgf1"
description: "flaga 33" description: "flaga 33"
...@@ -9,9 +9,6 @@ import org.mongodb.morphia.annotations.Reference; ...@@ -9,9 +9,6 @@ import org.mongodb.morphia.annotations.Reference;
import java.util.List; import java.util.List;
/**
* Created by gpietrus on 20.02.2016.
*/
@Entity("teams") @Entity("teams")
public class Team { public class Team {
@Id @Id
...@@ -53,4 +50,29 @@ public class Team { ...@@ -53,4 +50,29 @@ public class Team {
public List<User> getMembers() { public List<User> getMembers() {
return members; return members;
} }
@Override
public boolean equals(Object o)
{
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Team team = (Team) o;
if (id != null ? !id.equals(team.id) : team.id != null) return false;
if (name != null ? !name.equals(team.name) : team.name != null) return false;
if (description != null ? !description.equals(team.description) : team.description != null) return false;
return members != null ? members.equals(team.members) : team.members == null;
}
@Override
public int hashCode()
{
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (description != null ? description.hashCode() : 0);
result = 31 * result + (members != null ? members.hashCode() : 0);
return result;
}
} }
...@@ -15,6 +15,7 @@ import java.util.List; ...@@ -15,6 +15,7 @@ import java.util.List;
public abstract class Task public abstract class Task
{ {
@Id @Id
//todo: use id to identify task?
protected ObjectId id; protected ObjectId id;
@PublicProperty @PublicProperty
protected String name; protected String name;
......
...@@ -7,6 +7,8 @@ import org.mongodb.morphia.Datastore; ...@@ -7,6 +7,8 @@ import org.mongodb.morphia.Datastore;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import java.util.List; import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
@Singleton @Singleton
public class SolutionsRepository implements Repository public class SolutionsRepository implements Repository
...@@ -33,6 +35,23 @@ public class SolutionsRepository implements Repository ...@@ -33,6 +35,23 @@ public class SolutionsRepository implements Repository
public List<Solution> getByTeam(Team team) public List<Solution> getByTeam(Team team)
{ {
//todo: merge with upper //todo: merge with upper
return datastore.createQuery(Solution.class).filter("team", team).asList(); //todo: use morphia filter
// List<Solution> solutions = datastore.createQuery(Solution.class).asList();
List<Solution> solutions = datastore.createQuery(Solution.class)
// .filter("team", team)
.asList();
List<Solution> collect = solutions.stream()
.filter(new Predicate<Solution>()
{
@Override
public boolean test(Solution solution)
{
return solution.getTeam().equals(team);
}
})
.collect(Collectors.toList());
return collect;
// return datastore.createQuery(Solution.class).filter("team", team).asList();
//todo: group by task
} }
} }
\ No newline at end of file
...@@ -7,6 +7,7 @@ import com.telephoners.krakyournet.ctf.objects.Solution; ...@@ -7,6 +7,7 @@ import com.telephoners.krakyournet.ctf.objects.Solution;
import com.telephoners.krakyournet.ctf.objects.Team; import com.telephoners.krakyournet.ctf.objects.Team;
import com.telephoners.krakyournet.ctf.objects.User; import com.telephoners.krakyournet.ctf.objects.User;
import com.telephoners.krakyournet.ctf.objects.tasks.Task; import com.telephoners.krakyournet.ctf.objects.tasks.Task;
import javafx.util.Pair;
import org.apache.commons.codec.binary.Hex; import org.apache.commons.codec.binary.Hex;
import org.mongodb.morphia.Datastore; import org.mongodb.morphia.Datastore;
...@@ -80,49 +81,61 @@ public class TasksRepository implements Repository ...@@ -80,49 +81,61 @@ public class TasksRepository implements Repository
{ {
return this.getAll().stream() return this.getAll().stream()
.collect(Collectors.toMap( .collect(Collectors.toMap(
task -> task.getFlags().stream() task -> {
.map(flag -> calculateHashValue(username, flag.getValue())) List<String> collect = task.getFlags().stream()
.collect(Collectors.toList()), .map(flag -> calculateHashValue(username, flag.getValue()))
.collect(Collectors.toList());
return collect;
},
Task::getLevel Task::getLevel
)); ));
} }
private Optional<Task> getTaskFlagByHashValue(User user, String userHash) //todo: refactor with the function below
private Optional<Pair<Task, Flag>> getTaskFlagPairByHashValue(User user, String userHash)
{ {
String username = user.getName(); String username = user.getName();
//todo: inline
//todo: collapse lambdas //todo: collapse lambdas
List<Task> tasks = this.getAll(); Optional<Pair<Task, Flag>> matched = this.getAll().stream()
Optional<Task> task = tasks.stream() .collect(Collectors.toMap(
.filter(new Predicate<Task>() task -> task,
Task::getFlags
))
.entrySet()
.stream()
.map((Function<Map.Entry<Task, List<Flag>>, Pair<Task, Optional<Flag>>>) taskFlagsEntry -> {
Task task = taskFlagsEntry.getKey();
Optional<Flag> matchedFlag = taskFlagsEntry.getValue().stream()
.filter(new Predicate<Flag>()
{
@Override
public boolean test(Flag flag1)
{
return calculateHashValue(username, flag1.getValue()).equals(userHash);
}
})
.findFirst();
return new Pair<Task, Optional<Flag>>(task, matchedFlag);
})
.filter(new Predicate<Pair<Task, Optional<Flag>>>()
{ {
@Override @Override
public boolean test(Task task) public boolean test(Pair<Task, Optional<Flag>> taskOptionalPair)
{ {
return task.getFlags() return taskOptionalPair.getValue().isPresent();
.stream() }
.map(new Function<Flag, String>() })
{ .map(new Function<Pair<Task, Optional<Flag>>, Pair<Task, Flag>>()
@Override {
public String apply(Flag flag) @Override
{ public Pair<Task, Flag> apply(Pair<Task, Optional<Flag>> taskOptionalPair)
return calculateHashValue(username, flag.getValue()); {
} return new Pair<Task, Flag>(taskOptionalPair.getKey(), taskOptionalPair.getValue().get());
})
.filter(new Predicate<String>()
{
@Override
public boolean test(String flagValue)
{
return flagValue.equals(userHash);
}
})
.findFirst()
.isPresent();
} }
}) })
.findFirst(); .findFirst();
return task; return matched;
//todo: refactor
} }
public String calculateHashValue(String username, String flagValue) public String calculateHashValue(String username, String flagValue)
...@@ -138,14 +151,20 @@ public class TasksRepository implements Repository ...@@ -138,14 +151,20 @@ public class TasksRepository implements Repository
return encodedHash; return encodedHash;
} }
public boolean checkFlag(User user, String hashValue) public boolean checkHash(User user, String hashValue)
{ {
//todo: refactor
Optional<Pair<Task, Flag>> taskFlagPairOptional = getTaskFlagPairByHashValue(user, hashValue);
if (!taskFlagPairOptional.isPresent()) {
return false;
}
Optional<Task> task = getTaskFlagByHashValue(user, hashValue); Pair<Task, Flag> taskFlagPair = taskFlagPairOptional.get();
// Optional<Task> task = getByUserFlag(username, hashValue); Task task = taskFlagPair.getKey();
Flag flag = taskFlagPair.getValue();
Optional<Team> team = teamsRepository.getTeamByUser(user); Optional<Team> team = teamsRepository.getTeamByUser(user);
if (task.isPresent() && team.isPresent()) { if (team.isPresent()) {
solutionsRepository.add(new Solution(team.get(), task.get(), null)); //todo solutionsRepository.add(new Solution(team.get(), task, flag)); //todo
return true; return true;
} }
return false; return false;
......
...@@ -42,9 +42,9 @@ public class SolutionsResource ...@@ -42,9 +42,9 @@ public class SolutionsResource
@POST @POST
public Response submitSolution(@Auth User user, public Response submitSolution(@Auth User user,
String flag) throws Exception String hash) throws Exception
{ {
if (tasksRepository.checkFlag(user, flag)) { if (tasksRepository.checkHash(user, hash)) {
return Response.ok().build(); return Response.ok().build();
} }
return Response.status(Response.Status.NOT_ACCEPTABLE).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