decidim-bulletin_board 0.1.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +72 -0
- data/Gemfile.lock +16 -6
- data/decidim-bulletin_board.gemspec +5 -2
- data/lib/decidim/bulletin_board.rb +9 -4
- 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 -3
- 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 +3 -0
- data/lib/decidim/bulletin_board/voter/cast_vote.rb +53 -0
- metadata +63 -18
- data/lib/decidim/bulletin_board/create_election.rb +0 -51
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e07fa810de254d60c03c695aa6708e8813cf1f71a4df80fb7c0086ea57da74de
|
4
|
+
data.tar.gz: fd6bfc7b6afc49fac85f6b8d34d27a6c39cf37d679e2a7faf702a05bc7ec52d4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 98aaac12e64a4aafcd67d8058518cf36b389ef2bca2cddffc23f03944ed7194b2d38568e77a76e910a0289974e5e1a4f539b513fcebc935fb1e69b6ca332bb68
|
7
|
+
data.tar.gz: 73c0f19adfa8c2365491c4d9a46387fecb7d01098695e734e3653c1f43944e40aa5c07168ba86877394029ac16feb85ccdd0aa2b25ca02f54ec837588a82a4de
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,72 @@
|
|
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.0] - 2020-12-19
|
9
|
+
|
10
|
+
### Changed
|
11
|
+
|
12
|
+
- Renamed `setup_election` to `create_election` and moved `election_id` from the `election_data` to a separate argument.
|
13
|
+
|
14
|
+
### Fixed
|
15
|
+
|
16
|
+
- Include the missing `open_ballot_box` and `close_ballot_box` methods from the previous release.
|
17
|
+
- Added missing namespace on the seed task
|
18
|
+
|
19
|
+
## [0.4.0] - 2020-12-18
|
20
|
+
|
21
|
+
### Added
|
22
|
+
|
23
|
+
- `content_hash` field for the `LogEntry` records with the hash of the `content` field, if included in the message.
|
24
|
+
- `Command` base class for all the classes representing GraphQL queries or mutations sent to the Bulletin Board.
|
25
|
+
- `open_ballot_box` and `close_ballot_box` methods to the `Decidim::BulletinBoard::Client`.
|
26
|
+
|
27
|
+
### Changed
|
28
|
+
|
29
|
+
- New format for the messages: `iat`, `message_id` and Bulletin Board fields in the root message, and `content` for the Voting Scheme messages.
|
30
|
+
- Improved consistency between methods included by `Decidim::BulletinBoard::Client`.
|
31
|
+
|
32
|
+
## [0.3.1] - 2020-12-10
|
33
|
+
|
34
|
+
### Fixed
|
35
|
+
|
36
|
+
- Uses the correct private key in the `sign_data` method.
|
37
|
+
|
38
|
+
## [0.3.0] - 2020-12-10
|
39
|
+
|
40
|
+
### Added
|
41
|
+
|
42
|
+
- `Decidim::BulletinBoard::Authority` namespace that includes commands and forms to get the status of an election.
|
43
|
+
|
44
|
+
### Changed
|
45
|
+
|
46
|
+
- `Decidim::BulletinBoard::Client` now includes a `get_status` method to get the election status using the `Authority` namespace.
|
47
|
+
|
48
|
+
### Fixed
|
49
|
+
|
50
|
+
- `Decidim::BulletinBoard::Voter::CastVote` command uses the `encrypted_vote` as a `String` and not as a `Hash`.
|
51
|
+
|
52
|
+
## [0.2.0] - 2020-12-08
|
53
|
+
|
54
|
+
### Added
|
55
|
+
|
56
|
+
- `Decidim::BulletinBoard::Voter` namespace that includes commands and forms to perform the cast vote action.
|
57
|
+
|
58
|
+
### Changed
|
59
|
+
|
60
|
+
- `Decidim::BulletinBoard::Client` now includes a `cast_vote` method to cast a vote using the `Voter` namespace.
|
61
|
+
|
62
|
+
## [0.1.0] - 2020-12-07
|
63
|
+
|
64
|
+
### Added
|
65
|
+
|
66
|
+
- `Decidim::BulletinBoard::Client` class totally configurable using `ActiveSupport::Configurable`
|
67
|
+
- The client now includes a `setup_election` method that creates the election in the bulletin board.
|
68
|
+
|
69
|
+
[unreleased]: https://github.com/decidim/decidim-bulletin-board/compare/v0.3.0...HEAD
|
70
|
+
[0.3.0]: https://github.com/decidim/decidim-bulletin-board/compare/v0.2.0...v0.3.0
|
71
|
+
[0.2.0]: https://github.com/decidim/decidim-bulletin-board/compare/v0.1.0...v0.2.0
|
72
|
+
[0.1.0]: https://github.com/decidim/decidim-bulletin-board/releases/tag/v0.1.0
|
data/Gemfile.lock
CHANGED
@@ -1,20 +1,25 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
decidim-bulletin_board (0.
|
5
|
-
|
4
|
+
decidim-bulletin_board (0.5.0)
|
5
|
+
activemodel (>= 5.0.0)
|
6
|
+
activesupport (>= 5.0.0)
|
6
7
|
byebug (~> 11.0)
|
7
8
|
graphlient (~> 0.4.0)
|
8
|
-
jwt
|
9
|
+
jwt (~> 2.2.2)
|
10
|
+
wisper (~> 2.0.0)
|
9
11
|
|
10
12
|
GEM
|
11
13
|
remote: https://rubygems.org/
|
12
14
|
specs:
|
13
|
-
|
15
|
+
activemodel (6.0.3.4)
|
16
|
+
activesupport (= 6.0.3.4)
|
17
|
+
activesupport (6.0.3.4)
|
14
18
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
15
19
|
i18n (>= 0.7, < 2)
|
16
20
|
minitest (~> 5.1)
|
17
21
|
tzinfo (~> 1.1)
|
22
|
+
zeitwerk (~> 2.2, >= 2.2.2)
|
18
23
|
addressable (2.7.0)
|
19
24
|
public_suffix (>= 2.0.2, < 5.0)
|
20
25
|
ast (2.4.1)
|
@@ -84,16 +89,20 @@ GEM
|
|
84
89
|
ruby-progressbar (1.10.1)
|
85
90
|
ruby2_keywords (0.0.2)
|
86
91
|
thread_safe (0.3.6)
|
87
|
-
tzinfo (1.2.
|
92
|
+
tzinfo (1.2.9)
|
88
93
|
thread_safe (~> 0.1)
|
89
94
|
unicode-display_width (1.7.0)
|
90
95
|
webmock (3.10.0)
|
91
96
|
addressable (>= 2.3.6)
|
92
97
|
crack (>= 0.3.2)
|
93
98
|
hashdiff (>= 0.4.0, < 2.0.0)
|
99
|
+
wisper (2.0.1)
|
100
|
+
wisper-rspec (1.1.0)
|
101
|
+
zeitwerk (2.4.2)
|
94
102
|
|
95
103
|
PLATFORMS
|
96
104
|
ruby
|
105
|
+
x86_64-linux
|
97
106
|
|
98
107
|
DEPENDENCIES
|
99
108
|
decidim-bulletin_board!
|
@@ -103,6 +112,7 @@ DEPENDENCIES
|
|
103
112
|
rubocop-faker
|
104
113
|
rubocop-rspec (~> 1.21)
|
105
114
|
webmock (~> 3.6)
|
115
|
+
wisper-rspec (~> 1.1.0)
|
106
116
|
|
107
117
|
BUNDLED WITH
|
108
|
-
2.
|
118
|
+
2.2.0
|
@@ -23,12 +23,15 @@ 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 "
|
26
|
+
s.add_dependency "activemodel", ">= 5.0.0"
|
27
|
+
s.add_dependency "activesupport", ">= 5.0.0"
|
27
28
|
s.add_dependency "byebug", "~> 11.0"
|
28
29
|
s.add_dependency "graphlient", "~> 0.4.0"
|
29
|
-
s.add_dependency "jwt"
|
30
|
+
s.add_dependency "jwt", "~> 2.2.2"
|
31
|
+
s.add_dependency "wisper", "~> 2.0.0"
|
30
32
|
|
31
33
|
s.add_development_dependency "rake", "~> 13.0"
|
32
34
|
s.add_development_dependency "rspec", "~> 3.7"
|
33
35
|
s.add_development_dependency "webmock", "~> 3.6"
|
36
|
+
s.add_development_dependency "wisper-rspec", "~> 1.1.0"
|
34
37
|
end
|
@@ -1,13 +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"
|
8
|
+
require "wisper"
|
9
|
+
|
5
10
|
require "decidim/bulletin_board/jwk_utils"
|
11
|
+
require "decidim/bulletin_board/message_identifier"
|
12
|
+
|
6
13
|
require "decidim/bulletin_board/client"
|
7
|
-
require "decidim/bulletin_board/
|
8
|
-
require "decidim/bulletin_board/
|
9
|
-
require "active_support/configurable"
|
10
|
-
require "jwt"
|
14
|
+
require "decidim/bulletin_board/authority"
|
15
|
+
require "decidim/bulletin_board/voter"
|
11
16
|
|
12
17
|
module Decidim
|
13
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,9 +36,32 @@ module Decidim
|
|
34
36
|
private_key && server && api_key
|
35
37
|
end
|
36
38
|
|
37
|
-
def
|
38
|
-
|
39
|
-
|
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::Election::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
|
51
|
+
end
|
52
|
+
|
53
|
+
def cast_vote(election_id, voter_id, encrypted_vote)
|
54
|
+
cast_vote = Decidim::BulletinBoard::Voter::CastVote.new(election_id, voter_id, encrypted_vote)
|
55
|
+
cast_vote.on(:ok) { |pending_message| return pending_message }
|
56
|
+
cast_vote.on(:error) { |error_message| raise StandardError, error_message }
|
57
|
+
cast_vote.call
|
58
|
+
end
|
59
|
+
|
60
|
+
def get_status(election_id)
|
61
|
+
get_status = Decidim::BulletinBoard::Authority::GetElectionStatus.new(election_id)
|
62
|
+
get_status.on(:ok) { |status| return status }
|
63
|
+
get_status.on(:error) { |error_message| raise StandardError, error_message }
|
64
|
+
get_status.call
|
40
65
|
end
|
41
66
|
|
42
67
|
private
|
@@ -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
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Decidim
|
4
|
+
module BulletinBoard
|
5
|
+
module Voter
|
6
|
+
# This command uses the GraphQL client to cast the vote.
|
7
|
+
class CastVote < Decidim::BulletinBoard::Command
|
8
|
+
# Public: Initializes the command.
|
9
|
+
#
|
10
|
+
# form - A form object with the params.
|
11
|
+
def initialize(election_id, voter_id, encrypted_vote)
|
12
|
+
@election_id = election_id
|
13
|
+
@voter_id = voter_id
|
14
|
+
@encrypted_vote = encrypted_vote
|
15
|
+
end
|
16
|
+
|
17
|
+
# Executes the command. Broadcasts these events:
|
18
|
+
#
|
19
|
+
# - :ok when everything is valid and the mutation operation is successful.
|
20
|
+
# - :error if the form wasn't valid or the mutation operation was not successful.
|
21
|
+
#
|
22
|
+
# Returns nothing.
|
23
|
+
def call
|
24
|
+
message_id = message_id(unique_election_id(election_id), "vote.cast", voter_id)
|
25
|
+
signed_data = sign_message(message_id, { content: encrypted_vote })
|
26
|
+
|
27
|
+
begin
|
28
|
+
response = client.query do
|
29
|
+
mutation do
|
30
|
+
vote(messageId: message_id, signedData: signed_data) do
|
31
|
+
pendingMessage do
|
32
|
+
status
|
33
|
+
end
|
34
|
+
error
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
return broadcast(:error, response.data.vote.error) if response.data.vote.error.present?
|
40
|
+
|
41
|
+
broadcast(:ok, response.data.vote.pending_message)
|
42
|
+
rescue Graphlient::Errors::FaradayServerError
|
43
|
+
broadcast(:error, "something went wrong")
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
attr_reader :election_id, :voter_id, :encrypted_vote
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
metadata
CHANGED
@@ -1,36 +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.0
|
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
|
-
name:
|
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
|
-
- - "
|
25
|
+
- - ">="
|
29
26
|
- !ruby/object:Gem::Version
|
30
|
-
version:
|
27
|
+
version: 5.0.0
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: activesupport
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
31
32
|
- - ">="
|
32
33
|
- !ruby/object:Gem::Version
|
33
|
-
version: 5.0.0
|
34
|
+
version: 5.0.0
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: 5.0.0
|
34
42
|
- !ruby/object:Gem::Dependency
|
35
43
|
name: byebug
|
36
44
|
requirement: !ruby/object:Gem::Requirement
|
@@ -63,16 +71,30 @@ dependencies:
|
|
63
71
|
name: jwt
|
64
72
|
requirement: !ruby/object:Gem::Requirement
|
65
73
|
requirements:
|
66
|
-
- - "
|
74
|
+
- - "~>"
|
67
75
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
76
|
+
version: 2.2.2
|
69
77
|
type: :runtime
|
70
78
|
prerelease: false
|
71
79
|
version_requirements: !ruby/object:Gem::Requirement
|
72
80
|
requirements:
|
73
|
-
- - "
|
81
|
+
- - "~>"
|
74
82
|
- !ruby/object:Gem::Version
|
75
|
-
version:
|
83
|
+
version: 2.2.2
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: wisper
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - "~>"
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: 2.0.0
|
91
|
+
type: :runtime
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - "~>"
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: 2.0.0
|
76
98
|
- !ruby/object:Gem::Dependency
|
77
99
|
name: rake
|
78
100
|
requirement: !ruby/object:Gem::Requirement
|
@@ -115,6 +137,20 @@ dependencies:
|
|
115
137
|
- - "~>"
|
116
138
|
- !ruby/object:Gem::Version
|
117
139
|
version: '3.6'
|
140
|
+
- !ruby/object:Gem::Dependency
|
141
|
+
name: wisper-rspec
|
142
|
+
requirement: !ruby/object:Gem::Requirement
|
143
|
+
requirements:
|
144
|
+
- - "~>"
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
version: 1.1.0
|
147
|
+
type: :development
|
148
|
+
prerelease: false
|
149
|
+
version_requirements: !ruby/object:Gem::Requirement
|
150
|
+
requirements:
|
151
|
+
- - "~>"
|
152
|
+
- !ruby/object:Gem::Version
|
153
|
+
version: 1.1.0
|
118
154
|
description: ''
|
119
155
|
email:
|
120
156
|
- david@codegram.com
|
@@ -128,6 +164,7 @@ files:
|
|
128
164
|
- ".rubocop.yml"
|
129
165
|
- ".ruby-version"
|
130
166
|
- ".travis.yml"
|
167
|
+
- CHANGELOG.md
|
131
168
|
- CODE_OF_CONDUCT.md
|
132
169
|
- Gemfile
|
133
170
|
- Gemfile.lock
|
@@ -137,16 +174,24 @@ files:
|
|
137
174
|
- bin/setup
|
138
175
|
- decidim-bulletin_board.gemspec
|
139
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
|
140
182
|
- lib/decidim/bulletin_board/client.rb
|
141
|
-
- lib/decidim/bulletin_board/
|
183
|
+
- lib/decidim/bulletin_board/command.rb
|
142
184
|
- lib/decidim/bulletin_board/graphql/client.rb
|
143
185
|
- lib/decidim/bulletin_board/jwk_utils.rb
|
186
|
+
- lib/decidim/bulletin_board/message_identifier.rb
|
144
187
|
- lib/decidim/bulletin_board/version.rb
|
188
|
+
- lib/decidim/bulletin_board/voter.rb
|
189
|
+
- lib/decidim/bulletin_board/voter/cast_vote.rb
|
145
190
|
homepage: https://github.com
|
146
191
|
licenses:
|
147
192
|
- AGPL-3.0
|
148
193
|
metadata: {}
|
149
|
-
post_install_message:
|
194
|
+
post_install_message:
|
150
195
|
rdoc_options: []
|
151
196
|
require_paths:
|
152
197
|
- lib
|
@@ -162,7 +207,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
162
207
|
version: '0'
|
163
208
|
requirements: []
|
164
209
|
rubygems_version: 3.0.3
|
165
|
-
signing_key:
|
210
|
+
signing_key:
|
166
211
|
specification_version: 4
|
167
212
|
summary: ''
|
168
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
|