Esempio n. 1
0
 def get_secret(self, tag):
     secrets = self._secrets
     if not secrets:
         raise KeyError('no application secrets configured')
     try:
         return secrets[tag]
     except KeyError:
         raise suppress_cause(KeyError('unknown secret tag: %r' % (tag, )))
Esempio n. 2
0
def ab64_decode(data):
    if isinstance(data, unicode):
        try:
            data = data.encode('ascii')
        except UnicodeEncodeError:
            raise suppress_cause(
                ValueError(
                    'string argument should contain only ASCII characters'))

    return b64s_decode(data.replace('.', '+'))
Esempio n. 3
0
def b64s_decode(data):
    if isinstance(data, unicode):
        try:
            data = data.encode('ascii')
        except UnicodeEncodeError:
            raise suppress_cause(
                ValueError(
                    'string argument should contain only ASCII characters'))

    off = len(data) & 3
    if off == 0:
        pass
    else:
        if off == 2:
            data += _BASE64_PAD2
        else:
            if off == 3:
                data += _BASE64_PAD1
            else:
                raise ValueError('invalid base64 input')
    try:
        return a2b_base64(data)
    except _BinAsciiError as err:
        raise suppress_cause(TypeError(err))
Esempio n. 4
0
    def _calc_checksum(self, secret):
        if isinstance(secret, unicode):
            secret = secret.encode('utf-8')
        if self.use_defaults:
            self._check_truncate_policy(secret)
        try:
            salt_value = h64.decode_int12(self.salt.encode('ascii'))
        except ValueError:
            raise suppress_cause(ValueError('invalid chars in salt'))

        key1 = _crypt_secret_to_key(secret)
        result1 = des_encrypt_int_block(key1, 0, salt_value, 20)
        key2 = _crypt_secret_to_key(secret[8:16])
        result2 = des_encrypt_int_block(key2, 0, salt_value, 5)
        chk = h64big.encode_int64(result1) + h64big.encode_int64(result2)
        return chk.decode('ascii')
Esempio n. 5
0
    def using(cls, block_size=None, **kwds):
        subcls = super(scrypt, cls).using(**kwds)
        if block_size is not None:
            if isinstance(block_size, uh.native_string_types):
                block_size = int(block_size)
            subcls.block_size = subcls._norm_block_size(
                block_size, relaxed=kwds.get('relaxed'))
        try:
            _scrypt.validate(1 << cls.default_rounds, cls.block_size,
                             cls.parallelism)
        except ValueError as err:
            raise suppress_cause(
                ValueError('scrypt: invalid settings combination: ' +
                           str(err)))

        return subcls
Esempio n. 6
0
    def to_string(self):
        ident = self.ident
        if ident == IDENT_SCRYPT:
            return '$scrypt$ln=%d,r=%d,p=%d$%s$%s' % (
                self.rounds, self.block_size, self.parallelism,
                bascii_to_str(b64s_encode(
                    self.salt)), bascii_to_str(b64s_encode(self.checksum)))
        salt = self.salt
        try:
            salt.decode('ascii')
        except UnicodeDecodeError:
            raise suppress_cause(
                NotImplementedError(
                    'scrypt $7$ hashes dont support non-ascii salts'))

        return bascii_to_str(('').join([
            '$7$',
            h64.encode_int6(self.rounds),
            h64.encode_int30(self.block_size),
            h64.encode_int30(self.parallelism), self.salt, '$',
            h64.encode_bytes(self.checksum)
        ]))
            if not os.path.exists(source_path):
                raise EnvironmentError('django source path not found: %r' %
                                       source_path)
            if not all(
                    os.path.exists(os.path.join(source_path, name))
                    for name in ['django', 'tests']):
                raise EnvironmentError('invalid django source path: %r' %
                                       source_path)
            log.info('using django tests from source path: %r', source_path)
            tests_path = os.path.join(source_path, 'tests')
            sys.path.insert(0, tests_path)
            try:
                from auth_tests import test_hashers as test_hashers_mod
            except ImportError as err:
                raise suppress_cause(
                    EnvironmentError(
                        'error trying to import django tests from source path (%r): %r'
                        % (source_path, err)))
            finally:
                sys.path.remove(tests_path)

        else:
            hashers_skip_msg = 'requires PASSLIB_TESTS_DJANGO_SOURCE_PATH to be set'
            if TEST_MODE('full'):
                sys.stderr.write(
                    "\nWARNING: $PASSLIB_TESTS_DJANGO_SOURCE_PATH is not set; can't run Django's own unittests against passlib.ext.django\n"
                )
    else:
        if DJANGO_VERSION:
            hashers_skip_msg = 'django version too old'
        else:
            hashers_skip_msg = 'django not installed'