Esempio n. 1
0
def local_login(request, template_name=None, redirect_url=None, redirect_viewname=None):
    message = None
    if "next" in request.REQUEST:
        next = request.REQUEST['next']
    elif not redirect_url:
        if redirect_viewname != None:
            next = reverse(redirect_viewname)
        else:
            next = settings.RESOURCE_REDIRECT_URL
    else:
        next = redirect_url

    if request.method == 'POST':
        form = AuthenticationForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            user = authenticate(username=username, password=password)
            log.debug("Auhentication")
            if user is not None:
                login(request, user)
                log.debug("Login")
                institution = authentication_tools.get_institution(request)
                authentication_tools.add_session_username(request, username)
                return HttpResponseRedirect(next)
            else:
                message = "Invalid username or password"
                form = AuthenticationForm()
    else:
        form = AuthenticationForm()
    return render_to_response(template_name,
            { 'form' : form,
            'next' : next,
            'message' : message, },
            context_instance=RequestContext(request))
Esempio n. 2
0
def openid_login_complete(request):
    institution = authentication_tools.get_institution(request)
    resource_redirect_url = request.GET['next']
    session = request.session

    host = authentication_tools.get_url_host(request)
    nonce = request.GET['janrain_nonce']
    if not "" == settings.OPENID_COMPLETE_URL:
        url = openid_tools.get_return_url(host, nonce,
                                          settings.OPENID_COMPLETE_URL)
    else:
        url = openid_tools.get_return_url(host, nonce)

    query_dict = dict([(k.encode('utf8'), v.encode('utf8'))
                       for k, v in request.GET.items()])

    status, username = openid_tools.complete_openid(session, query_dict, url)

    if status == "SUCCESS":
        username = username
        user = authenticate(username=username)
        if user is not None:
            log.debug("Logging user in")
            login(request, user)
            authentication_tools.add_session_username(request,
                                                      username.split('@')[0])
            log.debug("Redirecting to " + resource_redirect_url)
            return HttpResponseRedirect(resource_redirect_url)
        else:
            log.debug("No user found")
            return HttpResponseRedirect(settings.LOGIN_URL)

    elif status == "CANCEL":
        message = "OpenID login failed due to a cancelled request.  This can be due to failure to release email address which is required by the service."
        return render_to_response('idpauth/openid.html', {
            'message': message,
            'next': resource_redirect_url,
        },
                                  context_instance=RequestContext(request))
    elif status == "FAILURE":
        return render_to_response('idpauth/openid.html', {
            'message': username,
            'next': resource_redirect_url,
        },
                                  context_instance=RequestContext(request))

    else:
        message = "An error was encountered"
        return render_to_response('idpauth/openid.html', {
            'message': message,
            'next': resource_redirect_url,
        },
                                  context_instance=RequestContext(request))
Esempio n. 3
0
def openid_login_complete(request):
    institution = authentication_tools.get_institution(request)
    resource_redirect_url = request.GET['next']
    session = request.session
    
    host = authentication_tools.get_url_host(request)
    nonce = request.GET['janrain_nonce']
    if not "" == settings.OPENID_COMPLETE_URL:
        url = openid_tools.get_return_url(host, nonce, settings.OPENID_COMPLETE_URL)
    else:
        url = openid_tools.get_return_url(host, nonce)
    
    query_dict = dict([
        (k.encode('utf8'), v.encode('utf8')) for k, v in request.GET.items()])
    
    status, username = openid_tools.complete_openid(session, query_dict, url)

    if status == "SUCCESS":
        username = username
        user = authenticate(username=username)
        if user is not None:
            log.debug("Logging user in")
            login(request, user)
            authentication_tools.add_session_username(request, username.split('@')[0])
            log.debug("Redirecting to " + resource_redirect_url)
            return HttpResponseRedirect(resource_redirect_url)
        else:
            log.debug("No user found")
            return HttpResponseRedirect(settings.LOGIN_URL)   
        
    elif status == "CANCEL":
        message = "OpenID login failed due to a cancelled request.  This can be due to failure to release email address which is required by the service."
        return render_to_response('idpauth/openid.html',
        {'message' : message,
        'next' : resource_redirect_url,},
        context_instance=RequestContext(request))
    elif status == "FAILURE":
        return render_to_response('idpauth/openid.html',
        {'message' : username,
        'next' : resource_redirect_url,},
        context_instance=RequestContext(request))

    else:
        message = "An error was encountered"
        return render_to_response('idpauth/openid.html',
        {'message' : message,
        'next' : resource_redirect_url, },
        context_instance=RequestContext(request))
Esempio n. 4
0
def ldap_login(request):
    username = request.POST['username']
    password = request.POST['password']
    resource_redirect_url = request.POST['next']

    institution = authentication_tools.get_institution(request)
    identityprovider = IdentityProviderLDAP.objects.filter(
        institution__iexact=str(institution))
    if identityprovider:
        server = identityprovider[0]
        roles = ldap_tools.get_ldap_roles(server, username, password)

        username = institution + "++" + username
        user = authenticate(username=username)
        roles = str(roles).strip('[').strip(']')
        try:
            user_profile = user.get_profile()
            user_profile.ldap_roles = roles
            user_profile.save()
        except ObjectDoesNotExist:
            user_profile = UserProfile(user=user, ldap_roles=roles)
            user_profile.save()

        if user is not None:
            if roles == None:
                log.debug("Roles were none, redirecting to login")
                return HttpResponseRedirect(settings.LOGIN_URL)
            else:
                log.debug("Logging user in")
                login(request, user)
                authentication_tools.add_session_username(request, username)
                log.debug("Redirecting to " + resource_redirect_url)
                return HttpResponseRedirect(resource_redirect_url)
        else:
            log.debug("No user found")
            return HttpResponseRedirect(settings.LOGIN_URL)
    else:
        message = 'There were errors retrieving the identity provider'
        return render_to_response('idpauth/ldap.html', {
            'next': resource_redirect_url,
            'message': message
        },
                                  context_instance=RequestContext(request))
Esempio n. 5
0
def ldap_login(request):
    username = request.POST['username']
    password = request.POST['password']
    resource_redirect_url = request.POST['next']

    institution = authentication_tools.get_institution(request)
    identityprovider = IdentityProviderLDAP.objects.filter(institution__iexact=str(institution))
    if identityprovider:
        server = identityprovider[0]
        roles = ldap_tools.get_ldap_roles(server, username, password)

        username = institution + "++" + username
        user = authenticate(username=username)
        roles = str(roles).strip('[').strip(']')
        try:
            user_profile = user.get_profile()
            user_profile.ldap_roles = roles
            user_profile.save()
        except ObjectDoesNotExist:
            user_profile = UserProfile(user=user, ldap_roles=roles)
            user_profile.save()

        if user is not None:
            if roles == None:
                log.debug("Roles were none, redirecting to login")
                return HttpResponseRedirect(settings.LOGIN_URL)
            else:
                log.debug("Logging user in")
                login(request, user)
                authentication_tools.add_session_username(request, username)
                log.debug("Redirecting to " + resource_redirect_url)
                return HttpResponseRedirect(resource_redirect_url)
        else:
            log.debug("No user found")
            return HttpResponseRedirect(settings.LOGIN_URL)
    else:
        message = 'There were errors retrieving the identity provider'
        return render_to_response('idpauth/ldap.html', 
        {'next' : resource_redirect_url,
        'message' : message},
        context_instance=RequestContext(request))
Esempio n. 6
0
def local_login(request,
                template_name=None,
                redirect_url=None,
                redirect_viewname=None):
    message = None
    if "next" in request.REQUEST:
        next = request.REQUEST['next']
    elif not redirect_url:
        if redirect_viewname != None:
            next = reverse(redirect_viewname)
        else:
            next = settings.RESOURCE_REDIRECT_URL
    else:
        next = redirect_url

    if request.method == 'POST':
        form = AuthenticationForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            user = authenticate(username=username, password=password)
            log.debug("Auhentication")
            if user is not None:
                login(request, user)
                log.debug("Login")
                institution = authentication_tools.get_institution(request)
                authentication_tools.add_session_username(request, username)
                return HttpResponseRedirect(next)
            else:
                message = "Invalid username or password"
                form = AuthenticationForm()
    else:
        form = AuthenticationForm()
    return render_to_response(template_name, {
        'form': form,
        'next': next,
        'message': message,
    },
                              context_instance=RequestContext(request))