Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for ca certificate #122

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
13 changes: 9 additions & 4 deletions apns.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
class APNs(object):
"""A class representing an Apple Push Notification service connection"""

def __init__(self, use_sandbox=False, cert_file=None, key_file=None, enhanced=False):
def __init__(self, use_sandbox=False, cert_file=None, key_file=None, enhanced=False, ca_certs=None):
"""
Set use_sandbox to True to use the sandbox (test) APNs servers.
Default is False.
Expand All @@ -104,6 +104,7 @@ def __init__(self, use_sandbox=False, cert_file=None, key_file=None, enhanced=Fa
self.use_sandbox = use_sandbox
self.cert_file = cert_file
self.key_file = key_file
self.ca_certs = ca_certs
self._feedback_connection = None
self._gateway_connection = None
self.enhanced = enhanced
Expand Down Expand Up @@ -157,7 +158,8 @@ def feedback_server(self):
self._feedback_connection = FeedbackConnection(
use_sandbox = self.use_sandbox,
cert_file = self.cert_file,
key_file = self.key_file
key_file = self.key_file,
ca_certs = self.ca_certs
)
return self._feedback_connection

Expand All @@ -168,6 +170,7 @@ def gateway_server(self):
use_sandbox = self.use_sandbox,
cert_file = self.cert_file,
key_file = self.key_file,
ca_certs = self.ca_certs,
enhanced = self.enhanced
)
return self._gateway_connection
Expand All @@ -177,10 +180,11 @@ class APNsConnection(object):
"""
A generic connection class for communicating with the APNs
"""
def __init__(self, cert_file=None, key_file=None, timeout=None, enhanced=False):
def __init__(self, cert_file=None, key_file=None, timeout=None, enhanced=False, ca_certs=None):
super(APNsConnection, self).__init__()
self.cert_file = cert_file
self.key_file = key_file
self.ca_certs = ca_certs
self.timeout = timeout
self._socket = None
self._ssl = None
Expand Down Expand Up @@ -210,6 +214,7 @@ def _connect(self):
self._last_activity_time = time.time()
self._socket.setblocking(False)
self._ssl = wrap_socket(self._socket, self.key_file, self.cert_file,
ca_certs=self.ca_certs,
do_handshake_on_connect=False)
while True:
try:
Expand All @@ -227,7 +232,7 @@ def _connect(self):
# Fallback for 'SSLError: _ssl.c:489: The handshake operation timed out'
for i in xrange(3):
try:
self._ssl = wrap_socket(self._socket, self.key_file, self.cert_file)
self._ssl = wrap_socket(self._socket, self.key_file, self.cert_file, ca_certs=self.ca_certs)
break
except SSLError, ex:
if ex.args[0] == SSL_ERROR_WANT_READ:
Expand Down