Class: Passbook::PKPass

Inherits:
Object
  • Object
show all
Defined in:
lib/passbook/pkpass.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pass) ⇒ PKPass

Returns a new instance of PKPass.



10
11
12
13
# File 'lib/passbook/pkpass.rb', line 10

def initialize pass
  @pass      = pass
  @manifest_files     = []
end

Instance Attribute Details

#manifest_filesObject

Returns the value of attribute manifest_files.



8
9
10
# File 'lib/passbook/pkpass.rb', line 8

def manifest_files
  @manifest_files
end

#passObject

Returns the value of attribute pass.



8
9
10
# File 'lib/passbook/pkpass.rb', line 8

def pass
  @pass
end

Instance Method Details

#addFile(file) ⇒ Object



15
16
17
# File 'lib/passbook/pkpass.rb', line 15

def addFile file
  @manifest_files << file
end

#addFiles(files) ⇒ Object



19
20
21
# File 'lib/passbook/pkpass.rb', line 19

def addFiles files
  @manifest_files += files
end

#buildObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/passbook/pkpass.rb', line 28

def build
  manifest = createManifest

  # Check pass for necessary files and fields
  checkPass manifest

  # Create pass signature
  signature = createSignature manifest

  return [manifest, signature]
end

#createObject

Backward compatibility



41
42
43
# File 'lib/passbook/pkpass.rb', line 41

def create
  self.file.path
end

#createSignature(manifest) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/passbook/pkpass.rb', line 75

def createSignature manifest
  p12   = get_p12_cert_and_key
  wwdc  = OpenSSL::X509::Certificate.new File.read(Passbook.wwdc_cert)
  pk7   = OpenSSL::PKCS7.sign p12[:cert], p12[:key], manifest.to_s, [wwdc], OpenSSL::PKCS7::BINARY | OpenSSL::PKCS7::DETACHED
  data  = OpenSSL::PKCS7.write_smime pk7

  str_debut = "filename=\"smime.p7s\"\n\n"
  data = data[data.index(str_debut)+str_debut.length..data.length-1]
  str_end = "\n\n------"
  data = data[0..data.index(str_end)-1]

  return Base64.decode64(data)
end

#file(options = {}) ⇒ Object

Return a Tempfile containing our ZipStream



46
47
48
49
50
51
52
53
54
# File 'lib/passbook/pkpass.rb', line 46

def file(options = {})
  options[:file_name] ||= 'pass.pkpass'

  temp_file = Tempfile.new(options[:file_name])
  temp_file.write self.stream.string
  temp_file.close

  temp_file
end

#get_p12_cert_and_keyObject



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/passbook/pkpass.rb', line 63

def get_p12_cert_and_key
  key_hash = {}
  if Passbook.p12_key
    key_hash[:key] = OpenSSL::PKey::RSA.new File.read(Passbook.p12_key), Passbook.p12_password
    key_hash[:cert] = OpenSSL::X509::Certificate.new File.read(Passbook.p12_certificate)
  else
    p12 = OpenSSL::PKCS12.new File.read(Passbook.p12_cert), Passbook.p12_password
    key_hash[:key], key_hash[:cert] = p12.key, p12.certificate 
  end
  key_hash 
end

#json=(json) ⇒ Object

for backwards compatibility



24
25
26
# File 'lib/passbook/pkpass.rb', line 24

def json= json
  @pass = json
end

#streamObject

Return a ZipOutputStream



57
58
59
60
61
# File 'lib/passbook/pkpass.rb', line 57

def stream
  manifest, signature = build

  outputZip manifest, signature
end