decidim-bulletin_board 0.3.0 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,6 +7,7 @@ module Decidim
7
7
  class Client
8
8
  def self.client
9
9
  @client ||= Graphlient::Client.new(BulletinBoard.server,
10
+ schema_path: "lib/decidim/bulletin_board/graphql/bb_schema.json",
10
11
  headers: {
11
12
  "Authorization" => BulletinBoard.api_key
12
13
  })
@@ -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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Decidim
4
4
  module BulletinBoard
5
- VERSION = "0.3.0"
5
+ VERSION = "0.5.2"
6
6
  end
7
7
  end
@@ -1,4 +1,3 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "decidim/bulletin_board/voter/cast_vote"
4
- require "decidim/bulletin_board/voter/vote_form"
@@ -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(form)
13
- @form = form
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
- return broadcast(:error, form.errors.full_messages.join(". ")) unless form.valid?
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: args[:message_id], signedData: args[:signed_data]) do
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 :form
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.3.0
4
+ version: 0.5.2
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-10 00:00:00.000000000 Z
12
+ date: 2020-12-20 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.1
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.1
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.1
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.1
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: '0'
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: '0'
83
+ version: 2.2.2
96
84
  - !ruby/object:Gem::Dependency
97
85
  name: wisper
98
86
  requirement: !ruby/object:Gem::Requirement
@@ -186,20 +174,25 @@ files:
186
174
  - bin/setup
187
175
  - decidim-bulletin_board.gemspec
188
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
189
180
  - lib/decidim/bulletin_board/authority/get_election_status.rb
181
+ - lib/decidim/bulletin_board/authority/open_ballot_box.rb
190
182
  - lib/decidim/bulletin_board/client.rb
191
- - lib/decidim/bulletin_board/create_election.rb
183
+ - lib/decidim/bulletin_board/command.rb
184
+ - lib/decidim/bulletin_board/graphql/bb_schema.json
192
185
  - lib/decidim/bulletin_board/graphql/client.rb
193
186
  - lib/decidim/bulletin_board/jwk_utils.rb
187
+ - lib/decidim/bulletin_board/message_identifier.rb
194
188
  - lib/decidim/bulletin_board/version.rb
195
189
  - lib/decidim/bulletin_board/voter.rb
196
190
  - lib/decidim/bulletin_board/voter/cast_vote.rb
197
- - lib/decidim/bulletin_board/voter/vote_form.rb
198
191
  homepage: https://github.com
199
192
  licenses:
200
193
  - AGPL-3.0
201
194
  metadata: {}
202
- post_install_message:
195
+ post_install_message:
203
196
  rdoc_options: []
204
197
  require_paths:
205
198
  - lib
@@ -215,7 +208,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
215
208
  version: '0'
216
209
  requirements: []
217
210
  rubygems_version: 3.0.3
218
- signing_key:
211
+ signing_key:
219
212
  specification_version: 4
220
213
  summary: ''
221
214
  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,71 +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 metadata
33
- def signed_data
34
- @signed_data ||= bulletin_board_client.sign_data(message)
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
-
61
- def message
62
- {
63
- iat: Time.now.to_i,
64
- message_id: message_id,
65
- content: encrypted_vote
66
- }
67
- end
68
- end
69
- end
70
- end
71
- end