Class: Akami::WSSE::Signature

Inherits:
Object
  • Object
show all
Includes:
C14nHelper, XPathHelper
Defined in:
lib/akami/wsse/signature.rb

Defined Under Namespace

Classes: MissingCertificate

Constant Summary collapse

ExclusiveXMLCanonicalizationAlgorithm =
"http://www.w3.org/2001/10/xml-exc-c14n#".freeze
RSASHA1SignatureAlgorithm =
"http://www.w3.org/2000/09/xmldsig#rsa-sha1".freeze
SHA1DigestAlgorithm =
"http://www.w3.org/2000/09/xmldsig#sha1".freeze
X509v3ValueType =
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3".freeze
Base64EncodingType =
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary".freeze
SignatureNamespace =
"http://www.w3.org/2000/09/xmldsig#".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from C14nHelper

#canonicalize

Methods included from XPathHelper

#at_xpath, #local_name_xpath, #xpath

Constructor Details

#initialize(certs = Certs.new, options = {}) ⇒ Signature

Returns a new instance of Signature.



38
39
40
41
42
43
# File 'lib/akami/wsse/signature.rb', line 38

def initialize(certs = Certs.new, options = {})
  @certs = certs
  @timestamp = options[:timestamp] || false
  @created_at = options[:created_at]
  @expires_at = options[:expires_at]
end

Instance Attribute Details

#certsObject

For a Savon::WSSE::Certs object. To hold the certs we need to sign.



13
14
15
# File 'lib/akami/wsse/signature.rb', line 13

def certs
  @certs
end

#created_atObject

Wheater to sign the timestamp or not ant their time



16
17
18
# File 'lib/akami/wsse/signature.rb', line 16

def created_at
  @created_at
end

#expires_atObject

Wheater to sign the timestamp or not ant their time



16
17
18
# File 'lib/akami/wsse/signature.rb', line 16

def expires_at
  @expires_at
end

#timestampObject

Wheater to sign the timestamp or not ant their time



16
17
18
# File 'lib/akami/wsse/signature.rb', line 16

def timestamp
  @timestamp
end

Instance Method Details

#body_attributesObject



83
84
85
86
87
88
# File 'lib/akami/wsse/signature.rb', line 83

def body_attributes
  {
    "xmlns:wsu" => Akami::WSSE::WSU_NAMESPACE,
    "wsu:Id" => body_id
  }
end

#body_idObject



55
56
57
# File 'lib/akami/wsse/signature.rb', line 55

def body_id
  @body_id ||= "Body-#{uid}".freeze
end

#documentObject

Without a document, the document cannot be signed. Generate the document once, and then set document and recall #to_token



20
21
22
23
# File 'lib/akami/wsse/signature.rb', line 20

def document
  return nil if @document.nil?
  @document.to_xml(save_with: Nokogiri::XML::Node::SaveOptions::AS_XML)
end

#document=(document) ⇒ Object



25
26
27
# File 'lib/akami/wsse/signature.rb', line 25

def document=(document)
  @document = Nokogiri::XML(document)
end

#have_document?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/akami/wsse/signature.rb', line 45

def have_document?
  !!document
end

#nowObject

Cache "now" so that digests match... TODO: figure out how we might want to expire this cache...



51
52
53
# File 'lib/akami/wsse/signature.rb', line 51

def now
  @now ||= Time.now
end

#security_token_idObject



79
80
81
# File 'lib/akami/wsse/signature.rb', line 79

def security_token_id
  @security_token_id ||= "SecurityToken-#{uid}".freeze
end

#timestamp_attributesObject



90
91
92
93
94
95
# File 'lib/akami/wsse/signature.rb', line 90

def timestamp_attributes
  {
    "xmlns:wsu" => Akami::WSSE::WSU_NAMESPACE,
    "wsu:Id" => timestamp_id
  }
end

#timestamp_idObject



59
60
61
# File 'lib/akami/wsse/signature.rb', line 59

def timestamp_id
  @timestamp_id ||= "TS-#{uid}".freeze
end

#to_tokenObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/akami/wsse/signature.rb', line 97

def to_token
  return {} unless have_document?

  sig = signed_info.merge(key_info).merge(signature_value)
  sig[:order!] = []
  ["SignedInfo", "SignatureValue", "KeyInfo"].each do |key|
    sig[:order!] << key if sig[key]
  end

  token = {
    "Signature" => sig,
    :attributes! => {"Signature" => {"xmlns" => SignatureNamespace}}
  }

  Akami::HashHelper.deep_merge!(token, binary_security_token) if certs.cert

  token[:order!] = []
  ["wsse:BinarySecurityToken", "Signature"].each do |key|
    token[:order!] << key if token[key]
  end

  token
end

#wsu_timestamp_hashObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/akami/wsse/signature.rb', line 63

def wsu_timestamp_hash
  return {} unless timestamp
  {
    "wsu:Timestamp" => {
      "wsu:Created" => (@created_at ||= Time.now).utc.xmlschema,
      "wsu:Expires" => (@expires_at ||= created_at + 60).utc.xmlschema
    },
    :attributes! => {
      "wsu:Timestamp" => {
        "wsu:Id" => timestamp_id,
        "xmlns:wsu" => WSU_NAMESPACE
      }
    }
  }
end