apple_receipt 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a381d5ac7037e9d434c0e0af717672f32d84f157b1086753aaf93e01951a2b42
4
+ data.tar.gz: f2722e6a2b589703f451035b4a34c2aa97aedbaa469e362443448c50d7bfcb3c
5
+ SHA512:
6
+ metadata.gz: e4f99e107fd8623e73bcc07ce0e095f6c1c44c345207355f915d0e49d31957325fb838869b5d4176907e23307901431be6d28e9ead4b7c81fde6003ee6bad39d
7
+ data.tar.gz: 5be1092bdcc3577e4d581284d6c998c8293e02aab9632fea8dee360c9c6b442e4bf6bcb7be1852c0a83534c86a97399ffac97797f0e312e4ec6cab0db6f87f57
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
10
+ *.gem
data/.rubocop.yml ADDED
@@ -0,0 +1,2 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ before_install: gem install bundler -v 1.16.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at info@koenrouwhorst.nl. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Koen Rouwhorst
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # AppleReceipt
2
+
3
+ [![Build Status](https://travis-ci.org/koenrh/apple_receipt.svg?branch=master)](https://travis-ci.org/koenrh/apple_receipt)
4
+
5
+ This gem allows you to to locally/cryptographically verify Apple receipts.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'apple_receipt'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ gem install apple_receipt
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ require 'apple_receipt'
27
+
28
+ # check validity (certificate chain, and signature)
29
+ receipt_raw = File.read('./receipt.txt')
30
+ receipt = AppleReceipt::Receipt.new(receipt_raw)
31
+ receipt.valid?
32
+ # => true
33
+
34
+ # read purchase info
35
+ receipt.purchase_info
36
+ # => {"original-purchase-date-pst"=>"2017-12-23 09:03:53 America/Los_Angeles",
37
+ # "quantity"=>"1",
38
+ # "unique-vendor-identifier"=>"D895D8DB-AEDF-4530-B7E5-E0C9A9A394B6",
39
+ # "original-purchase-date-ms"=>"1514048633000",
40
+ # "expires-date-formatted"=>"2018-01-23 17:03:44 Etc/GMT",
41
+ # "is-in-intro-offer-period"=>"false",
42
+ # "purchase-date-ms"=>"1514048624000",
43
+ # "expires-date-formatted-pst"=>"2018-01-23 09:03:44 America/Los_Angeles",
44
+ # "is-trial-period"=>"false",
45
+ # "item-id"=>"1190360447",
46
+ # "unique-identifier"=>"fed543dc24065fa2ab23ef08b0b44c0a0c9ed375",
47
+ # "original-transaction-id"=>"160000408504141",
48
+ # "expires-date"=>"1516727024000",
49
+ # "app-item-id"=>"947936149",
50
+ # "transaction-id"=>"160000408504141",
51
+ # "bvrs"=>"7000",
52
+ # "web-order-line-item-id"=>"160000091314729",
53
+ # "version-external-identifier"=>"825366855",
54
+ # "bid"=>"com.foo.bar",
55
+ # "product-id"=>"com.foo.bar.monthly",
56
+ # "purchase-date"=>"2017-12-23 17:03:44 Etc/GMT",
57
+ # "purchase-date-pst"=>"2017-12-23 09:03:44 America/Los_Angeles",
58
+ # "original-purchase-date"=>"2017-12-23 17:03:53 Etc/GMT"}
59
+ ```
60
+
61
+ ## Contributing
62
+
63
+ Bug reports and pull requests are welcome on [GitHub](https://github.com/koenrh/apple_receipt).
64
+ This project is intended to be a safe, welcoming space for collaboration, and
65
+ contributors are expected to adhere to the [Contributor Covenant](https://www.contributor-covenant.org)
66
+ code of conduct.
67
+
68
+ ## License
69
+
70
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rubocop/rake_task'
5
+ require 'rake/testtask'
6
+
7
+ RuboCop::RakeTask.new
8
+
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'spec'
11
+ t.libs << 'lib'
12
+ t.test_files = FileList['spec/**/*_spec.rb']
13
+ end
14
+
15
+ task default: %i[rubocop test]
@@ -0,0 +1,37 @@
1
+
2
+ # frozen_string_literal: true
3
+
4
+ lib = File.expand_path('../lib', __FILE__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+ require 'apple_receipt/version'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = 'apple_receipt'
10
+ spec.version = AppleReceipt::VERSION
11
+ spec.authors = ['Koen Rouwhorst']
12
+ spec.email = ['koenrh@blendle.com']
13
+
14
+ spec.summary = 'Local Apple receipt validation.'
15
+ # spec.description = %q{TODO: Write a longer description or delete this li
16
+ spec.homepage = 'https://www.blendle.com'
17
+ spec.license = 'MIT'
18
+
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
21
+ else
22
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
23
+ 'public gem pushes.'
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = 'exe'
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ['lib']
32
+
33
+ spec.add_development_dependency 'bundler', '~> 1.16'
34
+ spec.add_development_dependency 'minitest', '~> 5.0'
35
+ spec.add_development_dependency 'rake', '~> 10.0'
36
+ spec.add_development_dependency 'rubocop', '~> 0.50'
37
+ end
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'apple_receipt'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
Binary file
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apple_receipt/receipt'
4
+ require 'apple_receipt/version'
5
+
6
+ # AppleReceipt is the main module
7
+ module AppleReceipt
8
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module AppleReceipt
6
+ # NextStepParser parses NextSTEP Plist data structures.
7
+ module NextStepParser
8
+ module_function
9
+
10
+ def parse(input)
11
+ # Transform JSON-like NextSTEP PList data into JSON
12
+ raw_json = input.gsub(/;\n\t/, ",\n\t")
13
+ .gsub(/\ =/, ':')
14
+ .gsub(/;\n/, '')
15
+ JSON.parse(raw_json)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'base64'
4
+
5
+ require 'apple_receipt/next_step_parser'
6
+ require 'apple_receipt/receipt_parser'
7
+ require 'apple_receipt/validator'
8
+
9
+ module AppleReceipt
10
+ # Receipt represents an Apple receipt.
11
+ class Receipt
12
+ def initialize(raw_receipt)
13
+ receipt_decoded = Base64.decode64(raw_receipt)
14
+ @version, @signature, @cert, @data = ReceiptParser.parse(receipt_decoded)
15
+ end
16
+
17
+ def purchase_info
18
+ NextStepParser.parse(data)
19
+ end
20
+
21
+ def valid?
22
+ Validator.new(self).valid?
23
+ end
24
+
25
+ attr_reader :version, :signature, :cert, :data
26
+ end
27
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'base64'
4
+ require 'openssl'
5
+ require 'json'
6
+
7
+ require 'apple_receipt/next_step_parser'
8
+
9
+ module AppleReceipt
10
+ # ReceiptParser contains helper methods to parse receipt data structures.
11
+ module ReceiptParser
12
+ module_function
13
+
14
+ def bla(input)
15
+ receipt_hash = NextStepParser.parse(input)
16
+ signature_decoded = Base64.decode64(receipt_hash['signature'])
17
+ data = Base64.decode64(receipt_hash['purchase-info'])
18
+
19
+ sig = StringIO.new(signature_decoded)
20
+ [sig, data]
21
+ end
22
+
23
+ def parse(input)
24
+ sig, data = bla(input)
25
+
26
+ version = sig.read(1).unpack('C').first # 8-bit unsigned (unsigned char)
27
+ signature = sig.read(256)
28
+ cert_size = sig.read(4).unpack('L>')[0] # 32-bit unsigned, big-endian
29
+ receipt_cert = OpenSSL::X509::Certificate.new(sig.read(cert_size))
30
+
31
+ [version, signature, receipt_cert, data]
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'openssl'
4
+
5
+ module AppleReceipt
6
+ # Validator allows one to check the validity of a receipt.
7
+ class Validator
8
+ def initialize(receipt)
9
+ root_cert_pem = File.read('./certificates/AppleIncRootCertificate.cer')
10
+ root_cert = OpenSSL::X509::Certificate.new(root_cert_pem)
11
+
12
+ intermediate_cert_pem = File.read('./certificates/AppleWWDRCA.cer')
13
+ intermediate_cert = OpenSSL::X509::Certificate.new(intermediate_cert_pem)
14
+
15
+ store.add_cert(root_cert)
16
+ store.add_cert(intermediate_cert)
17
+
18
+ @receipt = receipt
19
+ end
20
+
21
+ def valid?
22
+ store.verify(receipt.cert) &&
23
+ public_key.verify(OpenSSL::Digest::SHA1.new,
24
+ receipt.signature, signed_data)
25
+ end
26
+
27
+ def public_key
28
+ receipt.cert.public_key
29
+ end
30
+
31
+ def signed_data
32
+ [receipt.version, receipt.data].pack('CA*')
33
+ end
34
+
35
+ def store
36
+ @store ||= OpenSSL::X509::Store.new
37
+ end
38
+
39
+ private
40
+
41
+ attr_reader :receipt
42
+ end
43
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AppleReceipt
4
+ VERSION = '0.1.1'
5
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: apple_receipt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Koen Rouwhorst
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-01-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.50'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.50'
69
+ description:
70
+ email:
71
+ - koenrh@blendle.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rubocop.yml"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - apple_receipt.gemspec
85
+ - bin/console
86
+ - bin/setup
87
+ - certificates/AppleIncRootCertificate.cer
88
+ - certificates/AppleWWDRCA.cer
89
+ - lib/apple_receipt.rb
90
+ - lib/apple_receipt/next_step_parser.rb
91
+ - lib/apple_receipt/receipt.rb
92
+ - lib/apple_receipt/receipt_parser.rb
93
+ - lib/apple_receipt/validator.rb
94
+ - lib/apple_receipt/version.rb
95
+ homepage: https://www.blendle.com
96
+ licenses:
97
+ - MIT
98
+ metadata:
99
+ allowed_push_host: https://rubygems.org
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 2.7.4
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Local Apple receipt validation.
120
+ test_files: []