# Path: social/utils.py
# def sanitize_redirect(host, redirect_to):
#     """
#     Given the hostname and an untrusted URL to redirect to,
#     this method tests it to make sure it isn't garbage/harmful
#     and returns it, else returns None, similar as how's it done
#     on django.contrib.auth.views.
#     """
#     # Quick sanity check.
#     if not redirect_to or not isinstance(redirect_to, six.string_types):
#         return None
# 
#     # Heavier security check, don't allow redirection to a different host.
#     netloc = urlparse(redirect_to)[1]
#     if netloc and netloc != host:
#         return None
#     return redirect_to
# 
# def user_is_authenticated(user):
#     if user and hasattr(user, 'is_authenticated'):
#         if isinstance(user.is_authenticated, collections.Callable):
#             authenticated = user.is_authenticated()
#         else:
#             authenticated = user.is_authenticated
#     elif user:
#         authenticated = True
#     else:
#         authenticated = False
#     return authenticated
# 
# def user_is_active(user):
#     if user and hasattr(user, 'is_active'):
#         if isinstance(user.is_active, collections.Callable):
#             is_active = user.is_active()
#         else:
#             is_active = user.is_active
#     elif user:
#         is_active = True
#     else:
#         is_active = False
#     return is_active


from social.p3 import quote
from social.utils import sanitize_redirect, user_is_authenticated, \
                         user_is_active


def do_auth(strategy, redirect_name='next'):
    # Save any defined next value into session
    data = strategy.request_data(merge=False)
    # Save extra data into session.
    for field_name in strategy.setting('FIELDS_STORED_IN_SESSION', []):
        if field_name in data:
            strategy.session_set(field_name, data[field_name])

    if redirect_name in data:
        # Check and sanitize a user-defined GET/POST next field value
        redirect_uri = data[redirect_name]
        if strategy.setting('SANITIZE_REDIRECTS', True):
            redirect_uri = sanitize_redirect(strategy.request_host(),
                                             redirect_uri)
        strategy.session_set(
            redirect_name,
            redirect_uri or strategy.setting('LOGIN_REDIRECT_URL')
        )
    return strategy.start()


def do_complete(strategy, login, user=None, redirect_name='next',
                *args, **kwargs):
    # pop redirect value before the session is trashed on login()
    data = strategy.request_data()
    redirect_value = strategy.session_get(redirect_name, '') or \
                     data.get(redirect_name, '')


# Path: social/utils.py
# def sanitize_redirect(host, redirect_to):
#     """
#     Given the hostname and an untrusted URL to redirect to,
#     this method tests it to make sure it isn't garbage/harmful
#     and returns it, else returns None, similar as how's it done
#     on django.contrib.auth.views.
#     """
#     # Quick sanity check.
#     if not redirect_to or not isinstance(redirect_to, six.string_types):
#         return None
# 
#     # Heavier security check, don't allow redirection to a different host.
#     netloc = urlparse(redirect_to)[1]
#     if netloc and netloc != host:
#         return None
#     return redirect_to
# 
# def user_is_authenticated(user):
#     if user and hasattr(user, 'is_authenticated'):
#         if isinstance(user.is_authenticated, collections.Callable):
#             authenticated = user.is_authenticated()
#         else:
#             authenticated = user.is_authenticated
#     elif user:
#         authenticated = True
#     else:
#         authenticated = False
#     return authenticated
# 
# def user_is_active(user):
#     if user and hasattr(user, 'is_active'):
#         if isinstance(user.is_active, collections.Callable):
#             is_active = user.is_active()
#         else:
#             is_active = user.is_active
#     elif user:
#         is_active = True
#     else:
#         is_active = False
#     return is_active


# Path: social/actions.py
from social.p3 import quote
from social.utils import sanitize_redirect, user_is_authenticated, \
                         user_is_active



def do_auth(strategy, redirect_name='next'):
    # Save any defined next value into session
    data = strategy.request_data(merge=False)
    # Save extra data into session.
    for field_name in strategy.setting('FIELDS_STORED_IN_SESSION', []):
        if field_name in data:
            strategy.session_set(field_name, data[field_name])

    if redirect_name in data:
        # Check and sanitize a user-defined GET/POST next field value
        redirect_uri = data[redirect_name]
        if strategy.setting('SANITIZE_REDIRECTS', True):
            redirect_uri = sanitize_redirect(strategy.request_host(),
                                             redirect_uri)
        strategy.session_set(
            redirect_name,
            redirect_uri or strategy.setting('LOGIN_REDIRECT_URL')
        )
    return strategy.start()


def do_complete(strategy, login, user=None, redirect_name='next',
                *args, **kwargs):
    # pop redirect value before the session is trashed on login()
    data = strategy.request_data()
    redirect_value = strategy.session_get(redirect_name, '') or \
                     data.get(redirect_name, '')


    is_authenticated = user_is_authenticated(user)