Пример #1
0
    def set_password(self, password, encryptAlgo='MD5'):
        """Set a password for a GNTP Message

		:param string password: Null to clear password
		:param string encryptAlgo: Supports MD5, SHA1, SHA256, SHA512
		"""
        if not password:
            self.info['encryptionAlgorithmID'] = None
            self.info['keyHashAlgorithm'] = None
            return

        self.password = gntp.shim.b(password)
        self.encryptAlgo = encryptAlgo.upper()

        if not self.encryptAlgo in self.hash_algo:
            raise errors.UnsupportedError('INVALID HASH "%s"' %
                                          self.encryptAlgo)

        hashfunction = self.hash_algo.get(self.encryptAlgo)

        password = password.encode('utf8')
        seed = time.ctime().encode('utf8')
        salt = hashfunction(seed).hexdigest()
        saltHash = hashfunction(seed).digest()
        keyBasis = password + saltHash
        key = hashfunction(keyBasis).digest()
        keyHash = hashfunction(key).hexdigest()

        self.info['keyHashAlgorithmID'] = self.encryptAlgo
        self.info['keyHash'] = keyHash.upper()
        self.info['salt'] = salt.upper()
Пример #2
0
def test_plugin_growl_exception_handling(mock_gntp):
    """
    NotifyGrowl() Exception Handling
    """
    TEST_GROWL_EXCEPTIONS = (
        errors.NetworkError(0, 'gntp.ParseError() not handled'),
        errors.AuthError(0, 'gntp.AuthError() not handled'),
        errors.ParseError(0, 'gntp.ParseError() not handled'),
        errors.UnsupportedError(0, 'gntp.UnsupportedError() not handled'),
    )

    mock_notifier = mock.Mock()
    mock_gntp.return_value = mock_notifier
    mock_notifier.notify.return_value = True

    # First we test the growl.register() function
    for exception in TEST_GROWL_EXCEPTIONS:
        mock_notifier.register.side_effect = exception

        # instantiate our object
        obj = apprise.Apprise.instantiate('growl://growl.server.hostname',
                                          suppress_exceptions=False)

        # Verify Growl object was instantiated
        assert obj is not None

        # We will fail to send the notification because our registration
        # would have failed
        assert obj.notify(title='test',
                          body='body',
                          notify_type=apprise.NotifyType.INFO) is False

    # Now we test the growl.notify() function
    mock_notifier.register.side_effect = None
    for exception in TEST_GROWL_EXCEPTIONS:
        mock_notifier.notify.side_effect = exception

        # instantiate our object
        obj = apprise.Apprise.instantiate('growl://growl.server.hostname',
                                          suppress_exceptions=False)

        # Verify Growl object was instantiated
        assert obj is not None

        # We will fail to send the notification because of the underlining
        # notify() call throws an exception
        assert obj.notify(title='test',
                          body='body',
                          notify_type=apprise.NotifyType.INFO) is False
Пример #3
0
    def _validate_password(self, password):
        """Validate GNTP Message against stored password"""
        self.password = password
        if password is None:
            raise errors.AuthError('Missing password')
        keyHash = self.info.get('keyHash', None)
        if keyHash is None and self.password is None:
            return True
        if keyHash is None:
            raise errors.AuthError('Invalid keyHash')
        if self.password is None:
            raise errors.AuthError('Missing password')

        hash_algo = {
            'MD5': hashlib.md5,
            'SHA1': hashlib.sha1,
            'SHA256': hashlib.sha256,
            'SHA512': hashlib.sha512,
        }

        if not self.info['keyHashAlgorithmID'] in hash_algo:
            raise errors.UnsupportedError('INVALID HASH "%s"' %
                                          self.encryptAlgo)

        hashfunction = hash_algo.get(self.info['keyHashAlgorithmID'])

        password = self.password.encode('utf8')
        saltHash = self._decode_hex(self.info['salt'])

        keyBasis = password + saltHash
        key = hashfunction(keyBasis).digest()
        keyHash = hashfunction(key).hexdigest()

        if not keyHash.upper() == self.info['keyHash'].upper():
            raise errors.AuthError('Invalid Hash')
        return True
Пример #4
0
# THE SOFTWARE.

import sys
import mock
import six
import pytest
import apprise

try:
    from gntp import errors

    TEST_GROWL_EXCEPTIONS = (
        errors.NetworkError(0, 'gntp.ParseError() not handled'),
        errors.AuthError(0, 'gntp.AuthError() not handled'),
        errors.ParseError(0, 'gntp.ParseError() not handled'),
        errors.UnsupportedError(0, 'gntp.UnsupportedError() not handled'),
    )

except ImportError:
    # no problem; gntp isn't available to us
    pass

# Disable logging for a cleaner testing output
import logging
logging.disable(logging.CRITICAL)


@pytest.mark.skipif('gntp' in sys.modules,
                    reason="Requires that gntp NOT be installed")
def test_plugin_growl_gntp_import_error():
    """