Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Added option to set contents of pem instead of path to file #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
24 changes: 16 additions & 8 deletions lib/apns/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ module APNS
# openssl pkcs12 -in mycert.p12 -out client-cert.pem -nodes -clcerts
@pem = nil # this should be the path of the pem file not the contentes
@pass = nil
@pem_contents # alternative way to specify pem certificate, overrides pem file path

class << self
attr_accessor :host, :pem, :port, :pass
attr_accessor :host, :pem, :port, :pass, :pem_contents
end

def self.send_notification(device_token, message)
Expand Down Expand Up @@ -63,13 +64,21 @@ def self.feedback

protected

def self.open_connection
def self.get_pem
return self.pem_contents unless self.pem_contents.nil?

raise "The path to your pem file is not set. (APNS.pem = /path/to/cert.pem)" unless self.pem
raise "The path to your pem file does not exist!" unless File.exist?(self.pem)

File.read(self.pem)
end

def self.open_connection
cert_contents = self.get_pem

context = OpenSSL::SSL::SSLContext.new
context.cert = OpenSSL::X509::Certificate.new(File.read(self.pem))
context.key = OpenSSL::PKey::RSA.new(File.read(self.pem), self.pass)
context.cert = OpenSSL::X509::Certificate.new(cert_contents)
context.key = OpenSSL::PKey::RSA.new(cert_contents, self.pass)

sock = TCPSocket.new(self.host, self.port)
ssl = OpenSSL::SSL::SSLSocket.new(sock,context)
Expand All @@ -79,12 +88,11 @@ def self.open_connection
end

def self.feedback_connection
raise "The path to your pem file is not set. (APNS.pem = /path/to/cert.pem)" unless self.pem
raise "The path to your pem file does not exist!" unless File.exist?(self.pem)
cert_contents = self.get_pem

context = OpenSSL::SSL::SSLContext.new
context.cert = OpenSSL::X509::Certificate.new(File.read(self.pem))
context.key = OpenSSL::PKey::RSA.new(File.read(self.pem), self.pass)
context.cert = OpenSSL::X509::Certificate.new(cert_contents)
context.key = OpenSSL::PKey::RSA.new(cert_contents, self.pass)

fhost = self.host.gsub('gateway','feedback')
puts fhost
Expand Down