From a3e917d787a82d14f5299ec449f853c734c4ac1b Mon Sep 17 00:00:00 2001 From: Thomas Lauro Date: Thu, 4 Jul 2013 02:24:10 +0400 Subject: [PATCH 1/2] Need review about this --- lib/passbook/pkpass.rb | 26 ++++++++++++++++++-------- spec/lib/passbook/pkpass_spec.rb | 5 +++-- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/lib/passbook/pkpass.rb b/lib/passbook/pkpass.rb index 4d44e0d..a7a1462 100644 --- a/lib/passbook/pkpass.rb +++ b/lib/passbook/pkpass.rb @@ -9,6 +9,18 @@ class PKPass TYPES = ['boarding-pass', 'coupon', 'event-ticket', 'store-card', 'generic'] + # Require fields, meta programming for accessor + REQUIRED_FIELDS = %w(passTypeIdentifier teamIdentifier serialNumber organizationName formatVersion description) + REQUIRED_FIELDS.each do |accessor| + class_eval %Q{ + def #{accessor}= value + json = JSON.parse(@pass) + json['#{accessor}'] = value + @pass = json.to_json + end + } + end + def initialize pass @pass = pass @manifest_files = [] @@ -69,9 +81,9 @@ def get_p12_cert_and_key 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 + key_hash[:key], key_hash[:cert] = p12.key, p12.certificate end - key_hash + key_hash end def createSignature manifest @@ -96,13 +108,11 @@ def checkPass manifest raise 'Icon@2x missing' unless manifest.include?('icon@2x.png') # Check for developer field in JSON - raise 'Pass Type Identifier missing' unless @pass.include?('passTypeIdentifier') - raise 'Team Identifier missing' unless @pass.include?('teamIdentifier') - raise 'Serial Number missing' unless @pass.include?('serialNumber') - raise 'Organization Name Identifier missing' unless @pass.include?('organizationName') - raise 'Format Version' unless @pass.include?('formatVersion') + REQUIRED_FIELDS.each do |require_field| + raise "#{require_field} mising" unless @pass.include?(require_field) + end + # Specific test raise 'Format Version should be a numeric' unless JSON.parse(@pass)['formatVersion'].is_a?(Numeric) - raise 'Description' unless @pass.include?('description') end def createManifest diff --git a/spec/lib/passbook/pkpass_spec.rb b/spec/lib/passbook/pkpass_spec.rb index 88ffe05..fbb7eed 100644 --- a/spec/lib/passbook/pkpass_spec.rb +++ b/spec/lib/passbook/pkpass_spec.rb @@ -57,7 +57,7 @@ File.should_receive(:read).with('my_p12_key').and_return 'my_p12_key_file' File.should_receive(:read).with('my_p12_certificate').and_return 'my_p12_certificate_file' OpenSSL::PKey::RSA.should_receive(:new).with('my_p12_key_file', 'password').and_return 'my_rsa_key' - OpenSSL::X509::Certificate.should_receive(:new).with('my_p12_certificate_file').and_return 'my_ssl_p12_cert' + OpenSSL::X509::Certificate.should_receive(:new).with('my_p12_certificate_file').and_return 'my_ssl_p12_cert' end subject {pass.get_p12_cert_and_key} @@ -89,6 +89,7 @@ let (:entries) {["pass.json", "manifest.json", "signature", "icon.png", "icon@2x.png", "logo.png", "logo@2x.png"]} before :each do + pass.passTypeIdentifier = 'test.pass.book' pass.addFiles ["#{base_path}/icon.png","#{base_path}/icon@2x.png","#{base_path}/logo.png","#{base_path}/logo@2x.png"] pass.should_receive(:createSignature).and_return('Signed by the Honey Badger') @file_entries = [] @@ -122,7 +123,7 @@ after do temp_file.delete - end + end end end From f53fc06433cf40a127715bca9f1e0a3e43ac03ff Mon Sep 17 00:00:00 2001 From: Thomas Lauro Date: Thu, 4 Jul 2013 02:30:07 +0400 Subject: [PATCH 2/2] Rename file and stream to to_file, to_stream and added valid? function --- lib/passbook/pkpass.rb | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/passbook/pkpass.rb b/lib/passbook/pkpass.rb index a7a1462..74405a2 100644 --- a/lib/passbook/pkpass.rb +++ b/lib/passbook/pkpass.rb @@ -56,8 +56,13 @@ def create self.file.path end - # Return a Tempfile containing our ZipStream + # Backward compatibility def file(options = {}) + to_file options + end + + # Return a Tempfile containing our ZipStream + def to_file options = {} options[:file_name] ||= 'pass.pkpass' temp_file = Tempfile.new(options[:file_name]) @@ -67,8 +72,13 @@ def file(options = {}) temp_file end - # Return a ZipOutputStream + # Backward compatibility def stream + to_stream + end + + # Return a ZipOutputStream + def to_stream manifest, signature = build outputZip manifest, signature @@ -100,6 +110,16 @@ def createSignature manifest return Base64.decode64(data) end + def valid? + manifest = createManifest + begin + checkPass manifest + rescue + return false + end + return true + end + private def checkPass manifest