Class: Nokogiri::XML::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/xmlsec.rb

Instance Method Summary collapse

Instance Method Details

#decrypt!(opts) ⇒ Object

Decrypts the current document, then returns it.

Examples:

# decrypt with a specific private key
doc.decrypt! key: 'private-key'


96
97
98
99
100
101
102
103
# File 'lib/xmlsec.rb', line 96

def decrypt! opts
  if opts[:key]
    decrypt_with_key opts[:name].to_s, opts[:key]
  else
    raise 'inadequate options specified for decryption'
  end
  self
end

#encrypt!(opts) ⇒ Object

Encrypts the current document, then returns it.

Examples:

# encrypt with a public key and optional key name
doc.encrypt! key: 'public-key', name: 'name'


80
81
82
83
84
85
86
87
# File 'lib/xmlsec.rb', line 80

def encrypt! opts
  if opts[:key]
    encrypt_with_key opts[:name].to_s, opts[:key], opts.select { |key, _| key != :key && key != :name }
  else
    raise "public :key is required for encryption"
  end
  self
end

#sign!(opts) ⇒ Object

Signs this document, and then returns it.

Examples:

doc.sign! key: 'rsa-private-key'
doc.sign! key: 'rsa-private-key', name: 'key-name'
doc.sign! cert: 'x509 certificate', key: 'cert private key'
doc.sign! cert: 'x509 certificate', key: 'cert private key',
          name: 'key-name'


15
16
17
18
19
20
21
22
23
24
25
# File 'lib/xmlsec.rb', line 15

def sign! opts
  if opts.has_key? :cert
    raise "need a private :key" unless opts[:key]
    sign_with_certificate opts
  elsif opts[:key]
    sign_with_key opts
  else
    raise "No private :key was given"
  end
  self
end

#verify_signatureObject

Attempts to verify the signature of this document using only certificates installed on the system. This is equivalent to calling ‘verify_with certificates: []` (that is, an empty array).



69
70
71
# File 'lib/xmlsec.rb', line 69

def verify_signature
  verify_with_certificates []
end

#verify_with(opts_or_keys) ⇒ Object

Verifies the signature on the current document.

Returns ‘true` if the signature is valid, `false` otherwise.

Examples:

# Try to validate with the given public or private key
doc.verify_with key: 'rsa-key'

# Try to validate with a set of keys. It will try to match
# based on the contents of the `KeyName` element.
doc.verify_with({
  'key-name'         => 'x509 certificate',
  'another-key-name' => 'rsa-public-key'
})

# Try to validate with a trusted certificate
doc.verify_with(x509: 'certificate')

# Try to validate with a set of certificates, any one of which
# can match
doc.verify_with(x509: ['cert1', 'cert2'])

You can also use ‘:cert` or `:certificate` or `:certs` or `:certificates` as aliases for `:x509`.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/xmlsec.rb', line 53

def verify_with opts_or_keys
  if (certs = opts_or_keys[:cert]) ||
     (certs = opts_or_keys[:certs])
    certs = [certs] unless certs.kind_of?(Array)
    verify_with_certificates certs
  elsif opts_or_keys[:key]
    verify_with_rsa_key opts_or_keys[:key]
  else
    verify_with_named_keys opts_or_keys
  end
end