Module: Linzer::Common

Included in:
Signer, Verifier
Defined in:
lib/linzer/common.rb

Instance Method Summary collapse

Instance Method Details

#signature_base(message, components, parameters) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/linzer/common.rb', line 5

def signature_base(message, components, parameters)
  signature_base = components.each_with_object(+"") do |component, base|
    base << "\"#{component}\": #{message[component]}\n"
  end

  signature_params =
    Starry.serialize([Starry::InnerList.new(components, parameters)])

  signature_base << "\"@signature-params\": #{signature_params}"
  signature_base
end

#validate_components(message, components) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/linzer/common.rb', line 17

def validate_components(message, components)
  if components.include?("@signature-params")
    raise Error.new "Invalid component in signature input"
  end

  msg = "Cannot verify signature. Missing component in message: %s"
  components.each do |c|
    raise Error.new msg % "\"#{c}\"" unless message.field?(c)
  end

  validate_uniqueness components
end

#validate_uniqueness(components) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/linzer/common.rb', line 30

def validate_uniqueness(components)
  msg = "Invalid signature. Duplicated component in signature input."

  uniq_components =
    components
      .partition { |c| c.start_with?("@") }
      .flat_map
      .with_index do |group, idx|
        group
          .map  { |comp| Starry.parse_item(idx.zero? ? comp[1..] : comp) }
          .uniq { |comp| [comp.value, comp.parameters] }
      end

  raise Error.new msg if components.count != uniq_components.count
end