Skip to content

Commit b0ce8f8

Browse files
committed
optional configuration without environment variables
1 parent 83c9c8d commit b0ce8f8

File tree

5 files changed

+83
-23
lines changed

5 files changed

+83
-23
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ If you followed the installation steps, you already saw that Passkit provides
5858
you the tables and ActiveRecord models, and also an engine with the necessary APIs already implemented.
5959

6060
Now is your turn. Before proceeding, you need to set these ENV variables:
61+
6162
* `PASSKIT_WEB_SERVICE_HOST`
6263
* `PASSKIT_CERTIFICATE_KEY`
6364
* `PASSKIT_PRIVATE_P12_CERTIFICATE`
@@ -68,6 +69,26 @@ Now is your turn. Before proceeding, you need to set these ENV variables:
6869
We have a [specific guide on how to get all these](docs/passkit_environment_variables.md), please follow it.
6970
You cannot start using this library without these variables set, and we cannot do the work for you.
7071

72+
Alternatively, you can configure passkit with an initializer, where you can use environment variables, Rails secrets,
73+
or any other source for the required credentials:
74+
75+
```ruby
76+
Passkit.configure do |config|
77+
# Required, no defaults
78+
config.apple_team_identifier = "dummy ID"
79+
config.certificate_key = "dummy key"
80+
config.private_p12_certificate = "path/to/file"
81+
config.apple_intermediate_certificate = "path/to/file"
82+
config.pass_type_identifier = "pass.com.some.id"
83+
84+
# Optional, defaults shown
85+
config.dashboard_username = nil
86+
config.dashboard_password = nil
87+
config.skip_verification = false # Unless true, throws exceptions on startup when a required configuration is missing
88+
config.web_service_host = "https://localhost:3000"
89+
config.available_passes = { "Passkit::ExampleStoreCard" => -> {} }
90+
end
91+
7192
## Usage
7293

7394
If you followed the installation steps and you have the ENV variables set, we can start looking at what is provided for you.

lib/passkit.rb

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ class << self
1818
def self.configure
1919
self.configuration ||= Configuration.new
2020
yield(configuration) if block_given?
21+
configuration.verify!
22+
end
23+
24+
def self.configured?
25+
self.configuration&.configured?
2126
end
2227

2328
class Configuration
@@ -27,11 +32,24 @@ class Configuration
2732
:private_p12_certificate,
2833
:apple_intermediate_certificate,
2934
:apple_team_identifier,
30-
:pass_type_identifier
35+
:pass_type_identifier,
36+
:dashboard_username,
37+
:dashboard_password,
38+
:format_version,
39+
:skip_verification
40+
41+
REQUIRED_ATTRIBUTES = %i[
42+
web_service_host
43+
certificate_key
44+
private_p12_certificate
45+
apple_intermediate_certificate
46+
apple_team_identifier
47+
pass_type_identifier
48+
]
3149

3250
DEFAULT_AUTHENTICATION = proc do
3351
authenticate_or_request_with_http_basic("Passkit Dashboard. Login required") do |username, password|
34-
username == ENV["PASSKIT_DASHBOARD_USERNAME"] && password == ENV["PASSKIT_DASHBOARD_PASSWORD"]
52+
username == Passkit.configuration.dashboard_username && password == Passkit.configuration.dashboard_password
3553
end
3654
end
3755
def authenticate_dashboard_with(&block)
@@ -40,14 +58,34 @@ def authenticate_dashboard_with(&block)
4058
end
4159

4260
def initialize
43-
@available_passes = {"Passkit::ExampleStoreCard" => -> {}}
44-
@web_service_host = ENV["PASSKIT_WEB_SERVICE_HOST"] || (raise "Please set PASSKIT_WEB_SERVICE_HOST")
45-
raise("PASSKIT_WEB_SERVICE_HOST must start with https://") unless @web_service_host.start_with?("https://")
46-
@certificate_key = ENV["PASSKIT_CERTIFICATE_KEY"] || (raise "Please set PASSKIT_CERTIFICATE_KEY")
47-
@private_p12_certificate = ENV["PASSKIT_PRIVATE_P12_CERTIFICATE"] || (raise "Please set PASSKIT_PRIVATE_P12_CERTIFICATE")
48-
@apple_intermediate_certificate = ENV["PASSKIT_APPLE_INTERMEDIATE_CERTIFICATE"] || (raise "Please set PASSKIT_APPLE_INTERMEDIATE_CERTIFICATE")
49-
@apple_team_identifier = ENV["PASSKIT_APPLE_TEAM_IDENTIFIER"] || (raise "Please set PASSKIT_APPLE_TEAM_IDENTIFIER")
50-
@pass_type_identifier = ENV["PASSKIT_PASS_TYPE_IDENTIFIER"] || (raise "Please set PASSKIT_PASS_TYPE_IDENTIFIER")
61+
# Required
62+
@certificate_key = ENV["PASSKIT_CERTIFICATE_KEY"]
63+
@private_p12_certificate = ENV["PASSKIT_PRIVATE_P12_CERTIFICATE"]
64+
@apple_intermediate_certificate = ENV["PASSKIT_APPLE_INTERMEDIATE_CERTIFICATE"]
65+
@apple_team_identifier = ENV["PASSKIT_APPLE_TEAM_IDENTIFIER"]
66+
@pass_type_identifier = ENV["PASSKIT_PASS_TYPE_IDENTIFIER"]
67+
68+
# Optional
69+
@skip_verification = false
70+
@web_service_host = ENV["PASSKIT_WEB_SERVICE_HOST"] || "https://localhost:3000"
71+
@available_passes = { "Passkit::ExampleStoreCard" => -> {} }
72+
@format_version = ENV["PASSKIT_FORMAT_VERSION"] || 1
73+
@dashboard_username = ENV["PASSKIT_DASHBOARD_USERNAME"]
74+
@dashboard_password = ENV["PASSKIT_DASHBOARD_PASSWORD"]
75+
end
76+
77+
def configured?
78+
REQUIRED_ATTRIBUTES.all? { |attr| send(attr).present? }
79+
end
80+
81+
def verify!
82+
return if skip_verification
83+
84+
REQUIRED_ATTRIBUTES.each do |attr|
85+
raise Error, "Please set #{attr.upcase}" unless send(attr).present?
86+
end
87+
88+
raise Error, "PASSKIT_WEB_SERVICE_HOST must start with https://" unless web_service_host.start_with?("https://")
5189
end
5290
end
5391
end

lib/passkit/base_pass.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ def initialize(generator = nil)
55
end
66

77
def format_version
8-
ENV["PASSKIT_FORMAT_VERSION"] || 1
8+
Passkit.configuration.format_version
99
end
1010

1111
def apple_team_identifier
12-
ENV["PASSKIT_APPLE_TEAM_IDENTIFIER"] || raise(Error.new("Missing environment variable: PASSKIT_APPLE_TEAM_IDENTIFIER"))
12+
Passkit.configuration.apple_team_identifier
1313
end
1414

1515
def pass_type_identifier
16-
ENV["PASSKIT_PASS_TYPE_IDENTIFIER"] || raise(Error.new("Missing environment variable: PASSKIT_PASS_TYPE_IDENTIFIER"))
16+
Passkit.configuration.pass_type_identifier
1717
end
1818

1919
def language
@@ -43,8 +43,7 @@ def pass_type
4343
end
4444

4545
def web_service_url
46-
raise Error.new("Missing environment variable: PASSKIT_WEB_SERVICE_HOST") unless ENV["PASSKIT_WEB_SERVICE_HOST"]
47-
"#{ENV["PASSKIT_WEB_SERVICE_HOST"]}/passkit/api"
46+
"#{Passkit.configuration.web_service_host}/passkit/api"
4847
end
4948

5049
# The foreground color, used for the values of fields shown on the front of the pass.

lib/passkit/generator.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,6 @@ def generate_json_pass
111111
File.write(@temporary_path.join("pass.json"), pass.to_json)
112112
end
113113

114-
# rubocop:enable Metrics/AbcSize
115-
116114
def generate_json_manifest
117115
manifest = {}
118116
Dir.glob(@temporary_path.join("**")).each do |file|
@@ -123,14 +121,18 @@ def generate_json_manifest
123121
File.write(@manifest_url, manifest.to_json)
124122
end
125123

126-
CERTIFICATE = Rails.root.join(ENV["PASSKIT_PRIVATE_P12_CERTIFICATE"])
127-
INTERMEDIATE_CERTIFICATE = Rails.root.join(ENV["PASSKIT_APPLE_INTERMEDIATE_CERTIFICATE"])
128-
CERTIFICATE_PASSWORD = ENV["PASSKIT_CERTIFICATE_KEY"]
124+
def certificate_path
125+
Rails.root.join(Passkit.configuration.private_p12_certificate)
126+
end
127+
128+
def intermediate_certificate_path
129+
Rails.root.join(Passkit.configuration.apple_intermediate_certificate)
130+
end
129131

130132
# :nocov:
131133
def sign_manifest
132-
p12_certificate = OpenSSL::PKCS12.new(File.read(CERTIFICATE), CERTIFICATE_PASSWORD)
133-
intermediate_certificate = OpenSSL::X509::Certificate.new(File.read(INTERMEDIATE_CERTIFICATE))
134+
p12_certificate = OpenSSL::PKCS12.new(File.read(certificate_path), Passkit.configuration.certificate_key)
135+
intermediate_certificate = OpenSSL::X509::Certificate.new(File.read(intermediate_certificate_path))
134136

135137
flag = OpenSSL::PKCS7::DETACHED | OpenSSL::PKCS7::BINARY
136138
signed = OpenSSL::PKCS7.sign(p12_certificate.certificate,

lib/passkit/url_generator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class UrlGenerator
33
include Passkit::Engine.routes.url_helpers
44

55
def initialize(pass_class, generator = nil, collection_name = nil)
6-
@url = passes_api_url(host: ENV["PASSKIT_WEB_SERVICE_HOST"],
6+
@url = passes_api_url(host: Passkit.configuration.web_service_host,
77
payload: PayloadGenerator.encrypted(pass_class, generator, collection_name))
88
end
99

0 commit comments

Comments
 (0)