Module: CsrPeek::Names

Defined in:
lib/csr_peek/names.rb

Overview

Helpers for turning an OpenSSL::X509::Name (subject, issuer) into plain Ruby. Subject Alternative Names live in CsrPeek::Extensions, not here.

Class Method Summary collapse

Class Method Details

.components(name) ⇒ Object

The ordered list of [oid, value] pairs from a name, preserving repeats. Deep-frozen for the same reason as #subject_to_h.



31
32
33
34
35
36
37
# File 'lib/csr_peek/names.rb', line 31

def components(name)
  return [] if name.nil?

  name.to_a.map { |oid, value, _type| [oid.freeze, value.freeze].freeze }.freeze
rescue
  []
end

.subject_to_h(name) ⇒ Object

Convert an OpenSSL::X509::Name to a hash of { "CN" => ..., "O" => ... }.

Keys are OpenSSL's short names for known OIDs ("CN", "O", "OU", ...) and the dotted OID string for anything it does not recognize. When an attribute repeats (multiple OU, say), the FIRST value wins; the full ordered list, repeats included, is available via #components. Never raises. Values are frozen (they come from OpenSSL, not from frozen literals) so the immutable value object that holds this hash is immutable all the way down.



19
20
21
22
23
24
25
26
27
# File 'lib/csr_peek/names.rb', line 19

def subject_to_h(name)
  return {} if name.nil?

  name.to_a.each_with_object({}) do |(oid, value, _type), acc|
    acc[oid.freeze] ||= value.freeze
  end.freeze
rescue
  {}
end