Skip to content

Commit 48c593e

Browse files
authored
🐞 Fix Grace period for parent-children elections (#214)
Parent issue: sequentech/meta#4095
1 parent 3430cf8 commit 48c593e

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

app/controllers/BallotboxApi.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ object BallotboxApi extends Controller with Response {
5858
}
5959
}
6060
val boothSecret = Play.current.configuration.getString("elections.auth.secret").get
61+
val voterTokenExpiry = Play.current.configuration.getString("elections.auth.expiry").get.toLong
6162

6263
/** cast a vote, performs several validations, see vote.validate */
6364
def vote(electionId: Long, voterId: String) =
@@ -87,10 +88,19 @@ object BallotboxApi extends Controller with Response {
8788
else {
8889
val configJson = Json.parse(election.configuration)
8990
val presentation = configJson.validate[ElectionConfig].get.presentation
91+
val authorizationHeader = request.headers.get("Authorization").get
92+
val tokenTimestamp = ActionHelper(authorizationHeader).getTokenTime
93+
val insideGracePeriod = (
94+
election.endDate.isDefined &&
95+
tokenTimestamp.isDefined &&
96+
election.endDate.get.getTime / 1000 + voterTokenExpiry > tokenTimestamp.get
97+
)
98+
9099
val gracefulEnd = (
91100
presentation.extra_options.isDefined &&
92101
presentation.extra_options.get.allow_voting_end_graceful_period.isDefined &&
93-
presentation.extra_options.get.allow_voting_end_graceful_period.get == true
102+
presentation.extra_options.get.allow_voting_end_graceful_period.get == true &&
103+
insideGracePeriod
94104
)
95105
if(
96106
election.state == Elections.STARTED ||

app/utils/Actions.scala

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,34 @@ import play.api._
2424
import play.api.libs.concurrent.Execution.Implicits.defaultContext
2525
import play.api.libs.{Crypto => PlayCrypto}
2626

27+
case class ActionHelper(authorizationHeader: String) {
28+
def getTokenTime(): Option[Long] = {
29+
val start = "khmac:///sha-256;";
30+
val slashPos = start.length + 64;
31+
32+
if(
33+
!authorizationHeader.startsWith(start) ||
34+
authorizationHeader.length < slashPos ||
35+
authorizationHeader.charAt(slashPos) != '/'
36+
) {
37+
Logger.warn(s"Malformed authorization header")
38+
return None
39+
}
40+
41+
val hash = authorizationHeader.substring(start.length, slashPos)
42+
val message = authorizationHeader.substring(slashPos + 1)
43+
44+
val split = message.split(':')
45+
if (split.length < 7) {
46+
Logger.warn(s"Malformed authorization header")
47+
return None
48+
}
49+
50+
val rcvTime = split(split.length - 1).toLong
51+
return Some(rcvTime)
52+
}
53+
}
54+
2755
case class HMACActionHelper(
2856
userId: String,
2957
objType: String,
@@ -223,3 +251,4 @@ object LoggingFilter extends Filter {
223251
}
224252
}
225253
}
254+

0 commit comments

Comments
 (0)