File tree Expand file tree Collapse file tree 2 files changed +40
-1
lines changed
Expand file tree Collapse file tree 2 files changed +40
-1
lines changed Original file line number Diff line number Diff 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 ||
Original file line number Diff line number Diff line change @@ -24,6 +24,34 @@ import play.api._
2424import play .api .libs .concurrent .Execution .Implicits .defaultContext
2525import 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+
2755case class HMACActionHelper (
2856 userId : String ,
2957 objType : String ,
@@ -223,3 +251,4 @@ object LoggingFilter extends Filter {
223251 }
224252 }
225253}
254+
You can’t perform that action at this time.
0 commit comments