Class: Rails::Auth::X509::Certificate

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/auth/x509/certificate.rb

Overview

X.509 client certificates obtained from HTTP requests

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(certificate) ⇒ Certificate

Returns a new instance of Certificate.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rails/auth/x509/certificate.rb', line 10

def initialize(certificate)
  unless certificate.is_a?(OpenSSL::X509::Certificate)
    raise TypeError, "expecting OpenSSL::X509::Certificate, got #{certificate.class}"
  end

  @certificate = certificate.freeze
  @subject = {}

  @certificate.subject.to_a.each do |name, data, _type|
    @subject[name.freeze] = data.freeze
  end

  @subject.freeze
end

Instance Attribute Details

#certificateObject (readonly)

Returns the value of attribute certificate.



8
9
10
# File 'lib/rails/auth/x509/certificate.rb', line 8

def certificate
  @certificate
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Compare ourself to another object by ensuring that it has the same type and that its certificate pem is the same as ours



51
52
53
# File 'lib/rails/auth/x509/certificate.rb', line 51

def ==(other)
  other.is_a?(self.class) && other.certificate.to_der == certificate.to_der
end

#[](component) ⇒ Object



25
26
27
# File 'lib/rails/auth/x509/certificate.rb', line 25

def [](component)
  @subject[component.to_s.upcase]
end

#attributesHash

Generates inspectable attributes for debugging

Returns:

  • (Hash)

    hash containing parts of the certificate subject (cn, ou)



42
43
44
45
46
47
# File 'lib/rails/auth/x509/certificate.rb', line 42

def attributes
  {
    cn: cn,
    ou: ou
  }
end

#cnObject Also known as: common_name



29
30
31
# File 'lib/rails/auth/x509/certificate.rb', line 29

def cn
  @subject["CN".freeze]
end

#ouObject Also known as: organizational_unit



34
35
36
# File 'lib/rails/auth/x509/certificate.rb', line 34

def ou
  @subject["OU".freeze]
end