Class: SAML2::Key

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

Defined Under Namespace

Modules: Type Classes: EncryptionMethod

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x509, use = nil, encryption_methods = []) ⇒ Key

Returns a new instance of Key.



38
39
40
# File 'lib/saml2/key.rb', line 38

def initialize(x509, use = nil, encryption_methods = [])
  @use, @x509, @encryption_methods = use, x509.gsub(/\w*-+(BEGIN|END) CERTIFICATE-+\w*/, "").strip, encryption_methods
end

Instance Attribute Details

#encryption_methodsObject

Returns the value of attribute encryption_methods.



28
29
30
# File 'lib/saml2/key.rb', line 28

def encryption_methods
  @encryption_methods
end

#useObject

Returns the value of attribute use.



28
29
30
# File 'lib/saml2/key.rb', line 28

def use
  @use
end

#x509Object

Returns the value of attribute x509.



28
29
30
# File 'lib/saml2/key.rb', line 28

def x509
  @x509
end

Class Method Details

.from_xml(node) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/saml2/key.rb', line 30

def self.from_xml(node)
  return nil unless node

  x509 = node.at_xpath('dsig:KeyInfo/dsig:X509Data/dsig:X509Certificate', Namespaces::ALL)
  methods = node.xpath('xenc:EncryptionMethod', Namespaces::ALL)
  new(x509 && x509.content.strip, node['use'], methods.map { |m| m['Algorithm'] })
end

Instance Method Details

#build(builder) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/saml2/key.rb', line 58

def build(builder)
  builder['md'].KeyDescriptor do |key_descriptor|
    key_descriptor.parent['use'] = use if use
    key_descriptor['dsig'].KeyInfo do |key_info|
      key_info['dsig'].X509Data do |x509_data|
        x509_data['dsig'].X509Certificate(x509)
      end
    end
    encryption_methods.each do |method|
      method.build(key_descriptor)
    end
  end
end

#certificateObject



50
51
52
# File 'lib/saml2/key.rb', line 50

def certificate
  @certificate ||= OpenSSL::X509::Certificate.new(Base64.decode64(x509))
end

#encryption?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/saml2/key.rb', line 42

def encryption?
  use.nil? || use == Type::ENCRYPTION
end

#fingerprintObject



54
55
56
# File 'lib/saml2/key.rb', line 54

def fingerprint
  @fingerprint ||= Digest::SHA1.hexdigest(certificate.to_der).gsub(/(\h{2})(?=\h)/, '\1:')
end

#signing?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/saml2/key.rb', line 46

def signing?
  use.nil? || use == Type::SIGNING
end