#-*-coding:utf-8-*-
import hashlib
from urlparse import urlparse, urljoin
from flask import request

def md5(password):
    return hashlib.md5(password).hexdigest()


def is_safe_url(target):
    ref_url = urlparse(request.host_url)
    test_url = urlparse(urljoin(request.host_url, target))
    return test_url.scheme in ('http', 'https') and\
           ref_url.netloc == test_url.netloc


def get_redirect_target():
    for target in request.args.get('next'), request.referrer:
        if not target:
            continue
        if is_safe_url(target):
            return target