lib/apns_simple/client.rb in apns_simple-0.5.1 vs lib/apns_simple/client.rb in apns_simple-0.7.0
- old
+ new
@@ -2,11 +2,11 @@
require 'socket'
module ApnsSimple
class Client
- attr_reader :ssl_context, :host, :port
+ attr_reader :certificate, :ssl_context, :host, :port
COMMAND = 8
CODES = {
0 => 'No errors encountered',
1 => 'Processing error',
@@ -20,21 +20,26 @@
10 => 'Shutdown',
255 => 'Unknown error'
}
def initialize(options)
- certificate = options.fetch(:certificate)
+ @certificate = options.fetch(:certificate)
passphrase = options[:passphrase] || ''
- @ssl_context = OpenSSL::SSL::SSLContext.new(:TLSv1)
+ @ssl_context = OpenSSL::SSL::SSLContext.new
@ssl_context.key = OpenSSL::PKey::RSA.new(certificate, passphrase)
- @ssl_context.cert = OpenSSL::X509::Certificate.new(certificate)
gateway_uri = options[:gateway_uri] || 'apn://gateway.push.apple.com:2195'
@host, @port = parse_gateway_uri(gateway_uri)
end
def push(notification)
begin
+ current_time = Time.now.utc
+ cert = OpenSSL::X509::Certificate.new(certificate)
+ if current_time < cert.not_before || current_time > cert.not_after
+ raise CertificateActivenessTimeError, "CURRENT_TIME: #{current_time}, NOT_BEFORE: #{cert.not_before}, NOT_AFTER: #{cert.not_after}"
+ end
+ ssl_context.cert = cert
sock = TCPSocket.new(host, port)
ssl = OpenSSL::SSL::SSLSocket.new(sock, ssl_context)
ssl.connect
ssl.write(notification.payload)
ssl.flush
\ No newline at end of file