decidim-bulletin_board 0.2.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +79 -0
- data/Gemfile.lock +16 -14
- data/decidim-bulletin_board.gemspec +3 -3
- data/lib/decidim/bulletin_board.rb +7 -5
- data/lib/decidim/bulletin_board/authority.rb +6 -0
- data/lib/decidim/bulletin_board/authority/close_ballot_box.rb +51 -0
- data/lib/decidim/bulletin_board/authority/create_election.rb +43 -0
- data/lib/decidim/bulletin_board/authority/get_election_status.rb +45 -0
- data/lib/decidim/bulletin_board/authority/open_ballot_box.rb +51 -0
- data/lib/decidim/bulletin_board/client.rb +28 -8
- data/lib/decidim/bulletin_board/command.rb +51 -0
- data/lib/decidim/bulletin_board/message_identifier.rb +86 -0
- data/lib/decidim/bulletin_board/version.rb +1 -1
- data/lib/decidim/bulletin_board/voter.rb +0 -1
- data/lib/decidim/bulletin_board/voter/cast_vote.rb +9 -16
- metadata +21 -27
- data/lib/decidim/bulletin_board/create_election.rb +0 -51
- data/lib/decidim/bulletin_board/voter/vote_form.rb +0 -63
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7338968926bbc16e964d828f014f50bb4e1d9f74846daac9a857b586687466c7
|
4
|
+
data.tar.gz: dda3edd9f06e01fad9a058be3672f9027009b8fca3012adf8e8ee00b408a665e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5ade03088b4e6a998dffced0d0efa1ee0169b4ed9614f2634e303c93cc62256748cb7189604879d0d813675d3437a8963b8c54272e25ffc78245d10d583b0e54
|
7
|
+
data.tar.gz: 4b7fdac495b5fce86c0b50b03069ef154041a43ed1c0db257bad5d7667cefbcaf45d51295d190ad3c16f56abdd3b3298daeb00dd57e1da78fb9630b1520a4ae1
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
|
+
|
8
|
+
## [0.5.1] - 2020-12-19
|
9
|
+
|
10
|
+
### Fixed
|
11
|
+
|
12
|
+
- Include the missing `close_ballot_box` method from the 0.4.0 release.
|
13
|
+
- Fixes for the client methods
|
14
|
+
|
15
|
+
## [0.5.0] - 2020-12-19
|
16
|
+
|
17
|
+
### Changed
|
18
|
+
|
19
|
+
- Renamed `setup_election` to `create_election` and moved `election_id` from the `election_data` to a separate argument.
|
20
|
+
|
21
|
+
### Fixed
|
22
|
+
|
23
|
+
- Include the missing `open_ballot_box` methods from the 0.4.0 release.
|
24
|
+
- Added missing namespace on the seed task
|
25
|
+
|
26
|
+
## [0.4.0] - 2020-12-18
|
27
|
+
|
28
|
+
### Added
|
29
|
+
|
30
|
+
- `content_hash` field for the `LogEntry` records with the hash of the `content` field, if included in the message.
|
31
|
+
- `Command` base class for all the classes representing GraphQL queries or mutations sent to the Bulletin Board.
|
32
|
+
- `open_ballot_box` and `close_ballot_box` methods to the `Decidim::BulletinBoard::Client`.
|
33
|
+
|
34
|
+
### Changed
|
35
|
+
|
36
|
+
- New format for the messages: `iat`, `message_id` and Bulletin Board fields in the root message, and `content` for the Voting Scheme messages.
|
37
|
+
- Improved consistency between methods included by `Decidim::BulletinBoard::Client`.
|
38
|
+
|
39
|
+
## [0.3.1] - 2020-12-10
|
40
|
+
|
41
|
+
### Fixed
|
42
|
+
|
43
|
+
- Uses the correct private key in the `sign_data` method.
|
44
|
+
|
45
|
+
## [0.3.0] - 2020-12-10
|
46
|
+
|
47
|
+
### Added
|
48
|
+
|
49
|
+
- `Decidim::BulletinBoard::Authority` namespace that includes commands and forms to get the status of an election.
|
50
|
+
|
51
|
+
### Changed
|
52
|
+
|
53
|
+
- `Decidim::BulletinBoard::Client` now includes a `get_status` method to get the election status using the `Authority` namespace.
|
54
|
+
|
55
|
+
### Fixed
|
56
|
+
|
57
|
+
- `Decidim::BulletinBoard::Voter::CastVote` command uses the `encrypted_vote` as a `String` and not as a `Hash`.
|
58
|
+
|
59
|
+
## [0.2.0] - 2020-12-08
|
60
|
+
|
61
|
+
### Added
|
62
|
+
|
63
|
+
- `Decidim::BulletinBoard::Voter` namespace that includes commands and forms to perform the cast vote action.
|
64
|
+
|
65
|
+
### Changed
|
66
|
+
|
67
|
+
- `Decidim::BulletinBoard::Client` now includes a `cast_vote` method to cast a vote using the `Voter` namespace.
|
68
|
+
|
69
|
+
## [0.1.0] - 2020-12-07
|
70
|
+
|
71
|
+
### Added
|
72
|
+
|
73
|
+
- `Decidim::BulletinBoard::Client` class totally configurable using `ActiveSupport::Configurable`
|
74
|
+
- The client now includes a `setup_election` method that creates the election in the bulletin board.
|
75
|
+
|
76
|
+
[unreleased]: https://github.com/decidim/decidim-bulletin-board/compare/v0.3.0...HEAD
|
77
|
+
[0.3.0]: https://github.com/decidim/decidim-bulletin-board/compare/v0.2.0...v0.3.0
|
78
|
+
[0.2.0]: https://github.com/decidim/decidim-bulletin-board/compare/v0.1.0...v0.2.0
|
79
|
+
[0.1.0]: https://github.com/decidim/decidim-bulletin-board/releases/tag/v0.1.0
|
data/Gemfile.lock
CHANGED
@@ -1,24 +1,25 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
decidim-bulletin_board (0.
|
5
|
-
activemodel (
|
6
|
-
activesupport (
|
4
|
+
decidim-bulletin_board (0.5.1)
|
5
|
+
activemodel (>= 5.0.0)
|
6
|
+
activesupport (>= 5.0.0)
|
7
7
|
byebug (~> 11.0)
|
8
8
|
graphlient (~> 0.4.0)
|
9
|
-
jwt
|
9
|
+
jwt (~> 2.2.2)
|
10
10
|
wisper (~> 2.0.0)
|
11
11
|
|
12
12
|
GEM
|
13
13
|
remote: https://rubygems.org/
|
14
14
|
specs:
|
15
|
-
activemodel (
|
16
|
-
activesupport (=
|
17
|
-
activesupport (
|
15
|
+
activemodel (6.1.0)
|
16
|
+
activesupport (= 6.1.0)
|
17
|
+
activesupport (6.1.0)
|
18
18
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
19
|
-
i18n (>=
|
20
|
-
minitest (
|
21
|
-
tzinfo (~>
|
19
|
+
i18n (>= 1.6, < 2)
|
20
|
+
minitest (>= 5.1)
|
21
|
+
tzinfo (~> 2.0)
|
22
|
+
zeitwerk (~> 2.3)
|
22
23
|
addressable (2.7.0)
|
23
24
|
public_suffix (>= 2.0.2, < 5.0)
|
24
25
|
ast (2.4.1)
|
@@ -87,9 +88,8 @@ GEM
|
|
87
88
|
rubocop-ast (>= 0.7.1)
|
88
89
|
ruby-progressbar (1.10.1)
|
89
90
|
ruby2_keywords (0.0.2)
|
90
|
-
|
91
|
-
|
92
|
-
thread_safe (~> 0.1)
|
91
|
+
tzinfo (2.0.4)
|
92
|
+
concurrent-ruby (~> 1.0)
|
93
93
|
unicode-display_width (1.7.0)
|
94
94
|
webmock (3.10.0)
|
95
95
|
addressable (>= 2.3.6)
|
@@ -97,9 +97,11 @@ GEM
|
|
97
97
|
hashdiff (>= 0.4.0, < 2.0.0)
|
98
98
|
wisper (2.0.1)
|
99
99
|
wisper-rspec (1.1.0)
|
100
|
+
zeitwerk (2.4.2)
|
100
101
|
|
101
102
|
PLATFORMS
|
102
103
|
ruby
|
104
|
+
x86_64-linux
|
103
105
|
|
104
106
|
DEPENDENCIES
|
105
107
|
decidim-bulletin_board!
|
@@ -112,4 +114,4 @@ DEPENDENCIES
|
|
112
114
|
wisper-rspec (~> 1.1.0)
|
113
115
|
|
114
116
|
BUNDLED WITH
|
115
|
-
2.
|
117
|
+
2.2.0
|
@@ -23,11 +23,11 @@ Gem::Specification.new do |s|
|
|
23
23
|
s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
24
|
s.require_paths = ["lib"]
|
25
25
|
|
26
|
-
s.add_dependency "activemodel", "
|
27
|
-
s.add_dependency "activesupport", "
|
26
|
+
s.add_dependency "activemodel", ">= 5.0.0"
|
27
|
+
s.add_dependency "activesupport", ">= 5.0.0"
|
28
28
|
s.add_dependency "byebug", "~> 11.0"
|
29
29
|
s.add_dependency "graphlient", "~> 0.4.0"
|
30
|
-
s.add_dependency "jwt"
|
30
|
+
s.add_dependency "jwt", "~> 2.2.2"
|
31
31
|
s.add_dependency "wisper", "~> 2.0.0"
|
32
32
|
|
33
33
|
s.add_development_dependency "rake", "~> 13.0"
|
@@ -1,16 +1,18 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "active_model"
|
4
|
+
require "active_support/configurable"
|
3
5
|
require "decidim/bulletin_board/version"
|
6
|
+
require "jwt"
|
4
7
|
require "graphlient"
|
5
8
|
require "wisper"
|
6
|
-
|
9
|
+
|
7
10
|
require "decidim/bulletin_board/jwk_utils"
|
11
|
+
require "decidim/bulletin_board/message_identifier"
|
12
|
+
|
8
13
|
require "decidim/bulletin_board/client"
|
9
|
-
require "decidim/bulletin_board/
|
10
|
-
require "decidim/bulletin_board/create_election"
|
14
|
+
require "decidim/bulletin_board/authority"
|
11
15
|
require "decidim/bulletin_board/voter"
|
12
|
-
require "active_support/configurable"
|
13
|
-
require "jwt"
|
14
16
|
|
15
17
|
module Decidim
|
16
18
|
# This module holds all the logic for the Bulletin Board Ruby Client to connect
|
@@ -0,0 +1,6 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "decidim/bulletin_board/authority/create_election"
|
4
|
+
require "decidim/bulletin_board/authority/get_election_status"
|
5
|
+
require "decidim/bulletin_board/authority/open_ballot_box"
|
6
|
+
require "decidim/bulletin_board/authority/close_ballot_box"
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Decidim
|
4
|
+
module BulletinBoard
|
5
|
+
module Authority
|
6
|
+
# This command uses the GraphQL client to request the closing of the ballot box.
|
7
|
+
class CloseBallotBox < Decidim::BulletinBoard::Command
|
8
|
+
# Public: Initializes the command.
|
9
|
+
#
|
10
|
+
# election_id - The local election identifier
|
11
|
+
def initialize(election_id)
|
12
|
+
@election_id = election_id
|
13
|
+
end
|
14
|
+
|
15
|
+
# Executes the command. Broadcasts these events:
|
16
|
+
#
|
17
|
+
# - :ok when everything is valid and the query operation is successful.
|
18
|
+
# - :error if query operation was not successful.
|
19
|
+
#
|
20
|
+
# Returns nothing.
|
21
|
+
def call
|
22
|
+
message_id = message_id(unique_election_id(election_id), "close_ballot_box")
|
23
|
+
signed_data = sign_message(message_id, {})
|
24
|
+
|
25
|
+
begin
|
26
|
+
response = client.query do
|
27
|
+
mutation do
|
28
|
+
closeBallotBox(messageId: message_id, signedData: signed_data) do
|
29
|
+
election do
|
30
|
+
status
|
31
|
+
end
|
32
|
+
error
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
return broadcast(:error, response.data.close_ballot_box.error) if response.data.close_ballot_box.error.present?
|
38
|
+
|
39
|
+
broadcast(:ok, response.data.close_ballot_box.election)
|
40
|
+
rescue Graphlient::Errors::ServerError
|
41
|
+
broadcast(:error, "Sorry, something went wrong")
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
attr_reader :election_id
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Decidim
|
4
|
+
module BulletinBoard
|
5
|
+
module Authority
|
6
|
+
# This class handles the creation of an election.
|
7
|
+
class CreateElection < Decidim::BulletinBoard::Command
|
8
|
+
def initialize(election_id, election_data)
|
9
|
+
@election_id = election_id
|
10
|
+
@election_data = election_data
|
11
|
+
end
|
12
|
+
|
13
|
+
def call
|
14
|
+
message_id = message_id(unique_election_id(election_id), "create_election")
|
15
|
+
signed_data = sign_message(message_id, election_data)
|
16
|
+
|
17
|
+
begin
|
18
|
+
response = client.query do
|
19
|
+
mutation do
|
20
|
+
createElection(messageId: message_id, signedData: signed_data) do
|
21
|
+
election do
|
22
|
+
status
|
23
|
+
end
|
24
|
+
error
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
return broadcast(:error, response.data.create_election.error) if response.data.create_election.error.present?
|
30
|
+
|
31
|
+
broadcast(:ok, response.data.create_election.election)
|
32
|
+
rescue Graphlient::Errors::ServerError
|
33
|
+
broadcast(:error, "Sorry, something went wrong")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
attr_reader :election_data, :election_id
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Decidim
|
4
|
+
module BulletinBoard
|
5
|
+
module Authority
|
6
|
+
# This command uses the GraphQL client to get the status of the election.
|
7
|
+
class GetElectionStatus < Decidim::BulletinBoard::Command
|
8
|
+
# Public: Initializes the command.
|
9
|
+
#
|
10
|
+
# election_id - The local election identifier
|
11
|
+
def initialize(election_id)
|
12
|
+
@election_id = election_id
|
13
|
+
end
|
14
|
+
|
15
|
+
# Executes the command. Broadcasts these events:
|
16
|
+
#
|
17
|
+
# - :ok when everything is valid and the query operation is successful.
|
18
|
+
# - :error if query operation was not successful.
|
19
|
+
#
|
20
|
+
# Returns nothing.
|
21
|
+
def call
|
22
|
+
unique_id = unique_election_id(election_id)
|
23
|
+
|
24
|
+
begin
|
25
|
+
response = client.query do
|
26
|
+
query do
|
27
|
+
election(uniqueId: unique_id) do
|
28
|
+
status
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
broadcast(:ok, response.data.election.status)
|
34
|
+
rescue Graphlient::Errors::ServerError
|
35
|
+
broadcast(:error, "Sorry, something went wrong")
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
attr_reader :election_id
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Decidim
|
4
|
+
module BulletinBoard
|
5
|
+
module Authority
|
6
|
+
# This command uses the GraphQL client to request the opening of the ballot box.
|
7
|
+
class OpenBallotBox < Decidim::BulletinBoard::Command
|
8
|
+
# Public: Initializes the command.
|
9
|
+
#
|
10
|
+
# election_id - The local election identifier
|
11
|
+
def initialize(election_id)
|
12
|
+
@election_id = election_id
|
13
|
+
end
|
14
|
+
|
15
|
+
# Executes the command. Broadcasts these events:
|
16
|
+
#
|
17
|
+
# - :ok when everything is valid and the query operation is successful.
|
18
|
+
# - :error if query operation was not successful.
|
19
|
+
#
|
20
|
+
# Returns nothing.
|
21
|
+
def call
|
22
|
+
message_id = message_id(unique_election_id(election_id), "open_ballot_box")
|
23
|
+
signed_data = sign_message(message_id, {})
|
24
|
+
|
25
|
+
begin
|
26
|
+
response = client.query do
|
27
|
+
mutation do
|
28
|
+
openBallotBox(messageId: message_id, signedData: signed_data) do
|
29
|
+
election do
|
30
|
+
status
|
31
|
+
end
|
32
|
+
error
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
return broadcast(:error, response.data.open_ballot_box.error) if response.data.open_ballot_box.error.present?
|
38
|
+
|
39
|
+
broadcast(:ok, response.data.open_ballot_box.election)
|
40
|
+
rescue Graphlient::Errors::ServerError
|
41
|
+
broadcast(:error, "Sorry, something went wrong")
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
attr_reader :election_id
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "decidim/bulletin_board/command"
|
4
|
+
|
3
5
|
module Decidim
|
4
6
|
module BulletinBoard
|
5
7
|
# The Bulletin Board client
|
@@ -34,23 +36,41 @@ module Decidim
|
|
34
36
|
private_key && server && api_key
|
35
37
|
end
|
36
38
|
|
37
|
-
def
|
38
|
-
|
39
|
+
def create_election(election_id, election_data)
|
40
|
+
create_election = Decidim::BulletinBoard::Authority::CreateElection.new(election_id, election_data)
|
41
|
+
create_election.on(:ok) { |election| return election }
|
42
|
+
create_election.on(:error) { |error_message| raise StandardError, error_message }
|
43
|
+
create_election.call
|
44
|
+
end
|
45
|
+
|
46
|
+
def open_ballot_box(election_id)
|
47
|
+
open_ballot_box = Decidim::BulletinBoard::Authority::OpenBallotBox.new(election_id)
|
48
|
+
open_ballot_box.on(:ok) { |election| return election }
|
49
|
+
open_ballot_box.on(:error) { |error_message| raise StandardError, error_message }
|
50
|
+
open_ballot_box.call
|
39
51
|
end
|
40
52
|
|
41
|
-
def
|
42
|
-
|
43
|
-
|
53
|
+
def close_ballot_box(election_id)
|
54
|
+
close_ballot_box = Decidim::BulletinBoard::Authority::CloseBallotBox.new(election_id)
|
55
|
+
close_ballot_box.on(:ok) { |election| return election }
|
56
|
+
close_ballot_box.on(:error) { |error_message| raise StandardError, error_message }
|
57
|
+
close_ballot_box.call
|
44
58
|
end
|
45
59
|
|
46
|
-
def cast_vote(
|
47
|
-
|
48
|
-
cast_vote = Decidim::BulletinBoard::Voter::CastVote.new(form)
|
60
|
+
def cast_vote(election_id, voter_id, encrypted_vote)
|
61
|
+
cast_vote = Decidim::BulletinBoard::Voter::CastVote.new(election_id, voter_id, encrypted_vote)
|
49
62
|
cast_vote.on(:ok) { |pending_message| return pending_message }
|
50
63
|
cast_vote.on(:error) { |error_message| raise StandardError, error_message }
|
51
64
|
cast_vote.call
|
52
65
|
end
|
53
66
|
|
67
|
+
def get_status(election_id)
|
68
|
+
get_status = Decidim::BulletinBoard::Authority::GetElectionStatus.new(election_id)
|
69
|
+
get_status.on(:ok) { |status| return status }
|
70
|
+
get_status.on(:error) { |error_message| raise StandardError, error_message }
|
71
|
+
get_status.call
|
72
|
+
end
|
73
|
+
|
54
74
|
private
|
55
75
|
|
56
76
|
attr_reader :identification_private_key, :private_key
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "decidim/bulletin_board/graphql/client"
|
4
|
+
|
5
|
+
module Decidim
|
6
|
+
module BulletinBoard
|
7
|
+
# The base class for all commands.
|
8
|
+
class Command
|
9
|
+
include Wisper::Publisher
|
10
|
+
|
11
|
+
delegate :authority_slug, :private_key, to: :class
|
12
|
+
|
13
|
+
def unique_election_id(election_id)
|
14
|
+
Decidim::BulletinBoard::MessageIdentifier.unique_election_id(authority_slug, election_id)
|
15
|
+
end
|
16
|
+
|
17
|
+
def message_id(unique_election_id, type_subtype, voter_id = nil)
|
18
|
+
Decidim::BulletinBoard::MessageIdentifier.format(unique_election_id, type_subtype, voter_id ? :voter : :authority, voter_id || authority_slug)
|
19
|
+
end
|
20
|
+
|
21
|
+
def sign_message(message_id, message)
|
22
|
+
JWT.encode(complete_message(message_id, message), private_key.keypair, "RS256")
|
23
|
+
end
|
24
|
+
|
25
|
+
def client
|
26
|
+
@client ||= BulletinBoard::Graphql::Client.client
|
27
|
+
end
|
28
|
+
|
29
|
+
def complete_message(message_id, message)
|
30
|
+
message.merge({
|
31
|
+
iat: Time.now.to_i,
|
32
|
+
message_id: message_id
|
33
|
+
})
|
34
|
+
end
|
35
|
+
|
36
|
+
class << self
|
37
|
+
def self.call(*args)
|
38
|
+
new(*args).call
|
39
|
+
end
|
40
|
+
|
41
|
+
def private_key
|
42
|
+
@private_key ||= JwkUtils.import_private_key(BulletinBoard.identification_private_key)
|
43
|
+
end
|
44
|
+
|
45
|
+
def authority_slug
|
46
|
+
@authority_slug ||= BulletinBoard.authority_name.parameterize
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Decidim
|
4
|
+
module BulletinBoard
|
5
|
+
class MessageIdentifier
|
6
|
+
AUTHOR_TYPE = {
|
7
|
+
a: :authority,
|
8
|
+
b: :bulletin_board,
|
9
|
+
t: :trustee,
|
10
|
+
v: :voter
|
11
|
+
}.freeze
|
12
|
+
|
13
|
+
INVERTED_AUTHOR_TYPE = AUTHOR_TYPE.invert.freeze
|
14
|
+
|
15
|
+
def initialize(message_id)
|
16
|
+
@message_id = message_id
|
17
|
+
end
|
18
|
+
|
19
|
+
def from_authority?
|
20
|
+
author_type == :authority
|
21
|
+
end
|
22
|
+
|
23
|
+
def from_trustee?
|
24
|
+
author_type == :trustee
|
25
|
+
end
|
26
|
+
|
27
|
+
def from_voter?
|
28
|
+
author_type == :voter
|
29
|
+
end
|
30
|
+
|
31
|
+
def author_type
|
32
|
+
@author_type ||= AUTHOR_TYPE[author.first.to_sym]
|
33
|
+
end
|
34
|
+
|
35
|
+
def author_id
|
36
|
+
@author_id ||= author.last
|
37
|
+
end
|
38
|
+
|
39
|
+
def authority_id
|
40
|
+
@authority_id ||= elements[0]
|
41
|
+
end
|
42
|
+
|
43
|
+
def election_id
|
44
|
+
@election_id = elements[0..1].join(".")
|
45
|
+
end
|
46
|
+
|
47
|
+
def type
|
48
|
+
@type = elements[2]
|
49
|
+
end
|
50
|
+
|
51
|
+
def subtype
|
52
|
+
@subtype = elements[3]
|
53
|
+
end
|
54
|
+
|
55
|
+
def to_s
|
56
|
+
@message_id
|
57
|
+
end
|
58
|
+
|
59
|
+
class << self
|
60
|
+
def format(unique_election_id, type_subtype, author_type, author_id)
|
61
|
+
"#{unique_election_id}.#{type_subtype}+#{INVERTED_AUTHOR_TYPE[author_type]}.#{author_id}"
|
62
|
+
end
|
63
|
+
|
64
|
+
def unique_election_id(authority_slug, election_id)
|
65
|
+
"#{authority_slug}.#{election_id}"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
private
|
70
|
+
|
71
|
+
attr_accessor :message_id
|
72
|
+
|
73
|
+
def elements
|
74
|
+
@elements ||= parts.first.split(".", 4)
|
75
|
+
end
|
76
|
+
|
77
|
+
def author
|
78
|
+
@author ||= parts.last.split(".", 2)
|
79
|
+
end
|
80
|
+
|
81
|
+
def parts
|
82
|
+
@parts ||= message_id.split("+")
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -4,13 +4,14 @@ module Decidim
|
|
4
4
|
module BulletinBoard
|
5
5
|
module Voter
|
6
6
|
# This command uses the GraphQL client to cast the vote.
|
7
|
-
class CastVote
|
8
|
-
include Wisper::Publisher
|
7
|
+
class CastVote < Decidim::BulletinBoard::Command
|
9
8
|
# Public: Initializes the command.
|
10
9
|
#
|
11
10
|
# form - A form object with the params.
|
12
|
-
def initialize(
|
13
|
-
@
|
11
|
+
def initialize(election_id, voter_id, encrypted_vote)
|
12
|
+
@election_id = election_id
|
13
|
+
@voter_id = voter_id
|
14
|
+
@encrypted_vote = encrypted_vote
|
14
15
|
end
|
15
16
|
|
16
17
|
# Executes the command. Broadcasts these events:
|
@@ -20,17 +21,13 @@ module Decidim
|
|
20
21
|
#
|
21
22
|
# Returns nothing.
|
22
23
|
def call
|
23
|
-
|
24
|
-
|
25
|
-
args = {
|
26
|
-
message_id: form.message_id,
|
27
|
-
signed_data: form.signed_data
|
28
|
-
}
|
24
|
+
message_id = message_id(unique_election_id(election_id), "vote.cast", voter_id)
|
25
|
+
signed_data = sign_message(message_id, { content: encrypted_vote })
|
29
26
|
|
30
27
|
begin
|
31
28
|
response = client.query do
|
32
29
|
mutation do
|
33
|
-
vote(messageId:
|
30
|
+
vote(messageId: message_id, signedData: signed_data) do
|
34
31
|
pendingMessage do
|
35
32
|
status
|
36
33
|
end
|
@@ -49,11 +46,7 @@ module Decidim
|
|
49
46
|
|
50
47
|
private
|
51
48
|
|
52
|
-
attr_reader :
|
53
|
-
|
54
|
-
def client
|
55
|
-
@client ||= BulletinBoard::Graphql::Client.client
|
56
|
-
end
|
49
|
+
attr_reader :election_id, :voter_id, :encrypted_vote
|
57
50
|
end
|
58
51
|
end
|
59
52
|
end
|
metadata
CHANGED
@@ -1,56 +1,44 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: decidim-bulletin_board
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Morcillo
|
8
8
|
- Svenja Schäfer
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2020-12-
|
12
|
+
date: 2020-12-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activemodel
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
requirements:
|
18
|
-
- - "~>"
|
19
|
-
- !ruby/object:Gem::Version
|
20
|
-
version: '5.0'
|
21
18
|
- - ">="
|
22
19
|
- !ruby/object:Gem::Version
|
23
|
-
version: 5.0.0
|
20
|
+
version: 5.0.0
|
24
21
|
type: :runtime
|
25
22
|
prerelease: false
|
26
23
|
version_requirements: !ruby/object:Gem::Requirement
|
27
24
|
requirements:
|
28
|
-
- - "~>"
|
29
|
-
- !ruby/object:Gem::Version
|
30
|
-
version: '5.0'
|
31
25
|
- - ">="
|
32
26
|
- !ruby/object:Gem::Version
|
33
|
-
version: 5.0.0
|
27
|
+
version: 5.0.0
|
34
28
|
- !ruby/object:Gem::Dependency
|
35
29
|
name: activesupport
|
36
30
|
requirement: !ruby/object:Gem::Requirement
|
37
31
|
requirements:
|
38
|
-
- - "~>"
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '5.0'
|
41
32
|
- - ">="
|
42
33
|
- !ruby/object:Gem::Version
|
43
|
-
version: 5.0.0
|
34
|
+
version: 5.0.0
|
44
35
|
type: :runtime
|
45
36
|
prerelease: false
|
46
37
|
version_requirements: !ruby/object:Gem::Requirement
|
47
38
|
requirements:
|
48
|
-
- - "~>"
|
49
|
-
- !ruby/object:Gem::Version
|
50
|
-
version: '5.0'
|
51
39
|
- - ">="
|
52
40
|
- !ruby/object:Gem::Version
|
53
|
-
version: 5.0.0
|
41
|
+
version: 5.0.0
|
54
42
|
- !ruby/object:Gem::Dependency
|
55
43
|
name: byebug
|
56
44
|
requirement: !ruby/object:Gem::Requirement
|
@@ -83,16 +71,16 @@ dependencies:
|
|
83
71
|
name: jwt
|
84
72
|
requirement: !ruby/object:Gem::Requirement
|
85
73
|
requirements:
|
86
|
-
- - "
|
74
|
+
- - "~>"
|
87
75
|
- !ruby/object:Gem::Version
|
88
|
-
version:
|
76
|
+
version: 2.2.2
|
89
77
|
type: :runtime
|
90
78
|
prerelease: false
|
91
79
|
version_requirements: !ruby/object:Gem::Requirement
|
92
80
|
requirements:
|
93
|
-
- - "
|
81
|
+
- - "~>"
|
94
82
|
- !ruby/object:Gem::Version
|
95
|
-
version:
|
83
|
+
version: 2.2.2
|
96
84
|
- !ruby/object:Gem::Dependency
|
97
85
|
name: wisper
|
98
86
|
requirement: !ruby/object:Gem::Requirement
|
@@ -176,6 +164,7 @@ files:
|
|
176
164
|
- ".rubocop.yml"
|
177
165
|
- ".ruby-version"
|
178
166
|
- ".travis.yml"
|
167
|
+
- CHANGELOG.md
|
179
168
|
- CODE_OF_CONDUCT.md
|
180
169
|
- Gemfile
|
181
170
|
- Gemfile.lock
|
@@ -185,19 +174,24 @@ files:
|
|
185
174
|
- bin/setup
|
186
175
|
- decidim-bulletin_board.gemspec
|
187
176
|
- lib/decidim/bulletin_board.rb
|
177
|
+
- lib/decidim/bulletin_board/authority.rb
|
178
|
+
- lib/decidim/bulletin_board/authority/close_ballot_box.rb
|
179
|
+
- lib/decidim/bulletin_board/authority/create_election.rb
|
180
|
+
- lib/decidim/bulletin_board/authority/get_election_status.rb
|
181
|
+
- lib/decidim/bulletin_board/authority/open_ballot_box.rb
|
188
182
|
- lib/decidim/bulletin_board/client.rb
|
189
|
-
- lib/decidim/bulletin_board/
|
183
|
+
- lib/decidim/bulletin_board/command.rb
|
190
184
|
- lib/decidim/bulletin_board/graphql/client.rb
|
191
185
|
- lib/decidim/bulletin_board/jwk_utils.rb
|
186
|
+
- lib/decidim/bulletin_board/message_identifier.rb
|
192
187
|
- lib/decidim/bulletin_board/version.rb
|
193
188
|
- lib/decidim/bulletin_board/voter.rb
|
194
189
|
- lib/decidim/bulletin_board/voter/cast_vote.rb
|
195
|
-
- lib/decidim/bulletin_board/voter/vote_form.rb
|
196
190
|
homepage: https://github.com
|
197
191
|
licenses:
|
198
192
|
- AGPL-3.0
|
199
193
|
metadata: {}
|
200
|
-
post_install_message:
|
194
|
+
post_install_message:
|
201
195
|
rdoc_options: []
|
202
196
|
require_paths:
|
203
197
|
- lib
|
@@ -213,7 +207,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
213
207
|
version: '0'
|
214
208
|
requirements: []
|
215
209
|
rubygems_version: 3.0.3
|
216
|
-
signing_key:
|
210
|
+
signing_key:
|
217
211
|
specification_version: 4
|
218
212
|
summary: ''
|
219
213
|
test_files: []
|
@@ -1,51 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Decidim
|
4
|
-
module BulletinBoard
|
5
|
-
# This class handles the creation of an election.
|
6
|
-
class CreateElection
|
7
|
-
def initialize(election_data, message_id)
|
8
|
-
@client = BulletinBoard::Graphql::Client.client
|
9
|
-
@election_data = election_data
|
10
|
-
@message_id = message_id
|
11
|
-
@private_key = private_key
|
12
|
-
end
|
13
|
-
|
14
|
-
def self.call(election_data, message_id)
|
15
|
-
new(election_data, message_id).call
|
16
|
-
end
|
17
|
-
|
18
|
-
def call
|
19
|
-
args = {
|
20
|
-
message_id: message_id,
|
21
|
-
signed_data: encode_data(election_data)
|
22
|
-
}
|
23
|
-
|
24
|
-
response = client.query do
|
25
|
-
mutation do
|
26
|
-
createElection(messageId: args[:message_id], signedData: args[:signed_data]) do
|
27
|
-
election do
|
28
|
-
status
|
29
|
-
end
|
30
|
-
error
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
response.data.create_election
|
36
|
-
end
|
37
|
-
|
38
|
-
private
|
39
|
-
|
40
|
-
attr_reader :client, :election_data, :message_id
|
41
|
-
|
42
|
-
def private_key
|
43
|
-
@private_key ||= JwkUtils.import_private_key(BulletinBoard.identification_private_key)
|
44
|
-
end
|
45
|
-
|
46
|
-
def encode_data(election_data)
|
47
|
-
JWT.encode(election_data, private_key.keypair, "RS256")
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
@@ -1,63 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Decidim
|
4
|
-
module BulletinBoard
|
5
|
-
module Voter
|
6
|
-
# A form object to handle some data transformation and validation to cast a vote.
|
7
|
-
class VoteForm
|
8
|
-
include ActiveModel::Validations
|
9
|
-
|
10
|
-
validates :election_data, :voter_data, :encrypted_vote, presence: true
|
11
|
-
validate :election_id_present
|
12
|
-
validate :voter_id_present
|
13
|
-
|
14
|
-
# Public: initialize the form
|
15
|
-
#
|
16
|
-
# bulletin_board_data - An instance of the bulletin board client
|
17
|
-
# election_data - A Hash including the necessary data from the election.
|
18
|
-
# voter_data - A Hash including the necessary data from the voter.
|
19
|
-
# encrypted_vote - A Hash including the encrypted vote to cast
|
20
|
-
def initialize(bulletin_board_client, election_data, voter_data, encrypted_vote)
|
21
|
-
@bulletin_board_client = bulletin_board_client
|
22
|
-
@election_data = election_data
|
23
|
-
@voter_data = voter_data
|
24
|
-
@encrypted_vote = encrypted_vote
|
25
|
-
end
|
26
|
-
|
27
|
-
# Public: returns a message identifier for the cast vote operation.
|
28
|
-
def message_id
|
29
|
-
@message_id ||= "#{election_id}.vote.cast+v.#{voter_id}"
|
30
|
-
end
|
31
|
-
|
32
|
-
# Public: uses the bulletin board client to sign the encrypted vote merged with the `message_id`.
|
33
|
-
def signed_data
|
34
|
-
@signed_data ||= bulletin_board_client.sign_data(encrypted_vote.merge(message_id: message_id))
|
35
|
-
end
|
36
|
-
|
37
|
-
private
|
38
|
-
|
39
|
-
attr_reader :bulletin_board_client, :election_data, :voter_data, :encrypted_vote
|
40
|
-
|
41
|
-
def election_id_present
|
42
|
-
errors.add(:election_data, "doesn't include the election id") unless election_id.present?
|
43
|
-
end
|
44
|
-
|
45
|
-
def election_id
|
46
|
-
return if election_data.blank?
|
47
|
-
|
48
|
-
election_data[:election_id]
|
49
|
-
end
|
50
|
-
|
51
|
-
def voter_id_present
|
52
|
-
errors.add(:voter_data, "doesn't include the voter id") unless voter_id.present?
|
53
|
-
end
|
54
|
-
|
55
|
-
def voter_id
|
56
|
-
return if voter_data.blank?
|
57
|
-
|
58
|
-
voter_data[:voter_id]
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|