<commit_before>"""
authentication helper methods.

"""
from django.http import HttpResponseRedirect

from atmosphere import settings

from authentication.models import Token as AuthToken
from core.models import AtmosphereUser as User

def cas_logoutRedirect():
    return HttpResponseRedirect(settings.CAS_SERVER +
                                "/cas/logout?service="+settings.SERVER_URL)


def saml_loginRedirect(request, redirect=None, gateway=False):
    login_url = "%s%s/login?service=%s/s_serviceValidater%s" %\
                (settings.CAS_SERVER, settings.CAS_AUTH_PREFIX,
                 settings.SERVER_URL,
                 "?sendback=%s" % redirect if redirect else "")
    if gateway:
        login_url += '&gateway=true'
    return HttpResponseRedirect(login_url)

def cas_loginRedirect(request, redirect=None, gateway=False):
    if not redirect:
        redirect = request.get_full_path()
    redirect_to = "%s/CAS_serviceValidater?sendback=%s" \
            % (settings.SERVER_URL, redirect)
    login_url = "%s%s/login?service=%s" \
            % (settings.CAS_SERVER, settings.CAS_AUTH_PREFIX, redirect_to)
    if gateway:
        login_url += '&gateway=true'
    return HttpResponseRedirect(login_url)


def get_or_create_user(username=None, attributes=None):
    """
    Retrieve or create a User matching the username (No password)
    """
    if not username:
        return None
    try:
        user = User.objects.get(username=username)
    except User.DoesNotExist:
        user = User.objects.create_user(username, "")
    if attributes:
        user.first_name = attributes['firstName']
        user.last_name = attributes['lastName']
        user.email = attributes['email']
    user.save()
    return user

def createAuthToken(username):
    """
    returns a new token for username
    """
    user = User.objects.get(username=username)
    auth_user_token = AuthToken(
        user=user,
        api_server_url=settings.API_SERVER_URL
    )
    auth_user_token.update_expiration()
    auth_user_token.save()
    return auth_user_token
                   <commit_msg>Usernames should ALWAYS be in lowercase, until the restriction is lifted<commit_after>"""
authentication helper methods.

"""
from django.http import HttpResponseRedirect

from atmosphere import settings

from authentication.models import Token as AuthToken
from core.models import AtmosphereUser as User

def cas_logoutRedirect():
    return HttpResponseRedirect(settings.CAS_SERVER +
                                "/cas/logout?service="+settings.SERVER_URL)


def saml_loginRedirect(request, redirect=None, gateway=False):
    login_url = "%s%s/login?service=%s/s_serviceValidater%s" %\
                (settings.CAS_SERVER, settings.CAS_AUTH_PREFIX,
                 settings.SERVER_URL,
                 "?sendback=%s" % redirect if redirect else "")
    if gateway:
        login_url += '&gateway=true'
    return HttpResponseRedirect(login_url)

def cas_loginRedirect(request, redirect=None, gateway=False):
    if not redirect:
        redirect = request.get_full_path()
    redirect_to = "%s/CAS_serviceValidater?sendback=%s" \
            % (settings.SERVER_URL, redirect)
    login_url = "%s%s/login?service=%s" \
            % (settings.CAS_SERVER, settings.CAS_AUTH_PREFIX, redirect_to)
    if gateway:
        login_url += '&gateway=true'
    return HttpResponseRedirect(login_url)


def get_or_create_user(username=None, attributes=None):
    """
    Retrieve or create a User matching the username (No password)
    """
    if not username:
        return None

    # NOTE: REMOVE this when it is no longer true!
    # Force any username lookup to be in lowercase
    username = username.lower()

    try:
        # Look for the username "EXACT MATCH"
        user = User.objects.get(username=username)
    except User.DoesNotExist:
        user = User.objects.create_user(username, "")
    if attributes:
        user.first_name = attributes['firstName']
        user.last_name = attributes['lastName']
        user.email = attributes['email']
    user.save()
    return user

def createAuthToken(username):
    """
    returns a new token for username
    """
    user = User.objects.get(username=username)
    auth_user_token = AuthToken(
        user=user,
        api_server_url=settings.API_SERVER_URL
    )
    auth_user_token.update_expiration()
    auth_user_token.save()
    return auth_user_token
                   
{'fraction_non_alphanumeric': 0.09579491792219474, 'fraction_numerical': 0.0, 'mean_word_length': 3.01806684733514, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1651612', 'n_tokens_mistral': 1290, 'n_tokens_neox': 1257, 'n_words': 289}