Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
C
CTF
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Grzegorz
CTF
Commits
d07c78a5
Commit
d07c78a5
authored
Mar 22, 2016
by
Grzegorz Pietrusza
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add hashValidator - but circular dependency
parent
86a7adf5
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
96 additions
and
43 deletions
+96
-43
HashValidator.java
...a/com/telephoners/krakyournet/ctf/core/HashValidator.java
+49
-0
SolutionsRepository.java
...ers/krakyournet/ctf/repositories/SolutionsRepository.java
+33
-1
TasksRepository.java
...phoners/krakyournet/ctf/repositories/TasksRepository.java
+7
-38
SolutionsResource.java
...ephoners/krakyournet/ctf/resources/SolutionsResource.java
+7
-4
No files found.
service/src/main/java/com/telephoners/krakyournet/ctf/core/HashValidator.java
0 → 100644
View file @
d07c78a5
package
com
.
telephoners
.
krakyournet
.
ctf
.
core
;
import
com.google.inject.Inject
;
import
com.google.inject.Singleton
;
import
com.telephoners.krakyournet.ctf.beans.Flag
;
import
com.telephoners.krakyournet.ctf.beans.User
;
import
com.telephoners.krakyournet.ctf.beans.tasks.Task
;
import
com.telephoners.krakyournet.ctf.repositories.SolutionsRepository
;
import
com.telephoners.krakyournet.ctf.repositories.TasksRepository
;
import
javafx.util.Pair
;
import
org.apache.commons.codec.binary.Hex
;
import
javax.inject.Named
;
import
java.security.MessageDigest
;
@Singleton
public
class
HashValidator
{
private
ApplicationConfiguration
applicationConfiguration
;
private
MessageDigest
messageDigest
;
//todo: use messageDigestProvider
private
SolutionsRepository
solutionsRepository
;
private
TasksRepository
tasksRepository
;
@Inject
public
HashValidator
(
ApplicationConfiguration
applicationConfiguration
,
SolutionsRepository
solutionsRepository
,
TasksRepository
tasksRepository
,
final
@Named
(
"messageDigest"
)
MessageDigest
messageDigest
)
{
this
.
applicationConfiguration
=
applicationConfiguration
;
this
.
solutionsRepository
=
solutionsRepository
;
this
.
tasksRepository
=
tasksRepository
;
this
.
messageDigest
=
messageDigest
;
}
public
String
calculateHashValue
(
User
user
,
String
flagValue
)
{
String
combinedStrings
=
applicationConfiguration
.
getSalt
()
+
user
.
getName
()
+
flagValue
;
return
Hex
.
encodeHexString
(
messageDigest
.
digest
(
combinedStrings
.
getBytes
()));
}
public
boolean
checkHash
(
User
user
,
String
hashValue
,
int
taskLevel
)
{
Pair
<
Task
,
Flag
>
taskFlagPair
=
tasksRepository
.
getTaskFlagPairByHashValue
(
user
,
hashValue
,
taskLevel
);
//todo: move to solutionsRepository
return
solutionsRepository
.
submitSolution
(
user
,
taskFlagPair
.
getKey
(),
taskFlagPair
.
getValue
(),
hashValue
);
}
}
service/src/main/java/com/telephoners/krakyournet/ctf/repositories/SolutionsRepository.java
View file @
d07c78a5
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.beans.User
;
import
com.telephoners.krakyournet.ctf.beans.tasks.Task
;
import
org.mongodb.morphia.Datastore
;
...
...
@@ -15,10 +17,17 @@ import java.util.stream.Collectors;
@Singleton
public
class
SolutionsRepository
extends
Repository
<
Solution
>
{
private
TeamsRepository
teamsRepository
;
private
TasksRepository
tasksRepository
;
@Inject
public
SolutionsRepository
(
Datastore
datastore
)
public
SolutionsRepository
(
TeamsRepository
teamsRepository
,
TasksRepository
tasksRepository
,
Datastore
datastore
)
{
super
(
datastore
);
this
.
tasksRepository
=
tasksRepository
;
this
.
teamsRepository
=
teamsRepository
;
}
public
Map
<
Integer
,
List
<
String
>>
getTeamSolutions
(
Team
team
)
...
...
@@ -50,4 +59,27 @@ public class SolutionsRepository extends Repository<Solution>
.
filter
(
"flag.value"
,
solution
.
getFlag
().
getValue
())
.
get
()
!=
null
;
}
public
List
<
Integer
>
getCompletedTasks
(
Team
team
)
{
Map
<
Integer
,
List
<
String
>>
teamSolutions
=
getTeamSolutions
(
team
);
return
tasksRepository
.
getAll
().
stream
()
.
filter
(
task
->
{
List
<
String
>
teamTaskSolutions
=
teamSolutions
.
get
(
task
.
getLevel
());
return
teamTaskSolutions
!=
null
&&
teamTaskSolutions
.
size
()
==
task
.
getFlags
().
size
();
})
.
map
(
Task:
:
getLevel
)
.
collect
(
Collectors
.
toList
());
}
public
boolean
submitSolution
(
User
user
,
Task
task
,
Flag
flag
,
String
hashValue
)
{
Team
team
=
teamsRepository
.
getTeamByUser
(
user
);
Solution
solution
=
new
Solution
(
team
,
task
,
flag
,
hashValue
);
if
(!
isAlreadySubmittedSolution
(
solution
))
{
add
(
solution
);
return
true
;
}
return
false
;
}
}
\ No newline at end of file
service/src/main/java/com/telephoners/krakyournet/ctf/repositories/TasksRepository.java
View file @
d07c78a5
...
...
@@ -2,14 +2,12 @@ package com.telephoners.krakyournet.ctf.repositories;
import
com.google.inject.name.Named
;
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.ApplicationConfiguration
;
import
com.telephoners.krakyournet.ctf.core.HashValidator
;
import
com.telephoners.krakyournet.ctf.helpers.DBObjectUtils
;
import
javafx.util.Pair
;
import
org.apache.commons.codec.binary.Hex
;
import
org.mongodb.morphia.Datastore
;
import
javax.inject.Inject
;
...
...
@@ -27,12 +25,14 @@ public class TasksRepository extends Repository<Task>
private
TeamsRepository
teamsRepository
;
private
SolutionsRepository
solutionsRepository
;
private
UsersRepository
usersRepository
;
private
HashValidator
hashValidator
;
private
MessageDigest
messageDigest
;
@Inject
public
TasksRepository
(
ApplicationConfiguration
applicationConfiguration
,
Datastore
datastore
,
TeamsRepository
teamsRepository
,
SolutionsRepository
solutionsRepository
,
UsersRepository
usersRepository
,
HashValidator
hashValidator
,
final
@Named
(
"messageDigest"
)
MessageDigest
messageDigest
)
{
super
(
datastore
);
...
...
@@ -41,6 +41,7 @@ public class TasksRepository extends Repository<Task>
this
.
teamsRepository
=
teamsRepository
;
this
.
solutionsRepository
=
solutionsRepository
;
this
.
usersRepository
=
usersRepository
;
this
.
hashValidator
=
hashValidator
;
this
.
messageDigest
=
messageDigest
;
}
...
...
@@ -64,50 +65,18 @@ public class TasksRepository extends Repository<Task>
.
collect
(
Collectors
.
toMap
(
Task:
:
getLevel
,
task
->
task
.
getFlags
().
stream
()
.
map
(
flag
->
calculateHashValue
(
usersRepository
.
getUserByName
(
username
),
flag
.
getValue
()))
.
map
(
flag
->
hashValidator
.
calculateHashValue
(
usersRepository
.
getUserByName
(
username
),
flag
.
getValue
()))
.
collect
(
Collectors
.
toList
())
));
}
p
rivate
Pair
<
Task
,
Flag
>
getTaskFlagPairByHashValue
(
User
user
,
String
userHash
,
int
taskLevel
)
p
ublic
Pair
<
Task
,
Flag
>
getTaskFlagPairByHashValue
(
User
user
,
String
userHash
,
int
taskLevel
)
{
String
username
=
user
.
getName
();
Flag
matchedFlag
=
getByLevel
(
taskLevel
).
getFlags
().
stream
()
.
filter
(
flag
->
calculateHashValue
(
user
,
flag
.
getValue
()).
equals
(
userHash
))
.
filter
(
flag
->
hashValidator
.
calculateHashValue
(
user
,
flag
.
getValue
()).
equals
(
userHash
))
.
findFirst
()
.
get
();
return
new
Pair
<>(
getByLevel
(
taskLevel
),
matchedFlag
);
}
public
String
calculateHashValue
(
User
user
,
String
flagValue
)
{
String
combinedStrings
=
applicationConfiguration
.
getSalt
()
+
user
.
getName
()
+
flagValue
;
return
Hex
.
encodeHexString
(
messageDigest
.
digest
(
combinedStrings
.
getBytes
()));
}
public
boolean
checkHash
(
User
user
,
String
hashValue
,
int
taskLevel
)
{
Team
team
=
teamsRepository
.
getTeamByUser
(
user
);
Pair
<
Task
,
Flag
>
taskFlagPair
=
getTaskFlagPairByHashValue
(
user
,
hashValue
,
taskLevel
);
Solution
solution
=
new
Solution
(
team
,
taskFlagPair
.
getKey
(),
taskFlagPair
.
getValue
(),
hashValue
);
if
(!
solutionsRepository
.
isAlreadySubmittedSolution
(
solution
))
{
solutionsRepository
.
add
(
solution
);
return
true
;
}
return
false
;
}
//todo: should it be here?
public
List
<
Integer
>
getCompletedTasks
(
Team
team
)
{
Map
<
Integer
,
List
<
String
>>
teamSolutions
=
solutionsRepository
.
getTeamSolutions
(
team
);
return
getAll
().
stream
()
.
filter
(
task
->
{
List
<
String
>
teamTaskSolutions
=
teamSolutions
.
get
(
task
.
getLevel
());
return
teamTaskSolutions
!=
null
&&
teamTaskSolutions
.
size
()
==
task
.
getFlags
().
size
();
})
.
map
(
Task:
:
getLevel
)
.
collect
(
Collectors
.
toList
());
}
}
\ No newline at end of file
service/src/main/java/com/telephoners/krakyournet/ctf/resources/SolutionsResource.java
View file @
d07c78a5
...
...
@@ -2,6 +2,7 @@ package com.telephoners.krakyournet.ctf.resources;
import
com.telephoners.krakyournet.ctf.beans.Team
;
import
com.telephoners.krakyournet.ctf.beans.User
;
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
;
...
...
@@ -25,14 +26,16 @@ public class SolutionsResource
private
TeamsRepository
teamsRepository
;
private
SolutionsRepository
solutionsRepository
;
private
TasksRepository
tasksRepository
;
private
HashValidator
hashValidator
;
@Inject
public
SolutionsResource
(
SolutionsRepository
solutionsRepository
,
TasksRepository
tasksRepository
,
TeamsRepository
teamsRepository
)
TeamsRepository
teamsRepository
,
HashValidator
hashValidator
)
{
this
.
solutionsRepository
=
solutionsRepository
;
this
.
tasksRepository
=
tasksRepository
;
this
.
teamsRepository
=
teamsRepository
;
this
.
hashValidator
=
hashValidator
;
}
@POST
...
...
@@ -41,7 +44,7 @@ public class SolutionsResource
@PathParam
(
"task_level"
)
int
taskLevel
,
String
hash
)
throws
Exception
{
if
(
tasksRepository
.
checkHash
(
user
,
hash
,
taskLevel
))
{
if
(
hashValidator
.
checkHash
(
user
,
hash
,
taskLevel
))
{
return
Response
.
ok
().
build
();
}
return
Response
.
status
(
Response
.
Status
.
NOT_ACCEPTABLE
).
build
();
...
...
@@ -51,7 +54,7 @@ public class SolutionsResource
@Path
(
"/completed"
)
public
List
<
Integer
>
getTeamCompletedTasks
(
@Auth
User
user
)
{
return
task
sRepository
.
getCompletedTasks
(
teamsRepository
.
getTeamByUser
(
user
));
return
solution
sRepository
.
getCompletedTasks
(
teamsRepository
.
getTeamByUser
(
user
));
}
@GET
...
...
@@ -62,7 +65,7 @@ public class SolutionsResource
.
stream
()
.
collect
(
Collectors
.
toMap
(
Team:
:
getName
,
team
->
task
sRepository
.
getCompletedTasks
(
team
)
team
->
solution
sRepository
.
getCompletedTasks
(
team
)
));
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment