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

Returns a new instance of Certificate.

Parameters:

  • certificate (OpenSSL::X509::Certificate)

    the Apple Push Notification certificate.

  • key (OpenSSL::PKey::RSA) (defaults to: nil)

    the private key that belongs to the 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)

Returns the Apple Push Notification certificate.

Returns:

  • (OpenSSL::X509::Certificate)

    the Apple Push Notification certificate.



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

def certificate
  @certificate
end

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

Returns the private key that belongs to the certificate.

Returns:

  • (OpenSSL::PKey::RSA, nil)

    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.

Parameters:

  • certificate_or_data (Certificate, String)

    a configured Certificate or PEM data to construct a Certificate from.

Returns:

  • (Certificate)

    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.

Parameters:

  • data (String)

    the PEM encoded certificate/key pair data.

  • passphrase (String) (defaults to: nil)

    a passphrase required to decrypt the PEM data.

Returns:



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.

Parameters:

  • context (OpenSSL::SSL::SSLContext)

    the context from which to initialize a Certificate.

Returns:



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.

Returns:

  • (Boolean)

    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_idString

Returns the App ID / app’s Bundle ID that this certificate is for.

Returns:

  • (String)

    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).

Returns:

  • (Boolean)

    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).

Returns:

  • (Boolean)

    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_contextOpenSSL::SSL::SSLContext

Returns a SSL context, configured with the certificate/key pair, which is used to connect to the APN service.

Returns:

  • (OpenSSL::SSL::SSLContext)

    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_pemString

Returns the certificate/key pair encoded as PEM data. Only used for testing.

Returns:

  • (String)

    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

#topicsArray<String>

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

Returns:

  • (Array<String>)

    a list of ‘topics’ that the certificate supports.

See Also:



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