From c8355f38bb80b7cb3f900c8dd1abaef413629c0e Mon Sep 17 00:00:00 2001 From: Karan Thukral Date: Fri, 13 Jun 2014 13:56:46 -0400 Subject: [PATCH 1/3] Added new property to all the user to use contents of the file instead of the file path --- lib/apns/core.rb | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/lib/apns/core.rb b/lib/apns/core.rb index c371d4e..0883e62 100644 --- a/lib/apns/core.rb +++ b/lib/apns/core.rb @@ -7,10 +7,11 @@ module APNS @port = 2195 # 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 + @pemContents = nil # Alternative, this is the contents of the pem file instead of the file path @pass = nil class << self - attr_accessor :host, :pem, :port, :pass + attr_accessor :host, :pem, :port, :pass, :pemContents, end def self.send_notification(device_token, message) @@ -64,12 +65,20 @@ def self.feedback protected def self.open_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) - 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) + + if self.pem && self.pemContents.nil? + raise "The path to your pem file does not exist!" unless File.exist?(self.pem) + context.cert = OpenSSL::X509::Certificate.new(File.read(self.pem)) + context.key = OpenSSL::PKey::RSA.new(File.read(self.pem), self.pass) + + elsif self.pemContents && self.pem.nil? + raise "The contents to your pem file are empty!" unless self.pemContents.empty? + context.cert = OpenSSL::X509::Certificate.new(self.pemContents) + context.key = OpenSSL::PKey::RSA.new(self.pemContents, self.pass) + end + + #raise "The path to your pem file is not set. (APNS.pem = /path/to/cert.pem)" unless self.pem sock = TCPSocket.new(self.host, self.port) ssl = OpenSSL::SSL::SSLSocket.new(sock,context) @@ -79,12 +88,20 @@ 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) + context = OpenSSL::SSL::SSLContext.new - 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) + if self.pem && self.pemContents.nil? + raise "The path to your pem file does not exist!" unless File.exist?(self.pem) + context.cert = OpenSSL::X509::Certificate.new(File.read(self.pem)) + context.key = OpenSSL::PKey::RSA.new(File.read(self.pem), self.pass) + + elsif self.pemContents && self.pem.nil? + raise "The contents to your pem file are empty!" unless self.pemContents.empty? + context.cert = OpenSSL::X509::Certificate.new(self.pemContents) + context.key = OpenSSL::PKey::RSA.new(self.pemContents, self.pass) + end + + #raise "The path to your pem file is not set. (APNS.pem = /path/to/cert.pem)" unless self.pem fhost = self.host.gsub('gateway','feedback') puts fhost From 876a10e1eb6960d54c7f73854f4cc9812d9ad7db Mon Sep 17 00:00:00 2001 From: Karan Thukral Date: Fri, 13 Jun 2014 14:30:38 -0400 Subject: [PATCH 2/3] Created method for setting up the context --- lib/apns/core.rb | 62 +++++++++++++++++++----------------------------- 1 file changed, 25 insertions(+), 37 deletions(-) diff --git a/lib/apns/core.rb b/lib/apns/core.rb index 0883e62..d0f40bb 100644 --- a/lib/apns/core.rb +++ b/lib/apns/core.rb @@ -7,11 +7,11 @@ module APNS @port = 2195 # 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 - @pemContents = nil # Alternative, this is the contents of the pem file instead of the file path + @pem_contents = nil # Alternative, this is the contents of the pem file instead of the file path @pass = nil class << self - attr_accessor :host, :pem, :port, :pass, :pemContents, + attr_accessor :host, :pem, :port, :pass, :pem_contents end def self.send_notification(device_token, message) @@ -65,51 +65,39 @@ def self.feedback protected def self.open_connection - context = OpenSSL::SSL::SSLContext.new - - if self.pem && self.pemContents.nil? - raise "The path to your pem file does not exist!" unless File.exist?(self.pem) - context.cert = OpenSSL::X509::Certificate.new(File.read(self.pem)) - context.key = OpenSSL::PKey::RSA.new(File.read(self.pem), self.pass) - - elsif self.pemContents && self.pem.nil? - raise "The contents to your pem file are empty!" unless self.pemContents.empty? - context.cert = OpenSSL::X509::Certificate.new(self.pemContents) - context.key = OpenSSL::PKey::RSA.new(self.pemContents, self.pass) - end - - #raise "The path to your pem file is not set. (APNS.pem = /path/to/cert.pem)" unless self.pem - - sock = TCPSocket.new(self.host, self.port) - ssl = OpenSSL::SSL::SSLSocket.new(sock,context) + context self.get_context + sock = TCPSocket.new(self.host, self.port) + ssl = OpenSSL::SSL::SSLSocket.new(sock,context) ssl.connect - return sock, ssl end def self.feedback_connection - context = OpenSSL::SSL::SSLContext.new - - if self.pem && self.pemContents.nil? - raise "The path to your pem file does not exist!" unless File.exist?(self.pem) - context.cert = OpenSSL::X509::Certificate.new(File.read(self.pem)) - context.key = OpenSSL::PKey::RSA.new(File.read(self.pem), self.pass) - - elsif self.pemContents && self.pem.nil? - raise "The contents to your pem file are empty!" unless self.pemContents.empty? - context.cert = OpenSSL::X509::Certificate.new(self.pemContents) - context.key = OpenSSL::PKey::RSA.new(self.pemContents, self.pass) - end - - #raise "The path to your pem file is not set. (APNS.pem = /path/to/cert.pem)" unless self.pem + context = self.get_context fhost = self.host.gsub('gateway','feedback') puts fhost - sock = TCPSocket.new(fhost, 2196) - ssl = OpenSSL::SSL::SSLSocket.new(sock,context) + sock = TCPSocket.new(fhost, 2196) + ssl = OpenSSL::SSL::SSLSocket.new(sock,context) ssl.connect - return sock, ssl end + + def self.get_context + context = OpenSSL::SSL::SSLContext.new + if self.pem + raise "The path to your pem file does not exist!" unless File.exist?(self.pem) + context.cert = OpenSSL::X509::Certificate.new(File.read(self.pem)) + context.key = OpenSSL::PKey::RSA.new(File.read(self.pem), self.pass) + elsif self.pem_contents + raise "The contents to your pem file are empty!" unless self.pem_contents.empty? + context.cert = OpenSSL::X509::Certificate.new(self.pem_contents) + context.key = OpenSSL::PKey::RSA.new(self.pem_contents, self.pass) + else + raise "The path to your pem file is not set. (APNS.pem = /path/to/cert.pem)" unless self.pem + end + return context + end + end From e10a4438e2bfefc57db16b72d6a428931cd531a6 Mon Sep 17 00:00:00 2001 From: Karan Thukral Date: Fri, 13 Jun 2014 14:39:08 -0400 Subject: [PATCH 3/3] Fixed spacing --- lib/apns/core.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/apns/core.rb b/lib/apns/core.rb index d0f40bb..42d4a8e 100644 --- a/lib/apns/core.rb +++ b/lib/apns/core.rb @@ -85,7 +85,7 @@ def self.feedback_connection end def self.get_context - context = OpenSSL::SSL::SSLContext.new + context = OpenSSL::SSL::SSLContext.new if self.pem raise "The path to your pem file does not exist!" unless File.exist?(self.pem) context.cert = OpenSSL::X509::Certificate.new(File.read(self.pem))