Class: Lowdown::Certificate
- Inherits:
-
Object
- Object
- Lowdown::Certificate
- 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
-
#certificate ⇒ OpenSSL::X509::Certificate
readonly
The Apple Push Notification certificate.
-
#key ⇒ OpenSSL::PKey::RSA?
readonly
The private key that belongs to the certificate.
Constructor Summary collapse
-
.certificate(certificate_or_data) ⇒ Certificate
Either the originally passed in Certificate or a new Certificate.
-
.from_pem_data(data, passphrase = nil) ⇒ Certificate
A convenience method that initializes a Certificate from PEM data.
-
.from_ssl_context(context) ⇒ Certificate
A convenience method that initializes a Certificate with the certificate and key from a SSL context object.
-
#initialize(certificate, key = nil) ⇒ Certificate
constructor
A new instance of Certificate.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Whether or not this Certificate is equal in contents to another Certificate.
-
#app_bundle_id ⇒ String
The App ID / app’s Bundle ID that this certificate is for.
-
#development? ⇒ Boolean
Whether or not the certificate supports the development (sandbox) environment (for development builds).
-
#production? ⇒ Boolean
Whether or not the certificate supports the production environment (for Testflight & App Store builds).
-
#ssl_context ⇒ OpenSSL::SSL::SSLContext
A SSL context, configured with the certificate/key pair, which is used to connect to the APN service.
-
#to_pem ⇒ String
The certificate/key pair encoded as PEM data.
-
#topics ⇒ Array<String>
A list of ‘topics’ that the certificate supports.
-
#universal? ⇒ Boolean
Whether or not the certificate is a Universal Certificate.
Constructor Details
#initialize(certificate, key = nil) ⇒ Certificate
Returns a new instance of Certificate.
57 58 59 |
# File 'lib/lowdown/certificate.rb', line 57 def initialize(certificate, key = nil) @key, @certificate = key, certificate end |
Instance Attribute Details
#certificate ⇒ OpenSSL::X509::Certificate (readonly)
Returns the Apple Push Notification certificate.
66 67 68 |
# File 'lib/lowdown/certificate.rb', line 66 def certificate @certificate end |
#key ⇒ OpenSSL::PKey::RSA? (readonly)
Returns the private key that belongs to the certificate.
71 72 73 |
# File 'lib/lowdown/certificate.rb', line 71 def key @key end |
Class Method Details
.certificate(certificate_or_data) ⇒ Certificate
Returns either the originally passed in Certificate or a new 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
Returns whether or not this Certificate is equal in contents to another Certificate.
85 86 87 |
# File 'lib/lowdown/certificate.rb', line 85 def ==(other) other.is_a?(Certificate) && other.to_pem == to_pem end |
#app_bundle_id ⇒ String
Returns the App ID / app’s Bundle ID that this certificate is for.
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
Returns whether or not the certificate supports the development (sandbox) environment (for development builds).
111 112 113 |
# File 'lib/lowdown/certificate.rb', line 111 def development? !extension(DEVELOPMENT_ENV_EXTENSION).nil? end |
#production? ⇒ Boolean
Returns whether or not the certificate supports the production environment (for Testflight & App Store builds).
118 119 120 |
# File 'lib/lowdown/certificate.rb', line 118 def production? !extension(PRODUCTION_ENV_EXTENSION).nil? end |
#ssl_context ⇒ OpenSSL::SSL::SSLContext
Returns a SSL context, configured with the certificate/key pair, which is used to connect to the APN service.
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_pem ⇒ String
Returns the certificate/key pair encoded as PEM data. Only used for testing.
78 79 80 |
# File 'lib/lowdown/certificate.rb', line 78 def to_pem [@key, @certificate].compact.map(&:to_pem).join("\n") end |
#topics ⇒ Array<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 |