Class: Samlr::Fingerprint

Inherits:
Object show all
Defined in:
lib/samlr/fingerprint.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Fingerprint

Returns a new instance of Fingerprint.



5
6
7
8
9
10
11
# File 'lib/samlr/fingerprint.rb', line 5

def initialize(value)
  if value.is_a?(OpenSSL::X509::Certificate)
    @value = Fingerprint.x509(value)
  else
    @value = Fingerprint.normalize(value)
  end
end

Instance Attribute Details

#valueObject

Returns the value of attribute value.



3
4
5
# File 'lib/samlr/fingerprint.rb', line 3

def value
  @value
end

Class Method Details

.normalize(value) ⇒ Object

Converts a string to fingerprint normal form



40
41
42
# File 'lib/samlr/fingerprint.rb', line 40

def self.normalize(value)
  value.to_s.upcase.gsub(/[^A-F0-9]/, "").scan(/../).join(":")
end

.x509(certificate) ⇒ Object

Extracts a fingerprint for an x509 certificate



35
36
37
# File 'lib/samlr/fingerprint.rb', line 35

def self.x509(certificate)
  normalize(OpenSSL::Digest::SHA1.new.hexdigest(certificate.to_der))
end

Instance Method Details

#==(other) ⇒ Object

Fingerprints compare if their values are equal and not blank



14
15
16
# File 'lib/samlr/fingerprint.rb', line 14

def ==(other)
  other.is_a?(Fingerprint) && other.valid? && valid? && other.to_s == to_s
end

#compare!(other) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/samlr/fingerprint.rb', line 18

def compare!(other)
  if self != other
    raise FingerprintError.new("Fingerprint mismatch", "#{self} vs. #{other}")
  else
    true
  end
end

#to_sObject



30
31
32
# File 'lib/samlr/fingerprint.rb', line 30

def to_s
  value
end

#valid?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/samlr/fingerprint.rb', line 26

def valid?
  value =~ /([A-F0-9]:?)+/
end