Optionally disable ssl certificate validity check. (#9181)

* Optionally disable ssl certificate validity check.

* Fix lines too long.

* Fix formatting.

* Force build CI

* Fix "Method could be a function (no-self-use)"
This commit is contained in:
ohmer1 2017-09-06 01:58:13 -04:00 committed by Andrey
parent e7a5f7bcdf
commit 5971a7c009

View file

@ -21,12 +21,14 @@ REQUIREMENTS = ['sleekxmpp==1.3.2',
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
CONF_TLS = 'tls' CONF_TLS = 'tls'
CONF_VERIFY = 'verify'
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_SENDER): cv.string, vol.Required(CONF_SENDER): cv.string,
vol.Required(CONF_PASSWORD): cv.string, vol.Required(CONF_PASSWORD): cv.string,
vol.Required(CONF_RECIPIENT): cv.string, vol.Required(CONF_RECIPIENT): cv.string,
vol.Optional(CONF_TLS, default=True): cv.boolean, vol.Optional(CONF_TLS, default=True): cv.boolean,
vol.Optional(CONF_VERIFY, default=True): cv.boolean,
}) })
@ -34,18 +36,20 @@ def get_service(hass, config, discovery_info=None):
"""Get the Jabber (XMPP) notification service.""" """Get the Jabber (XMPP) notification service."""
return XmppNotificationService( return XmppNotificationService(
config.get(CONF_SENDER), config.get(CONF_PASSWORD), config.get(CONF_SENDER), config.get(CONF_PASSWORD),
config.get(CONF_RECIPIENT), config.get(CONF_TLS)) config.get(CONF_RECIPIENT), config.get(CONF_TLS),
config.get(CONF_VERIFY))
class XmppNotificationService(BaseNotificationService): class XmppNotificationService(BaseNotificationService):
"""Implement the notification service for Jabber (XMPP).""" """Implement the notification service for Jabber (XMPP)."""
def __init__(self, sender, password, recipient, tls): def __init__(self, sender, password, recipient, tls, verify):
"""Initialize the service.""" """Initialize the service."""
self._sender = sender self._sender = sender
self._password = password self._password = password
self._recipient = recipient self._recipient = recipient
self._tls = tls self._tls = tls
self._verify = verify
def send_message(self, message="", **kwargs): def send_message(self, message="", **kwargs):
"""Send a message to a user.""" """Send a message to a user."""
@ -53,10 +57,11 @@ class XmppNotificationService(BaseNotificationService):
data = '{}: {}'.format(title, message) if title else message data = '{}: {}'.format(title, message) if title else message
send_message('{}/home-assistant'.format(self._sender), self._password, send_message('{}/home-assistant'.format(self._sender), self._password,
self._recipient, self._tls, data) self._recipient, self._tls, self._verify, data)
def send_message(sender, password, recipient, use_tls, message): def send_message(sender, password, recipient, use_tls,
verify_certificate, message):
"""Send a message over XMPP.""" """Send a message over XMPP."""
import sleekxmpp import sleekxmpp
@ -73,6 +78,10 @@ def send_message(sender, password, recipient, use_tls, message):
self.use_ipv6 = False self.use_ipv6 = False
self.add_event_handler('failed_auth', self.check_credentials) self.add_event_handler('failed_auth', self.check_credentials)
self.add_event_handler('session_start', self.start) self.add_event_handler('session_start', self.start)
if not verify_certificate:
self.add_event_handler('ssl_invalid_cert',
self.discard_ssl_invalid_cert)
self.connect(use_tls=self.use_tls, use_ssl=False) self.connect(use_tls=self.use_tls, use_ssl=False)
self.process() self.process()
@ -87,4 +96,10 @@ def send_message(sender, password, recipient, use_tls, message):
"""Disconnect from the server if credentials are invalid.""" """Disconnect from the server if credentials are invalid."""
self.disconnect() self.disconnect()
@staticmethod
def discard_ssl_invalid_cert(event):
"""Do nothing if ssl certificate is invalid."""
_LOGGER.info('Ignoring invalid ssl certificate as requested.')
return
SendNotificationBot() SendNotificationBot()