Class: Lowdown::Certificate

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

Overview

This class is a wrapper around a certificate/key pair that returns values used by Lowdown.

Instance Attribute Summary collapse

Constructor Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(certificate, key = nil) ⇒ Certificate



57
58
59
# File 'lib/lowdown/certificate.rb', line 57

def initialize(certificate, key = nil)
  @key, @certificate = key, certificate
end

Instance Attribute Details

#certificateOpenSSL::X509::Certificate (readonly)



66
67
68
# File 'lib/lowdown/certificate.rb', line 66

def certificate
  @certificate
end

#keyOpenSSL::PKey::RSA? (readonly)



71
72
73
# File 'lib/lowdown/certificate.rb', line 71

def key
  @key
end

Class Method Details

.certificate(certificate_or_data) ⇒ Certificate



16
17
18
19
20
21
22
# File 'lib/lowdown/certificate.rb', line 16

def self.certificate(certificate_or_data)
  if certificate_or_data.is_a?(Certificate)
    certificate_or_data
  else
    from_pem_data(certificate_or_data)
  end
end

.from_pem_data(data, passphrase = nil) ⇒ Certificate

A convenience method that initializes a Certificate from PEM data.



34
35
36
37
38
# File 'lib/lowdown/certificate.rb', line 34

def self.from_pem_data(data, passphrase = nil)
  key = OpenSSL::PKey::RSA.new(data, passphrase)
  certificate = OpenSSL::X509::Certificate.new(data)
  new(certificate, key)
end

.from_ssl_context(context) ⇒ Certificate

A convenience method that initializes a Certificate with the certificate and key from a SSL context object.



47
48
49
# File 'lib/lowdown/certificate.rb', line 47

def self.from_ssl_context(context)
  new(context.cert, context.key)
end

Instance Method Details

#==(other) ⇒ Boolean



85
86
87
# File 'lib/lowdown/certificate.rb', line 85

def ==(other)
  other.is_a?(Certificate) && other.to_pem == to_pem
end

#app_bundle_idString



140
141
142
# File 'lib/lowdown/certificate.rb', line 140

def app_bundle_id
  @certificate.subject.to_a.find { |key, *_| key == "UID" }[1]
end

#development?Boolean



111
112
113
# File 'lib/lowdown/certificate.rb', line 111

def development?
  !extension(DEVELOPMENT_ENV_EXTENSION).nil?
end

#production?Boolean



118
119
120
# File 'lib/lowdown/certificate.rb', line 118

def production?
  !extension(PRODUCTION_ENV_EXTENSION).nil?
end

#ssl_contextOpenSSL::SSL::SSLContext



92
93
94
95
96
97
# File 'lib/lowdown/certificate.rb', line 92

def ssl_context
  @ssl_context ||= OpenSSL::SSL::SSLContext.new.tap do |context|
    context.key = @key
    context.cert = @certificate
  end
end

#to_pemString



78
79
80
# File 'lib/lowdown/certificate.rb', line 78

def to_pem
  [@key, @certificate].compact.map(&:to_pem).join("\n")
end

#topicsArray<String>

Returns a list of ‘topics’ that the certificate supports.



127
128
129
130
131
132
133
134
135
# File 'lib/lowdown/certificate.rb', line 127

def topics
  if universal?
    ext = extension(UNIVERSAL_CERTIFICATE_EXTENSION)
    seq = OpenSSL::ASN1.decode(OpenSSL::ASN1.decode(ext.to_der).value[1].value)
    seq.select.with_index { |_, index| index.even? }.map(&:value)
  else
    [app_bundle_id]
  end
end

#universal?Boolean

Returns whether or not the certificate is a Universal Certificate.



104
105
106
# File 'lib/lowdown/certificate.rb', line 104

def universal?
  !extension(UNIVERSAL_CERTIFICATE_EXTENSION).nil?
end