Skip to content

Commit

Permalink
Make ActiveRecord validations work with Apns2 client (rpush#601)
Browse files Browse the repository at this point in the history
  • Loading branch information
favrik authored May 6, 2021
1 parent 97ef4fe commit 84647b4
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 22 deletions.
1 change: 1 addition & 0 deletions lib/rpush/client/active_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'rpush/client/active_model/notification'
require 'rpush/client/active_model/payload_data_size_validator'
require 'rpush/client/active_model/registration_ids_count_validator'
require 'rpush/client/active_model/certificate_private_key_validator'

require 'rpush/client/active_model/apns/device_token_format_validator'
require 'rpush/client/active_model/apns/app'
Expand Down
18 changes: 1 addition & 17 deletions lib/rpush/client/active_model/apns/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,13 @@ def self.included(base)
base.instance_eval do
validates :environment, presence: true, inclusion: { in: %w(development production sandbox) }
validates :certificate, presence: true
validate :certificate_has_matching_private_key
validates_with Rpush::Client::ActiveModel::CertificatePrivateKeyValidator
end
end

def service_name
'apns'
end

private

def certificate_has_matching_private_key
result = false
if certificate.present?
begin
x509 = OpenSSL::X509::Certificate.new(certificate)
pkey = OpenSSL::PKey::RSA.new(certificate, password)
result = x509 && pkey
rescue OpenSSL::OpenSSLError
errors.add :certificate, 'value must contain a certificate and a private key.'
end
end
result
end
end
end
end
Expand Down
8 changes: 7 additions & 1 deletion lib/rpush/client/active_model/apns2/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ module Client
module ActiveModel
module Apns2
module App
extend Rpush::Client::ActiveModel::Apns::App
def self.included(base)
base.instance_eval do
validates :environment, presence: true, inclusion: { in: %w(development production sandbox) }
validates :certificate, presence: true
validates_with Rpush::Client::ActiveModel::CertificatePrivateKeyValidator
end
end

def service_name
'apns2'
Expand Down
19 changes: 19 additions & 0 deletions lib/rpush/client/active_model/certificate_private_key_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Rpush
module Client
module ActiveModel
class CertificatePrivateKeyValidator < ::ActiveModel::Validator
def validate(record)
if record.certificate.present?
begin
x509 = OpenSSL::X509::Certificate.new(record.certificate)
pkey = OpenSSL::PKey::RSA.new(record.certificate, record.password)
x509 && pkey
rescue OpenSSL::OpenSSLError
record.errors.add :certificate, 'value must contain a certificate and a private key.'
end
end
end
end
end
end
end
2 changes: 1 addition & 1 deletion spec/unit/client/active_record/apns/notification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

it "should validate the length of the binary conversion of the notification" do
notification = described_class.new
notification.app = Rpush::Apns2::App.create(name: 'test', environment: 'development')
notification.app = Rpush::Apns2::App.create(name: 'test', environment: 'development', certificate: TEST_CERT)
notification.device_token = "a" * 108
notification.alert = ""

Expand Down
1 change: 1 addition & 0 deletions spec/unit/client/active_record/apns2/app_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'unit_spec_helper'

describe Rpush::Client::ActiveRecord::Apns2::App do
it_behaves_like 'Rpush::Client::Apns::App'
end if active_record?
2 changes: 1 addition & 1 deletion spec/unit/client/active_record/apns2/notification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

it "should validate the length of the binary conversion of the notification" do
notification = described_class.new
notification.app = Rpush::Apns2::App.create(name: 'test', environment: 'development')
notification.app = Rpush::Apns2::App.create(name: 'test', environment: 'development', certificate: TEST_CERT)
notification.device_token = "a" * 108
notification.alert = ""

Expand Down
2 changes: 1 addition & 1 deletion spec/unit/client/redis/apns/notification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

it "should validate the length of the binary conversion of the notification" do
notification = described_class.new
notification.app = Rpush::Apns2::App.create(name: 'test', environment: 'development')
notification.app = Rpush::Apns2::App.create(name: 'test', environment: 'development', certificate: TEST_CERT)
notification.device_token = "a" * 108
notification.alert = ""

Expand Down
2 changes: 1 addition & 1 deletion spec/unit/client/redis/apns2/notification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

it "should validate the length of the binary conversion of the notification" do
notification = described_class.new
notification.app = Rpush::Apns2::App.create(name: 'test', environment: 'development')
notification.app = Rpush::Apns2::App.create(name: 'test', environment: 'development', certificate: TEST_CERT)
notification.device_token = "a" * 108
notification.alert = ""

Expand Down

0 comments on commit 84647b4

Please sign in to comment.