Commit b10069ee authored by Grzegorz Pietrusza's avatar Grzegorz Pietrusza

more mocks

parent a91f65e8
...@@ -2,17 +2,17 @@ package com.telephoners.krakyournet.ctf.auth; ...@@ -2,17 +2,17 @@ package com.telephoners.krakyournet.ctf.auth;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.telephoners.krakyournet.ctf.beans.User; import com.telephoners.krakyournet.ctf.beans.User;
import com.telephoners.krakyournet.ctf.repositories.UsersRepository; import com.telephoners.krakyournet.ctf.repositories.Repository;
import io.dropwizard.auth.AuthenticationException; import io.dropwizard.auth.AuthenticationException;
import io.dropwizard.auth.Authenticator; import io.dropwizard.auth.Authenticator;
import io.dropwizard.auth.basic.BasicCredentials; import io.dropwizard.auth.basic.BasicCredentials;
public class UserAuthenticator implements Authenticator<BasicCredentials, User> public class UserAuthenticator implements Authenticator<BasicCredentials, User>
{ {
private UsersRepository usersRepository; private Repository<User> usersRepository;
@Inject @Inject
public UserAuthenticator(UsersRepository usersRepository) public UserAuthenticator(Repository<User> usersRepository)
{ {
this.usersRepository = usersRepository; this.usersRepository = usersRepository;
} }
......
package com.telephoners.krakyournet.ctf.beans; package com.telephoners.krakyournet.ctf.beans;
import com.fasterxml.jackson.annotation.JsonProperty; import org.ektorp.docref.DocumentReferences;
import org.ektorp.support.CouchDbDocument; import org.ektorp.support.CouchDbDocument;
import java.security.Principal; import java.security.Principal;
...@@ -14,28 +14,19 @@ public class User extends CouchDbDocument implements Principal ...@@ -14,28 +14,19 @@ public class User extends CouchDbDocument implements Principal
private String email; private String email;
private boolean admin = false; private boolean admin = false;
public User(String name, String password, String email, boolean admin) @DocumentReferences //todo: make sure it works
private Team team;
public User(String name, String password, String email, boolean admin, Team team)
{ {
this.name = name; this.name = name;
this.password = password; this.password = password;
this.email = email; this.email = email;
this.admin = admin; this.admin = admin;
this.team = team;
} }
public void setAdmin(boolean admin) @Override
{
this.admin = admin;
}
public boolean isAdmin()
{
return admin;
}
public User()
{
}
public String getName() public String getName()
{ {
return name; return name;
...@@ -65,4 +56,24 @@ public class User extends CouchDbDocument implements Principal ...@@ -65,4 +56,24 @@ public class User extends CouchDbDocument implements Principal
{ {
this.email = email; this.email = email;
} }
public boolean isAdmin()
{
return admin;
}
public void setAdmin(boolean admin)
{
this.admin = admin;
}
public Team getTeam()
{
return team;
}
public void setTeam(Team team)
{
this.team = team;
}
} }
...@@ -2,11 +2,10 @@ package com.telephoners.krakyournet.ctf.commands; ...@@ -2,11 +2,10 @@ package com.telephoners.krakyournet.ctf.commands;
import com.google.inject.Guice; import com.google.inject.Guice;
import com.google.inject.Injector; import com.google.inject.Injector;
import com.telephoners.krakyournet.ctf.core.ApplicationConfiguration;
import com.telephoners.krakyournet.ctf.modules.ApplicationModule;
import com.telephoners.krakyournet.ctf.beans.tasks.TextTask; import com.telephoners.krakyournet.ctf.beans.tasks.TextTask;
import com.telephoners.krakyournet.ctf.beans.tasks.WebTask; import com.telephoners.krakyournet.ctf.beans.tasks.WebTask;
import com.telephoners.krakyournet.ctf.repositories.TasksRepository; import com.telephoners.krakyournet.ctf.core.ApplicationConfiguration;
import com.telephoners.krakyournet.ctf.modules.ApplicationModule;
import io.dropwizard.cli.ConfiguredCommand; import io.dropwizard.cli.ConfiguredCommand;
import io.dropwizard.setup.Bootstrap; import io.dropwizard.setup.Bootstrap;
import net.sourceforge.argparse4j.inf.Namespace; import net.sourceforge.argparse4j.inf.Namespace;
...@@ -25,15 +24,15 @@ public class RegisterTasksCommand extends ConfiguredCommand<ApplicationConfigura ...@@ -25,15 +24,15 @@ public class RegisterTasksCommand extends ConfiguredCommand<ApplicationConfigura
private void initializeTasks(ApplicationConfiguration applicationConfiguration, Injector injector) private void initializeTasks(ApplicationConfiguration applicationConfiguration, Injector injector)
{ {
TasksRepository tasksRepository = injector.getInstance(TasksRepository.class); // Repository<Task> tasksRepository = injector.getInstance(TasksRepository.class);
List<TextTask> cryptoTasks = applicationConfiguration.getTextTasks(); List<TextTask> cryptoTasks = applicationConfiguration.getTextTasks();
List<WebTask> webTasks = applicationConfiguration.getWebTasks(); List<WebTask> webTasks = applicationConfiguration.getWebTasks();
// tasksRepository.clean(); // tasksRepository.clean();
cryptoTasks.forEach(tasksRepository::add); // cryptoTasks.forEach(tasksRepository::add);
webTasks.forEach(tasksRepository::add); // webTasks.forEach(tasksRepository::add);
} }
@Override @Override
......
...@@ -4,8 +4,6 @@ import com.google.inject.Guice; ...@@ -4,8 +4,6 @@ import com.google.inject.Guice;
import com.google.inject.Injector; import com.google.inject.Injector;
import com.telephoners.krakyournet.ctf.core.ApplicationConfiguration; import com.telephoners.krakyournet.ctf.core.ApplicationConfiguration;
import com.telephoners.krakyournet.ctf.modules.ApplicationModule; import com.telephoners.krakyournet.ctf.modules.ApplicationModule;
import com.telephoners.krakyournet.ctf.repositories.TeamsRepository;
import com.telephoners.krakyournet.ctf.repositories.UsersRepository;
import io.dropwizard.cli.ConfiguredCommand; import io.dropwizard.cli.ConfiguredCommand;
import io.dropwizard.setup.Bootstrap; import io.dropwizard.setup.Bootstrap;
import net.sourceforge.argparse4j.inf.Namespace; import net.sourceforge.argparse4j.inf.Namespace;
...@@ -22,14 +20,14 @@ public class RegisterTeamsCommand extends ConfiguredCommand<ApplicationConfigura ...@@ -22,14 +20,14 @@ public class RegisterTeamsCommand extends ConfiguredCommand<ApplicationConfigura
private void initializeTasks(ApplicationConfiguration applicationConfiguration, Injector injector) private void initializeTasks(ApplicationConfiguration applicationConfiguration, Injector injector)
{ {
TeamsRepository teamsRepository = injector.getInstance(TeamsRepository.class); // Repository<Team> teamsRepository = injector.getInstance(.class);
UsersRepository usersRepository = injector.getInstance(UsersRepository.class); // Repository<User> usersRepository = injector.getInstance(UsersRepository.class);
//
// teamsRepository.clean(); // teamsRepository.clean();
applicationConfiguration.getTeams().forEach(team -> { // applicationConfiguration.getTeams().forEach(team -> {
team.getMembers().stream().forEach(usersRepository::add); // team.getMembers().stream().forEach(usersRepository::add);
teamsRepository.add(team); // teamsRepository.add(team);
}); // });
} }
@Override @Override
......
package com.telephoners.krakyournet.ctf.repositories;
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.exceptions.SolutionAlreadySubmittedException;
import org.ektorp.CouchDbConnector;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class SolutionsRepository extends Repository<Solution>
{
public SolutionsRepository(Class<Solution> type, CouchDbConnector connector)
{
super(type, connector);
}
public Map<Integer, List<Flag>> getTeamSolutions(Team team)
{
return null;
/*
//todo: do not identify team by name? conflict in team ids?
return datastore.createQuery(Solution.class)
.filter("team.name", team.getName()).asList()
.stream()
.collect(Collectors.groupingBy(new Function<Solution, Task>()
{
@Override
public Task apply(Solution solution1)
{
return solution1.getTask();
}
}))
.entrySet()
.stream()
.collect(Collectors.toMap(
taskSolutions -> taskSolutions.getKey().getLevel(),
taskSolutions -> taskSolutions.getValue().stream()
.map(Solution::getFlag).collect(Collectors.toList())
)); */
}
private boolean isAlreadySubmittedSolution(Solution solution)
{
return false;
/*
List<Solution> solutions = datastore.find(Solution.class)
.filter("flag.value", solution.getFlag().getValue()) //todo: do not filter by field
.filter("team.name", solution.getTeam().getName()) //todo: whole object instead
.asList();
return !solutions
.isEmpty();*/
}
public List<Integer> getCompletedTasks(Team team)
{
Map<Integer, List<Flag>> teamSolutions = getTeamSolutions(team);
return null;
// return tasksRepository.getAll().stream()
// .filter(task -> {
// List<Flag> teamTaskSolutions = teamSolutions.get(task.getLevel());
// return teamTaskSolutions != null && teamTaskSolutions.size() == task.getFlags().size();
// })
// .map(Task::getLevel)
// .collect(Collectors.toList());
}
public void submitSolution(Solution solution)
{
if (isAlreadySubmittedSolution(solution)) {
throw new SolutionAlreadySubmittedException();
}
add(solution);
}
private int calculatePointsSum(List<Flag> flags) {
return flags.stream()
.mapToInt(Flag::getPoints)
.sum();
}
public Map<Integer, Integer> getPointsSum(Team team) {
return getTeamSolutions(team).entrySet().stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
entry -> calculatePointsSum(entry.getValue())
));
}
}
\ No newline at end of file
package com.telephoners.krakyournet.ctf.repositories;
import com.sun.tools.javac.util.List;
import com.telephoners.krakyournet.ctf.beans.tasks.Task;
import org.ektorp.CouchDbConnector;
public class TasksRepository extends Repository<Task>
{
public TasksRepository(Class<Task> type, CouchDbConnector connector)
{
super(type, connector);
}
public Task getByLevel(int level)
{
return null;
/*
return datastore.createQuery(Task.class)
.filter("level", level)
.asList()
.stream()
.findFirst()
.orElseThrow(TaskNotFoundException::new);*/
}
public List<Task> getAllPublic()
{
return null;/*
return datastore.createQuery(Task.class)
.order("level")
.retrievedFields(true, DBObjectUtils.getPublicFields(Task.class))
.asList();*/
}
}
\ No newline at end of file
package com.telephoners.krakyournet.ctf.repositories;
import com.telephoners.krakyournet.ctf.beans.Team;
import com.telephoners.krakyournet.ctf.beans.User;
import org.ektorp.CouchDbConnector;
import javax.inject.Singleton;
public class TeamsRepository extends Repository<Team>
{
public TeamsRepository(Class<Team> type, CouchDbConnector connector)
{
super(type, connector);
}
public Team getTeamByUser(User user)
{
return null;
/*
return datastore.createQuery(Team.class).asList().stream()
.filter(team -> team.getMembers().contains(user))
.findFirst()
.orElseThrow(TeamNotFoundException::new);*/
}
public Team getTeamByUserName(String username)
{
return null;
// return getTeamByUser(usersRepository.getUserByName(username));
}
}
package com.telephoners.krakyournet.ctf.repositories;
import com.telephoners.krakyournet.ctf.beans.User;
import io.dropwizard.auth.basic.BasicCredentials;
import org.ektorp.CouchDbConnector;
public class UsersRepository extends Repository<User>
{
public UsersRepository(Class<User> type, CouchDbConnector connector)
{
super(type, connector);
}
public User getUserByName(String username)
{
return null;
/*
return datastore.createQuery(User.class)
.field("name").equal(username)
.asList()
.stream()
.findFirst()
.orElseThrow(UserNotFoundException::new);*/
}
public User authenticateUser(BasicCredentials basicCredentials)
{
return null;
/*
return datastore.createQuery(User.class)
.field("name").equal(basicCredentials.getUsername())
.field("password").equal(Hex.encodeHexString(messageDigestProvider.getMessageDigest().digest(basicCredentials.getPassword().getBytes())))
.get();*/
}
}
...@@ -5,33 +5,34 @@ import com.telephoners.krakyournet.ctf.beans.Solution; ...@@ -5,33 +5,34 @@ import com.telephoners.krakyournet.ctf.beans.Solution;
import com.telephoners.krakyournet.ctf.beans.Team; import com.telephoners.krakyournet.ctf.beans.Team;
import com.telephoners.krakyournet.ctf.beans.User; import com.telephoners.krakyournet.ctf.beans.User;
import com.telephoners.krakyournet.ctf.beans.tasks.Task; import com.telephoners.krakyournet.ctf.beans.tasks.Task;
import com.telephoners.krakyournet.ctf.exceptions.InvalidSolutionException; import com.telephoners.krakyournet.ctf.repositories.Repository;
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 io.dropwizard.auth.Auth;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import javax.ws.rs.*; import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
@Singleton @Singleton
@Path(value = "/solutions") @Path(value = "/solutions")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public class SolutionsResource public class SolutionsResource
{ {
private final TeamsRepository teamsRepository; private final Repository<Team> teamsRepository;
private final SolutionsRepository solutionsRepository; private final Repository<Solution> solutionsRepository;
private final TasksRepository tasksRepository; private final Repository<Task> tasksRepository;
@Inject @Inject
public SolutionsResource(SolutionsRepository solutionsRepository, TasksRepository tasksRepository, public SolutionsResource(Repository<Solution> solutionsRepository,
TeamsRepository teamsRepository) Repository<Task> tasksRepository,
Repository<Team> teamsRepository)
{ {
this.solutionsRepository = solutionsRepository; this.solutionsRepository = solutionsRepository;
this.teamsRepository = teamsRepository; this.teamsRepository = teamsRepository;
...@@ -44,6 +45,9 @@ public class SolutionsResource ...@@ -44,6 +45,9 @@ public class SolutionsResource
@PathParam("task_level") int taskLevel, @PathParam("task_level") int taskLevel,
String userFlag) throws Exception String userFlag) throws Exception
{ {
return null;
//todo: this should be by id
/*
Task task = tasksRepository.getByLevel(taskLevel); Task task = tasksRepository.getByLevel(taskLevel);
Flag flag = task.getFlags() Flag flag = task.getFlags()
.stream() .stream()
...@@ -52,44 +56,52 @@ public class SolutionsResource ...@@ -52,44 +56,52 @@ public class SolutionsResource
.orElseThrow(InvalidSolutionException::new); .orElseThrow(InvalidSolutionException::new);
solutionsRepository.submitSolution(new Solution(teamsRepository.getTeamByUser(user), task, flag)); solutionsRepository.submitSolution(new Solution(teamsRepository.getTeamByUser(user), task, flag));
return Response.ok().entity(flag).build(); return Response.ok().entity(flag).build();*/
} }
@GET @GET
@Path("/my/completed") @Path("/my/completed")
public List<Integer> getTeamCompletedTasks(@Auth User user) public List<Integer> getTeamCompletedTasks(@Auth User user)
{ {
return solutionsRepository.getCompletedTasks(teamsRepository.getTeamByUser(user)); return null;
/*
return solutionsRepository.getCompletedTasks(teamsRepository.getTeamByUser(user));*/
} }
@GET @GET
@Path("/all/completed") @Path("/all/completed")
public Map<String, List<Integer>> getAllTeamsCompletedTasks(@Auth User user) public Map<String, List<Integer>> getAllTeamsCompletedTasks(@Auth User user)
{ {
return null;
/*
return teamsRepository.getAll() return teamsRepository.getAll()
.stream() .stream()
.collect(Collectors.toMap( .collect(Collectors.toMap(
Team::getName, Team::getName,
solutionsRepository::getCompletedTasks solutionsRepository::getCompletedTasks
)); ));*/
} }
@GET @GET
@Path("/my") @Path("/my")
public Map<Integer, List<Flag>> getTeamSummittedFlags(@Auth User user) { public Map<Integer, List<Flag>> getTeamSummittedFlags(@Auth User user) {
return null;
/*
Map<Integer, List<Flag>> teamSolutions = solutionsRepository.getTeamSolutions(teamsRepository.getTeamByUser(user)); Map<Integer, List<Flag>> teamSolutions = solutionsRepository.getTeamSolutions(teamsRepository.getTeamByUser(user));
return teamSolutions; return teamSolutions;*/
} }
@GET @GET
@Path("/all") @Path("/all")
public Map<String, Map<Integer, Integer>> getTeamsSolutions() public Map<String, Map<Integer, Integer>> getTeamsSolutions()
{ {
return null;
/*
return teamsRepository.getAll() return teamsRepository.getAll()
.stream() .stream()
.collect(Collectors.toMap( .collect(Collectors.toMap(
Team::getName, Team::getName,
solutionsRepository::getPointsSum solutionsRepository::getPointsSum
)); ));*/
} }
} }
...@@ -2,7 +2,7 @@ package com.telephoners.krakyournet.ctf.resources; ...@@ -2,7 +2,7 @@ package com.telephoners.krakyournet.ctf.resources;
import com.telephoners.krakyournet.ctf.beans.User; import com.telephoners.krakyournet.ctf.beans.User;
import com.telephoners.krakyournet.ctf.beans.tasks.Task; import com.telephoners.krakyournet.ctf.beans.tasks.Task;
import com.telephoners.krakyournet.ctf.repositories.TasksRepository; import com.telephoners.krakyournet.ctf.repositories.Repository;
import io.dropwizard.auth.Auth; import io.dropwizard.auth.Auth;
import javax.inject.Inject; import javax.inject.Inject;
...@@ -20,10 +20,10 @@ import java.net.URISyntaxException; ...@@ -20,10 +20,10 @@ import java.net.URISyntaxException;
@Path(value = "/task") @Path(value = "/task")
public class TaskResource public class TaskResource
{ {
private final TasksRepository tasksRepository; private final Repository<Task> tasksRepository;
@Inject @Inject
public TaskResource(TasksRepository tasksRepository) public TaskResource(Repository<Task> tasksRepository)
{ {
this.tasksRepository = tasksRepository; this.tasksRepository = tasksRepository;
} }
...@@ -36,7 +36,8 @@ public class TaskResource ...@@ -36,7 +36,8 @@ public class TaskResource
final @PathParam("path") String path, final @PathParam("path") String path,
@Context ContainerRequestContext containerRequestContext) throws IOException, URISyntaxException @Context ContainerRequestContext containerRequestContext) throws IOException, URISyntaxException
{ {
Task task = tasksRepository.getByLevel(taskLevel); return null;
return task.getTaskResponse(); // Task task = tasksRepository.getByLevel(taskLevel);
// return task.getTaskResponse();
} }
} }
package com.telephoners.krakyournet.ctf.resources; package com.telephoners.krakyournet.ctf.resources;
import com.telephoners.krakyournet.ctf.beans.tasks.Task; import com.telephoners.krakyournet.ctf.beans.tasks.Task;
import com.telephoners.krakyournet.ctf.repositories.TasksRepository; import com.telephoners.krakyournet.ctf.repositories.Repository;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
...@@ -16,10 +16,10 @@ import java.util.List; ...@@ -16,10 +16,10 @@ import java.util.List;
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public class TasksResource public class TasksResource
{ {
private final TasksRepository tasksRepository; private final Repository<Task> tasksRepository;
@Inject @Inject
public TasksResource(TasksRepository tasksRepository) public TasksResource(Repository<Task> tasksRepository)
{ {
this.tasksRepository = tasksRepository; this.tasksRepository = tasksRepository;
} }
...@@ -27,7 +27,9 @@ public class TasksResource ...@@ -27,7 +27,9 @@ public class TasksResource
@GET @GET
public List<Task> getTasksPublic() public List<Task> getTasksPublic()
{ {
return null;
/*
List<Task> allPublic = tasksRepository.getAllPublic(); List<Task> allPublic = tasksRepository.getAllPublic();
return allPublic; return allPublic;*/
} }
} }
...@@ -4,7 +4,7 @@ import com.google.common.collect.ImmutableMap; ...@@ -4,7 +4,7 @@ import com.google.common.collect.ImmutableMap;
import com.telephoners.krakyournet.ctf.beans.Team; import com.telephoners.krakyournet.ctf.beans.Team;
import com.telephoners.krakyournet.ctf.beans.User; import com.telephoners.krakyournet.ctf.beans.User;
import com.telephoners.krakyournet.ctf.core.ApplicationConfiguration; import com.telephoners.krakyournet.ctf.core.ApplicationConfiguration;
import com.telephoners.krakyournet.ctf.repositories.TeamsRepository; import com.telephoners.krakyournet.ctf.repositories.Repository;
import io.dropwizard.auth.Auth; import io.dropwizard.auth.Auth;
import javax.inject.Inject; import javax.inject.Inject;
...@@ -23,11 +23,11 @@ import java.io.IOException; ...@@ -23,11 +23,11 @@ import java.io.IOException;
public class UtilResource public class UtilResource
{ {
private static String redirectHtml = "<!DOCTYPE html><html lang=\"en\" ng-app=\"ctfApp\"><head></head><script type=\"text/javascript\">window.location = \"http://\" + window.location.host;</script></html>"; private static String redirectHtml = "<!DOCTYPE html><html lang=\"en\" ng-app=\"ctfApp\"><head></head><script type=\"text/javascript\">window.location = \"http://\" + window.location.host;</script></html>";
private final TeamsRepository teamsRepository; private final Repository<Team> teamsRepository;
private final ApplicationConfiguration applicationConfiguration; private final ApplicationConfiguration applicationConfiguration;
@Inject @Inject
public UtilResource(TeamsRepository teamsRepository, public UtilResource(Repository<Team> teamsRepository,
ApplicationConfiguration applicationConfiguration) ApplicationConfiguration applicationConfiguration)
{ {
this.teamsRepository = teamsRepository; this.teamsRepository = teamsRepository;
...@@ -49,8 +49,9 @@ public class UtilResource ...@@ -49,8 +49,9 @@ public class UtilResource
String userName = user.getName(); String userName = user.getName();
responseBuilder.put("userName", userName); responseBuilder.put("userName", userName);
if (!user.isAdmin()) { if (!user.isAdmin()) {
Team team = teamsRepository.getTeamByUserName(userName); //todo: fix
responseBuilder.put("teamName", team.getName()); // Team team = teamsRepository.getTeamByUserName(userName);
// responseBuilder.put("teamName", team.getName());
} else { } else {
responseBuilder.put("isAdmin", true); responseBuilder.put("isAdmin", true);
} }
......
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