Ejemplo n.º 1
0
    def test_b64_digest(self):
        '''
		Perform a digest using the specified algorithm of SHA1. The b64-encoded
		digest should be returned.
		'''
        utils_digest = utils._b64_digest('a', 'b', 'c')
        actual_digest = base64.b64encode(hashlib.sha256(b'abc').digest())
        self.assertEqual(actual_digest, utils_digest)

        single_digest = utils._b64_digest('abc')
        multi_digest = utils._b64_digest('a', 'b', 'c')
        self.assertEqual(single_digest, multi_digest)
Ejemplo n.º 2
0
    def test_b64_digest_specified_algorithm(self):
        '''
		Perform a digest using a specified algorithm. The b64-encoded
		digest should be returned.
		'''
        utils_digest = utils._b64_digest('a', 'b', 'c', algorithm='sha512')
        actual_digest = base64.b64encode(hashlib.sha512(b'abc').digest())
        self.assertEqual(actual_digest, utils_digest)
Ejemplo n.º 3
0
    def test_b64_digest_sha1(self):
        '''
		Perform a digest using the specified algorithm of SHA1. The b64-encoded
		digest should be returned.
		'''
        with mock.patch.object(settings, 'ALLOWED_DIGEST_ALGORITHMS',
                               ['SHA1']):
            utils_digest = utils._b64_digest('a', 'b', 'c')
            actual_digest = base64.b64encode(hashlib.sha1(b'abc').digest())

            self.assertEqual(actual_digest, utils_digest)
Ejemplo n.º 4
0
    def make_header_values(self,
                           user=None,
                           username=None,
                           timestamp=None,
                           digest=None,
                           b64_digest=None,
                           nonce=None,
                           b64_nonce=None,
                           digest_algorithm=None):
        '''
		Make the header values from the given parameters.

		:param user: (optional) user to authenticate with header
		:type user: django.contrib.auth.models.User

		:param username: (optional) username to provide in header
		:type username: str

		:param timestamp: (optional) timestamp to use in header
		:type timestamp: str

		:param digest: (optional) header digest
		:type digest: bytes

		:param b64_digest: (optional) header digest as base64
		:type b64_digest: bytes

		:param nonce: (optional) header nonce
		:type nonce: bytes

		:param b64_nonce: (optional) header nonce as base64
		:type b64_nonce: bytes

		:param digest_algorithm: (optional, default: sha256) digest algorithm to
			use. It must be supported by hashlib.
		:type digest_algorithm: str

		:return: WSSE authentication header parts
		:rtype: dict
		'''
        if user is None:
            user = self.user
        if username is None:
            username = user.username

        if timestamp is None:
            now = timezone.now()
            timestamp = now.strftime(settings.TIMESTAMP_FORMATS[0])

        if nonce is None:
            nonce = utils._random_string(length=settings.NONCE_LENGTH)

        if digest is None:
            digest = utils._b64_digest(nonce,
                                       timestamp,
                                       self.user_secret.secret,
                                       algorithm=digest_algorithm)

        if b64_nonce is None:
            b64_nonce = base64.b64encode(utils._to_bytes(nonce))
        if b64_digest is not None:
            digest = b64_digest

        header_values = {
            'Username': username,
            'PasswordDigest': utils._from_bytes(digest),
            'Nonce': utils._from_bytes(b64_nonce),
            'Created': timestamp
        }

        return header_values