Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐞 Fix Grace period for parent-children elections #214

Open
wants to merge 3 commits into
base: 10.5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion app/controllers/BallotboxApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ object BallotboxApi extends Controller with Response {
}
}
val boothSecret = Play.current.configuration.getString("elections.auth.secret").get
val voterTokenExpiry = Play.current.configuration.getString("elections.auth.expiry").get.toLong

/** cast a vote, performs several validations, see vote.validate */
def vote(electionId: Long, voterId: String) =
Expand Down Expand Up @@ -87,10 +88,19 @@ object BallotboxApi extends Controller with Response {
else {
val configJson = Json.parse(election.configuration)
val presentation = configJson.validate[ElectionConfig].get.presentation
val authorizationHeader = request.headers.get("Authorization").get
val tokenTimestamp = ActionHelper(authorizationHeader).getTokenTime
val insideGracePeriod = (
election.endDate.isDefined &&
tokenTimestamp.isDefined &&
election.endDate.get.getTime / 1000 + voterTokenExpiry > tokenTimestamp.get
)

val gracefulEnd = (
presentation.extra_options.isDefined &&
presentation.extra_options.get.allow_voting_end_graceful_period.isDefined &&
presentation.extra_options.get.allow_voting_end_graceful_period.get == true
presentation.extra_options.get.allow_voting_end_graceful_period.get == true &&
insideGracePeriod
)
if(
election.state == Elections.STARTED ||
Expand Down
29 changes: 29 additions & 0 deletions app/utils/Actions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,34 @@ import play.api._
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.libs.{Crypto => PlayCrypto}

case class ActionHelper(authorizationHeader: String) {
def getTokenTime(): Option[Long] = {
val start = "khmac:///sha-256;";
val slashPos = start.length + 64;

if(
!authorizationHeader.startsWith(start) ||
authorizationHeader.length < slashPos ||
authorizationHeader.charAt(slashPos) != '/'
) {
Logger.warn(s"Malformed authorization header")
return None
}

val hash = authorizationHeader.substring(start.length, slashPos)
val message = authorizationHeader.substring(slashPos + 1)

val split = message.split(':')
if (split.length < 7) {
Logger.warn(s"Malformed authorization header")
return None
}

val rcvTime = split(split.length - 1).toLong
return Some(rcvTime)
}
}

case class HMACActionHelper(
userId: String,
objType: String,
Expand Down Expand Up @@ -223,3 +251,4 @@ object LoggingFilter extends Filter {
}
}
}

Loading