Class: Moxml::Signature::TransformPipeline

Inherits:
Object
  • Object
show all
Defined in:
lib/moxml/signature/transform_pipeline.rb

Overview

Shared transform-pipeline logic used by both Signer (signing flow, signature_element: nil) and Verifier (verification flow, with the containing Signature element so the Enveloped Signature transform can remove it from the digest input).

Each Transform in the chain is looked up via Algorithms.lookup(:transform, uri), with a fallback to Algorithms.lookup(:canonicalization, uri) per spec §6.6.1 (any canonicalization algorithm can be used as a transform).

Type coercion matches spec §4.4.3.2 reference processing model:

- octets → nodeset: parse as XML
- nodeset → octets: apply inclusive C14N 1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context:, signature_element: nil) ⇒ TransformPipeline

Returns a new instance of TransformPipeline.



20
21
22
23
# File 'lib/moxml/signature/transform_pipeline.rb', line 20

def initialize(context:, signature_element: nil)
  @context = context
  @signature_element = signature_element
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



18
19
20
# File 'lib/moxml/signature/transform_pipeline.rb', line 18

def context
  @context
end

#signature_elementObject (readonly)

Returns the value of attribute signature_element.



18
19
20
# File 'lib/moxml/signature/transform_pipeline.rb', line 18

def signature_element
  @signature_element
end

Instance Method Details

#apply(input, transforms_model) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/moxml/signature/transform_pipeline.rb', line 25

def apply(input, transforms_model)
  current = input
  current_type = type_of(current)
  transforms = transforms_from(transforms_model)

  transforms.each do |transform_model|
    algo_class = lookup(transform_model.algorithm)
    transform = algo_class.new(
      parameters: transform_model.parameters,
      context: context,
      signature_element: signature_element,
    )
    current_type, current = coerce(current, current_type,
                                   algo_class.input_type)
    current = transform.transform(current)
    current_type = algo_class.output_type
  end

  current
end

#to_octets(value, reference = nil) ⇒ Object

Final step: convert the pipeline output (which may still be a node) to octets suitable for digesting. Uses inclusive C14N 1.0 as the default mapping per spec §4.4.3.2.



49
50
51
52
53
54
55
# File 'lib/moxml/signature/transform_pipeline.rb', line 49

def to_octets(value, reference = nil)
  return value if value.is_a?(String)

  c14n_uri = canonicalization_from(reference) || DEFAULT_OCTET_C14N
  klass = Algorithms.lookup(:canonicalization, c14n_uri)
  klass.new(identifier_uri: c14n_uri).canonicalize(value)
end