Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/action_push_native/service/apns/token_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def regenerate_if_expired
def generate
payload = { iss: config.fetch(:team_id), iat: Time.now.utc.to_i }
header = { kid: config.fetch(:key_id) }
private_key = OpenSSL::PKey::EC.new(config.fetch(:encryption_key))
private_key = OpenSSL::PKey.read(config.fetch(:encryption_key))
JWT.encode(payload, private_key, "ES256", header)
end
end
14 changes: 14 additions & 0 deletions test/lib/action_push_native/service/apns_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,20 @@ class ApnsTest < ActiveSupport::TestCase
end
end

test "generate produces a valid ES256 JWT from a PEM key" do
ActionPushNative::Service::Apns::TokenProvider.any_instance.unstub(:fresh_access_token)

ec_key = OpenSSL::PKey::EC.generate("prime256v1")
config = { team_id: "TEAM123", key_id: "KEY456", encryption_key: ec_key.to_pem }
provider = ActionPushNative::Service::Apns::TokenProvider.new(config)

token = provider.fresh_access_token

decoded = JWT.decode(token, ec_key, true, algorithm: "ES256")
assert_equal "TEAM123", decoded.first["iss"]
assert_equal "KEY456", decoded.last["kid"]
end

test "connect to APNs development server" do
apns = Apns.new(@config.merge(connect_to_development_server: true))
stub_request(:post, "https://api.sandbox.push.apple.com/3/device/123").to_return(status: 200)
Expand Down
Loading