Class: PEM::CertManager

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cert_fileObject

Returns the value of attribute cert_file


4
5
6
# File 'lib/pem/cert_manager.rb', line 4

def cert_file
  @cert_file
end

#certificate_typeObject

Returns the value of attribute certificate_type


4
5
6
# File 'lib/pem/cert_manager.rb', line 4

def certificate_type
  @certificate_type
end

#passphraseObject

Returns the value of attribute passphrase


4
5
6
# File 'lib/pem/cert_manager.rb', line 4

def passphrase
  @passphrase
end

#pem_fileObject

Returns the value of attribute pem_file


4
5
6
# File 'lib/pem/cert_manager.rb', line 4

def pem_file
  @pem_file
end

#rsa_fileObject

Returns the value of attribute rsa_file


4
5
6
# File 'lib/pem/cert_manager.rb', line 4

def rsa_file
  @rsa_file
end

Instance Method Details

#p12_certificateObject


45
46
47
# File 'lib/pem/cert_manager.rb', line 45

def p12_certificate
  OpenSSL::PKCS12.create(passphrase, certificate_type, private_key, x509_certificate)
end

#pem_certificateObject


49
50
51
# File 'lib/pem/cert_manager.rb', line 49

def pem_certificate
  x509_certificate.to_pem + private_key.to_pem
end

#private_keyObject


37
38
39
# File 'lib/pem/cert_manager.rb', line 37

def private_key
  OpenSSL::PKey::RSA.new(File.read(rsa_file))
end

#runObject

Download the cert, do all kinds of Keychain related things


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pem/cert_manager.rb', line 7

def run
  # Keychain (security) documentation: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/security.1.html
  # Old project, which might be helpful: https://github.com/jprichardson/keychain_manager

  Helper.log.info "Refreshing push notification profiles for app '#{PEM.config[:app_identifier]}'"

  dev = PEM::DeveloperCenter.new

  self.cert_file = dev.fetch_cer_file
  if self.cert_file
    self.rsa_file = File.join(TMP_FOLDER, 'private_key.key')
    self.certificate_type = (PEM.config[:development] ? 'development' : 'production')
    self.pem_file = File.join(TMP_FOLDER, "#{certificate_type}_#{PEM.config[:app_identifier]}.pem")
    self.passphrase = PEM.config[:p12_password] || ''

    File.write(pem_file, pem_certificate)

    # Generate p12 file as well
    if PEM.config[:generate_p12]
      output = "#{certificate_type}_#{PEM.config[:app_identifier]}.p12"
      File.write(output, p12_certificate.to_der)
      puts output.green
    end

    return pem_file, rsa_file
  else
    return nil, nil
  end
end

#x509_certificateObject


41
42
43
# File 'lib/pem/cert_manager.rb', line 41

def x509_certificate
  OpenSSL::X509::Certificate.new(File.read(cert_file))
end