コード例 #1
0
def _add_auth(auth, headers, auth_type='basic'):
    """Adds authentication key to headers.

    :param dict headers: Initial headers dictionary.
    :param str auth_type: Either 'basic' or 'jwt'.
    :return: The modified headers dictionary
    """
    headers = headers or {}
    # The auth header to add
    header = None
    if auth_type == 'basic':
        if isinstance(auth, (tuple, list)) and len(auth) == 2:
            header = _basic_auth_str(*auth)
    elif auth_type == 'jwt':
        token = auth[0] if isinstance(auth, (tuple, list)) else auth
        header = ' '.join(['Bearer', token])
    else:
        raise ValueError('Auth type not supported: {0!r}'.format(auth_type))

    if PY2:
        auth_header = binary_type(header)
    else:
        auth_header = header
    headers['Authorization'] = auth_header
    return headers
コード例 #2
0
def _add_auth(auth, headers):
    '''Adds authentication key to headers.'''
    headers = headers or {}
    if isinstance(auth, (tuple, list)) and len(auth) == 2:
        if PY2:
            auth_header = binary_type(_basic_auth_str(*auth))
        else:
            auth_header = _basic_auth_str(*auth)
        headers["Authorization"] = auth_header
    return headers