Class: ParseClientCert

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

Overview

Object wrapper for data from client certificate

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ ParseClientCert

Returns a new instance of ParseClientCert.



9
10
11
12
13
# File 'lib/parse_client_cert.rb', line 9

def initialize(args)
  args.each do |k, v|
    instance_variable_set("@#{k}", v) unless v.nil?
  end
end

Instance Attribute Details

#cObject

Returns the value of attribute c.



7
8
9
# File 'lib/parse_client_cert.rb', line 7

def c
  @c
end

#cnObject

Returns the value of attribute cn.



7
8
9
# File 'lib/parse_client_cert.rb', line 7

def cn
  @cn
end

#emailaddressObject

Returns the value of attribute emailaddress.



7
8
9
# File 'lib/parse_client_cert.rb', line 7

def emailaddress
  @emailaddress
end

#locObject

Returns the value of attribute loc.



7
8
9
# File 'lib/parse_client_cert.rb', line 7

def loc
  @loc
end

#oObject

Returns the value of attribute o.



7
8
9
# File 'lib/parse_client_cert.rb', line 7

def o
  @o
end

#ouObject

Returns the value of attribute ou.



7
8
9
# File 'lib/parse_client_cert.rb', line 7

def ou
  @ou
end

#serialObject

Returns the value of attribute serial.



7
8
9
# File 'lib/parse_client_cert.rb', line 7

def serial
  @serial
end

#stObject

Returns the value of attribute st.



7
8
9
# File 'lib/parse_client_cert.rb', line 7

def st
  @st
end

Class Method Details

.from_apache(request) ⇒ Object



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

def from_apache(request)
  return nil if request.nil? or request.env.nil?
  pem = request.env['SSL_CLIENT_CERT']
  if pem.nil?
    nil
  else
    cert = OpenSSL::X509::Certificate.new(pem)
    process_certificate(cert)
  end
end

.from_nginx(request) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/parse_client_cert.rb', line 27

def from_nginx(request)
  return nil if request.nil? or request.env.nil?
  pem = request.env['HTTP_X_SSL_CERT']
  if pem.nil?
    nil
  else
    cert = OpenSSL::X509::Certificate.new(pem.tr("\t", "\n"))
    process_certificate(cert)
  end
end

.process_certificate(certificate) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/parse_client_cert.rb', line 38

def process_certificate(certificate)
  serial = certificate.serial.to_i.to_s(16)
  av_pairs = certificate.subject.to_a.map do |i|
    [i[0].downcase.to_sym, i[1]]
  end
  subject = Hash[*av_pairs.flatten].merge(serial: serial)
  ParseClientCert.new(subject)
end