Module: Spoiled

Defined in:
lib/spoiled.rb,
lib/spoiled/version.rb

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.verify(url, port = 443) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/spoiled.rb', line 9

def self.verify url, port = 443
  tcp_client = TCPSocket.new(url, 443)
  ssl_client = OpenSSL::SSL::SSLSocket.new(tcp_client)
  ssl_client.connect
  cert = OpenSSL::X509::Certificate.new(ssl_client.peer_cert)
  ssl_client.sysclose
  tcp_client.close

  certprops = OpenSSL::X509::Name.new(cert.issuer).to_a
  issuer = certprops.select { |name, data, type| name == "O" }.first[1]

  cn = cert.subject.to_s.gsub!(/.*?(?=CN=)/im, "").gsub("CN=", '')

  result = {
    url: url,
    port: port,
    valid_on: cert.not_before.to_date,
    valid_until: cert.not_after.to_date,
    issuer: issuer,
    time_remaining: ( cert.not_after.to_date - Date.today ).to_i.days,
    common_name: cn
  }
end