Пример #1
0
import six
from six.moves import copyreg

if six.PY2:
    from urlparse import urlparse

    # workaround for https://bugs.python.org/issue9374 - Python < 2.7.4
    if urlparse('s3://bucket/key?key=value').query != 'key=value':
        from urlparse import uses_query
        uses_query.append('s3')

# Undo what Twisted's perspective broker adds to pickle register
# to prevent bugs like Twisted#7989 while serializing requests
import twisted.persisted.styles  # NOQA
# Remove only entries with twisted serializers for non-twisted types.
for k, v in frozenset(copyreg.dispatch_table.items()):
    if not str(getattr(k, '__module__', '')).startswith('twisted') \
            and str(getattr(v, '__module__', '')).startswith('twisted'):
        copyreg.dispatch_table.pop(k)
Пример #2
0
def parse_url(url):
    """
    Parses a URL like, 'ssh://user@host:22' and returns a dict of::

        {
            'scheme': scheme,
            'user': user,
            'host': host,
            'port': port,
            'password': password,
            'identities': identities,
            'debug': debug
        }

    .. note:: 'web+ssh://' URLs are also supported.

    If an ssh URL is given without a username, os.environ['GO_USER'] will be
    used and if that doesn't exist it will fall back to os.environ['USER'].

    SSH Identities may be specified as a query string:

        ssh://user@host:22/?identities=id_rsa,id_ecdsa

    .. note::

        *password* and *identities* may be returned as None and [],
        respectively if not provided.
    """
    identities = set()
    debug = False
    import socket
    try:
        from urlparse import urlparse, parse_qs, uses_query
        if 'ssh' not in uses_query:  # Only necessary in Python 2.X
            uses_query.append('ssh')
    except ImportError:  # Python 3
        from urllib.parse import urlparse, parse_qs
    parsed = urlparse(url)
    if parsed.query:
        q_attrs = parse_qs(parsed.query)
        for ident in q_attrs.get('identities', identities):
            identities.update(ident.split(','))
        debug = q_attrs.get('debug', False)
        if debug:  # Passing anything turns on debug
            debug = True
    if parsed.port:
        port = parsed.port
    else:
        port = socket.getservbyname(parsed.scheme, 'tcp')
    # Commented these out so that users can enter an arbitrary username.  It is
    # more user-friendly this way since users won't have to have multiple
    # bookmarks to the same host just to connect with different usernames.
    #elif os.environ.get('GO_USER'):
    #username = os.environ['GO_USER']
    #elif os.environ.get('USER'):
    #username = os.environ['USER']
    #return (o.scheme, username, o.hostname, port, o.password, identities)
    return {
        'scheme': parsed.scheme,
        'user': parsed.username,
        'host': parsed.hostname,
        'port': port,
        'password': parsed.password,
        'identities': identities,
        'debug': debug
    }
Пример #3
0
def parse_url(url):
    """
    Parses a URL like, 'ssh://user@host:22' and returns a dict of::

        {
            'scheme': scheme,
            'user': user,
            'host': host,
            'port': port,
            'password': password,
            'identities': identities,
            'debug': debug
        }

    .. note:: 'web+ssh://' URLs are also supported.

    If an ssh URL is given without a username, os.environ['GO_USER'] will be
    used and if that doesn't exist it will fall back to os.environ['USER'].

    SSH Identities may be specified as a query string:

        ssh://user@host:22/?identities=id_rsa,id_ecdsa

    .. note::

        *password* and *identities* may be returned as None and [],
        respectively if not provided.
    """
    identities = set()
    debug = False
    import socket
    try:
        from urlparse import urlparse, parse_qs, uses_query
        if 'ssh' not in uses_query: # Only necessary in Python 2.X
            uses_query.append('ssh')
    except ImportError: # Python 3
        from urllib.parse import urlparse, parse_qs
    parsed = urlparse(url)
    if parsed.query:
        q_attrs = parse_qs(parsed.query)
        for ident in q_attrs.get('identities', identities):
            identities.update(ident.split(','))
        debug = q_attrs.get('debug', False)
        if debug: # Passing anything turns on debug
            debug = True
    if parsed.port:
        port = parsed.port
    else:
        port = socket.getservbyname(parsed.scheme, 'tcp')
    # Commented these out so that users can enter an arbitrary username.  It is
    # more user-friendly this way since users won't have to have multiple
    # bookmarks to the same host just to connect with different usernames.
    #elif os.environ.get('GO_USER'):
        #username = os.environ['GO_USER']
    #elif os.environ.get('USER'):
        #username = os.environ['USER']
    #return (o.scheme, username, o.hostname, port, o.password, identities)
    return {
        'scheme': parsed.scheme,
        'user': parsed.username,
        'host': parsed.hostname,
        'port': port,
        'password': parsed.password,
        'identities': identities,
        'debug': debug
    }
Пример #4
0
from otp.ai.passlib import exc
from otp.ai.passlib.exc import TokenError, MalformedTokenError, InvalidTokenError, UsedTokenError
from otp.ai.passlib.utils import to_unicode, to_bytes, consteq, getrandbytes, rng, SequenceMixin, xor_bytes, getrandstr
from otp.ai.passlib.utils.binary import BASE64_CHARS, b32encode, b32decode
from otp.ai.passlib.utils.compat import u, unicode, native_string_types, bascii_to_str, int_types, num_types, irange, byte_elem_value, UnicodeIO, suppress_cause
from otp.ai.passlib.utils.decor import hybrid_method, memoized_property
from otp.ai.passlib.crypto.digest import lookup_hash, compile_hmac, pbkdf2_hmac
from otp.ai.passlib.hash import pbkdf2_sha256
__all__ = [
    'AppWallet', 'TOTP', 'TokenError', 'MalformedTokenError',
    'InvalidTokenError', 'UsedTokenError', 'TotpToken', 'TotpMatch'
]
if sys.version_info < (2, 7, 4):
    from urlparse import uses_query
    if 'otpauth' not in uses_query:
        uses_query.append('otpauth')
        log.debug("registered 'otpauth' scheme with urlparse.uses_query")
    del uses_query
_clean_re = re.compile(u('\\s|[-=]'), re.U)
_chunk_sizes = [4, 6, 5]


def _get_group_size(klen):
    for size in _chunk_sizes:
        if not klen % size:
            return size

    best = _chunk_sizes[0]
    rem = 0
    for size in _chunk_sizes:
        if klen % size > rem:
Пример #5
0
import sys

if sys.version_info[0] == 2:
    from urlparse import urlparse

    # workaround for http://bugs.python.org/issue7904 - Python < 2.7
    if urlparse('s3://bucket/key').netloc != 'bucket':
        from urlparse import uses_netloc
        uses_netloc.append('s3')

    # workaround for http://bugs.python.org/issue9374 - Python < 2.7.4
    if urlparse('s3://bucket/key?key=value').query != 'key=value':
        from urlparse import uses_query
        uses_query.append('s3')
Пример #6
0
def parse_url(url):
    """
    Parses a URL like, 'ssh://user@host:22' and returns a dict of::

        {
            'scheme': scheme,
            'user': user,
            'host': host,
            'port': port,
            'password': password,
            'provider': provider,
            'identity': identity,
            'debug': debug
        }

    .. note:: 'web+ssh://' URLs are also supported.

    If an ssh URL is given without a username, os.environ['GO_USER'] will be
    used and if that doesn't exist it will fall back to os.environ['USER'].

    SSH identity may be specified as a query string:

        ssh://user@host:22/?provider=name&identity=id_rsa

    .. note::

        *password*, *provider*, and *identity* may be returned as None
    """
    provider = None
    identity = None
    debug = False

    try:
        from urlparse import urlparse, parse_qs, uses_query

        if "ssh" not in uses_query:  # Only necessary in Python 2.X
            uses_query.append("ssh")
    except ImportError:  # Python 3
        from urllib.parse import urlparse, parse_qs

    parsed = urlparse(url)
    if parsed.query:
        q_attrs = parse_qs(parsed.query)
        provider = q_attrs.get("provider")[0]
        identity = q_attrs.get("identity")[0]
        debug = q_attrs.get("debug", False)
        if debug:  # Passing anything turns on debug
            debug = True

    if parsed.port:
        port = parsed.port
    else:
        port = socket.getservbyname(parsed.scheme, "tcp")

    return {
        "scheme": parsed.scheme,
        "user": parsed.username,
        "host": parsed.hostname,
        "port": port,
        "password": parsed.password,
        "provider": provider,
        "identity": identity,
        "debug": debug,
    }