Class: Moxml::Signature::Algorithms::CanonicalizationBase

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

Overview

Base class for canonicalization algorithms.

Subclasses declare identifier "http://..." and implement #engine (returning a Moxml::C14n::* walker). Canonicalizers operate on a Moxml::Node subtree and return UTF-8 octets.

Canonicalization algorithms can also be used as transforms per spec §6.6.1. The base class provides the #transform method that adapts the canonicalize interface to the transform pipeline.

Direct Known Subclasses

ExcC14n10, InclusiveC14n10, InclusiveC14n11

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier_uri: nil, with_comments: nil, inclusive_namespaces: [], context: nil, **_unused) ⇒ CanonicalizationBase

context: is required when this algorithm is used as a transform so that octet-stream input can be parsed with the same adapter the caller used for the rest of the document.



40
41
42
43
44
45
46
# File 'lib/moxml/signature/algorithms/canonicalization_base.rb', line 40

def initialize(identifier_uri: nil, with_comments: nil,
               inclusive_namespaces: [], context: nil, **_unused)
  @identifier_uri = identifier_uri
  @with_comments = with_comments.nil? ? uri_has_comments?(identifier_uri) : with_comments
  @inclusive_namespaces = inclusive_namespaces || []
  @context = context
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



34
35
36
# File 'lib/moxml/signature/algorithms/canonicalization_base.rb', line 34

def context
  @context
end

#identifier_uriObject (readonly)

Returns the value of attribute identifier_uri.



34
35
36
# File 'lib/moxml/signature/algorithms/canonicalization_base.rb', line 34

def identifier_uri
  @identifier_uri
end

#inclusive_namespacesObject (readonly)

Returns the value of attribute inclusive_namespaces.



34
35
36
# File 'lib/moxml/signature/algorithms/canonicalization_base.rb', line 34

def inclusive_namespaces
  @inclusive_namespaces
end

#with_commentsObject (readonly)

Returns the value of attribute with_comments.



34
35
36
# File 'lib/moxml/signature/algorithms/canonicalization_base.rb', line 34

def with_comments
  @with_comments
end

Class Method Details

.for_uri(uri, **opts) ⇒ Object



29
30
31
# File 'lib/moxml/signature/algorithms/canonicalization_base.rb', line 29

def for_uri(uri, **opts)
  new(identifier_uri: uri, **opts)
end

.identifier(uri) ⇒ Object



25
26
27
# File 'lib/moxml/signature/algorithms/canonicalization_base.rb', line 25

def identifier(uri)
  Algorithms.register(:canonicalization, uri, self)
end

.input_typeObject

Canonicalization presents the transform interface (spec §6.6.1). Input: octet stream or node-set. Output: octet stream.



21
# File 'lib/moxml/signature/algorithms/canonicalization_base.rb', line 21

def input_type; :nodeset; end

.output_typeObject



23
# File 'lib/moxml/signature/algorithms/canonicalization_base.rb', line 23

def output_type; :octets; end

Instance Method Details

#canonicalize(node) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/moxml/signature/algorithms/canonicalization_base.rb', line 48

def canonicalize(node)
  engine.canonicalize(
    node,
    with_comments: @with_comments,
    inclusive_namespaces: @inclusive_namespaces,
  )
end

#transform(input) ⇒ Object

Adapt to the transform interface (spec §6.6.1). Octet-stream input is parsed using the same Moxml::Context the caller used; this preserves the byte-exact invariant across adapters.



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/moxml/signature/algorithms/canonicalization_base.rb', line 59

def transform(input)
  return canonicalize(input) if input.is_a?(::Moxml::Node)

  if context.nil?
    raise TransformError,
          "canonicalization transform requires a context to parse " \
          "octet-stream input; none was provided"
  end

  parsed = context.parse(input.to_s)
  canonicalize(parsed.root)
end