Example #1
0
def devpiserver_get_credentials(request):
    """Extracts username and password from Authentication header.

    Returns a tuple with (username, password) if credentials could be
    extracted, or None if no credentials were found.
    """
    # support basic authentication for setup.py upload/register
    authorization = request.headers.get("Authorization")
    if not authorization:
        return None
    try:
        authmeth, auth = authorization.split(" ", 1)
    except ValueError:  # not enough values to unpack
        return None
    if authmeth.lower() != "basic":
        return None

    try:
        authbytes = b64decode(auth.strip())
    except (TypeError, binascii.Error):  # can't decode
        return None

    # try utf-8 first, then latin-1; see discussion in
    # https://github.com/Pylons/pyramid/issues/898
    try:
        auth = authbytes.decode("utf-8")
    except UnicodeDecodeError:
        auth = authbytes.decode("latin-1")

    try:
        username, password = auth.split(":", 1)
    except ValueError:  # not enough values to unpack
        return None
    return username, password
Example #2
0
    def _get_credentials(self, request):
        authorization = request.headers.get('X-Devpi-Auth')
        if not authorization:
            # support basic authentication for setup.py upload/register
            authorization = request.headers.get('Authorization')
            if not authorization:
                return None
            try:
                authmeth, auth = authorization.split(' ', 1)
            except ValueError: # not enough values to unpack
                return None
            if authmeth.lower() != 'basic':
                return None
        else:
            auth = authorization

        try:
            authbytes = b64decode(auth.strip())
        except (TypeError, binascii.Error):  # can't decode
            return None

        # try utf-8 first, then latin-1; see discussion in
        # https://github.com/Pylons/pyramid/issues/898
        try:
            auth = authbytes.decode('utf-8')
        except UnicodeDecodeError:
            auth = authbytes.decode('latin-1')

        try:
            username, password = auth.split(':', 1)
        except ValueError:  # not enough values to unpack
            return None
        return username, password
Example #3
0
def devpiserver_get_credentials(request):
    """Extracts username and password from X-Devpi-Auth header.

    Returns a tuple with (username, password) if credentials could be
    extracted, or None if no credentials were found.
    """
    auth = request.headers.get('X-Devpi-Auth')
    if not auth:
        return None
    try:
        authbytes = b64decode(auth.strip())
    except (TypeError, binascii.Error):  # can't decode
        return None

    # try utf-8 first, then latin-1; see discussion in
    # https://github.com/Pylons/pyramid/issues/898
    try:
        auth = authbytes.decode('utf-8')
    except UnicodeDecodeError:
        auth = authbytes.decode('latin-1')

    try:
        username, password = auth.split(':', 1)
    except ValueError:  # not enough values to unpack
        return None
    return username, password
Example #4
0
    def _get_credentials(self, request):
        authorization = request.headers.get('Authorization')
        if not authorization:
            return None
        try:
            authmeth, auth = authorization.split(' ', 1)
        except ValueError:  # not enough values to unpack
            return None
        if authmeth.lower() == 'bearer':
            return auth
        if authmeth.lower() != 'basic':
            return None

        try:
            authbytes = b64decode(auth.strip())
        except (TypeError, binascii.Error):  # can't decode
            return None

        # try utf-8 first, then latin-1; see discussion in
        # https://github.com/Pylons/pyramid/issues/898
        try:
            auth = authbytes.decode('utf-8')
        except UnicodeDecodeError:  # pragma: no cover
            auth = authbytes.decode('latin-1')

        try:
            username, _ = auth.split(':', 1)
        except ValueError:  # not enough values to unpack
            return None
        return username
def get_token_credentials(request):
    """Helper method for parsing API token credentials."""
    authorization = request.headers.get('Authorization')

    try:
        authmeth, auth = authorization.split(' ', 1)
    except:
        return None
    else:
        # Normalize "authmeth"
        authmeth = authmeth.lower()
        # Make sure "auth" doesn't have any unwanted whitespace
        auth = auth.strip()

    if authmeth == 'token':
        # Parse traditional token value
        try:
            authbytes = b64decode(auth)
        except (TypeError, binascii.Error): # can't decode
            return None

        # Try utf-8 first, then latin-1; see discussion in
        # https://github.com/Pylons/pyramid/issues/898
        try:
            auth = authbytes.decode('utf-8')
        except UnicodeDecodeError:
            auth = authbytes.decode('latin-1')

        try:
            userid, token = auth.split(':', 1)
        except ValueError: # not enough values to unpack
            return None

        return dict(userid=userid, token=token)

    return None