Esempio n. 1
0
def login(request):
    """
    Login view

    """
    if request.method == 'POST':
        form = AuthenticationForm(data=request.POST)
        if form.is_valid():
            user = authenticate(email=request.POST['email'],
                                password=request.POST['password'])
            if user is not None:
                if settings.DEBUG:
                    print("User is not Empty!")
                if user.is_active:
                    django_login(request, user)
                    return redirect('/')
    else:
        form = AuthenticationForm()
    return render_to_response('registration/login.html', {
        'form': form,
    }, context_instance=RequestContext(request))
Esempio n. 2
0
def login(request):
    """
    Login view

    """
    if request.method == 'POST':
        form = AuthenticationForm(data=request.POST)
        if form.is_valid():
            user = authenticate(email=request.POST['email'],
                                password=request.POST['password'])
            if user is not None:
                if settings.DEBUG:
                    print("User is not Empty!")
                if user.is_active:
                    django_login(request, user)
                    return redirect('/')
    else:
        form = AuthenticationForm()
    return render_to_response('registration/login.html', {
        'form': form,
    }, context_instance=RequestContext(request))
Esempio n. 3
0
def sms_login(request, *args, **kwargs):

    # Check session variables to find information carried forward.
    access_field = settings.USERNAME_FIELD
    # This is the key field name. Probably username or email

    if access_field in request.session:
        if request.session[access_field] != "":
            access_key = request.session[access_field]
        else:
            access_key = ""
    else:
        access_key = ""
    if settings.DEBUG:
        # print(request.GET)
        print("SMS_LOGIN.GET:", access_field, ":[%s]" % (access_key))
        # print(request.POST)
        print(args)
        print(kwargs)
    next = ""

    # Passing next parameter through to form
    if request.GET:
        if 'next' in request.GET:
            next = request.GET['next']
            next = request.get_full_path().split('next=')[1]

    if settings.DEBUG:
        print("We got a next value of:", next)
        print("full path = ", request.get_full_path())
    if request.method == 'POST':
        form = AuthenticationForm(request.POST)
        if request.POST['login'].lower() == 'resend code':
            if settings.DEBUG:
                print("Resending Code for %s" % request.POST[access_field])
            # form = SMSCodeForm(request.POST)
            # form.username = request.POST['username']
            request.session[access_field] = request.POST[access_field]
            return HttpResponseRedirect(reverse('accounts:sms_code'))
        if form.is_valid():
            print("Authenticating...")
            access_key = form.cleaned_data[access_field] # .lower()
            password = form.cleaned_data['password'] # .lower()
            sms_code = form.cleaned_data['sms_code']

            if not validate_sms(access_key=access_key, smscode=sms_code):
                messages.error(request, "Invalid Access Code.")
                return render_to_response('accounts/login.html',
                                          {'form': AuthenticationForm(),
                                           'next': next},
                                          RequestContext(request))
            # DONE: Trying to handle LDAP Errors. eg. Not available
            try:
                user = authenticate(username=access_key, password=password)
            except (ldap3.LDAPBindError,
                    ldap3.LDAPSASLPrepError,
                    ldap3.LDAPSocketOpenError):
                print("We got an LDAP Error - Bind:",dir(ldap3.LDAPBindError),
                    "\nSASL Prep:", ldap3.LDAPSASLPrepError,
                    "\nSocketOpenError:",ldap3.LDAPSocketOpenError)
                messages.error(request, "We had a problem reaching the Directory Server")
                return render_to_response('accounts/login.html', {'next': next},
                                      RequestContext(request))

            #######

            if user is not None:

                if user.is_active:
                    django_login(request, user)

                    # DONE: Set a session variable to identify as
                    # master account and not a subacc
                    session_device(request,
                                    "True",
                                    Session="auth_master")

                    # DONE: Now Send a message on login
                    if user.notify_activity in "ET":
                        send_activity_message(request,
                                              user)
                    # Otherwise don't send a message
                    if next != "":
                        if settings.DEBUG:
                            print("About to redirect to:", next )
                            print("QUERY_DICT:", dict(request.POST.items()))
                            print("but what about kwargs", kwargs)
                        return HttpResponseRedirect(next)
                    else:
                        return HttpResponseRedirect(reverse('home'))
                else:

                    messages.error(request, "Your account is not active.")
                    args = {'next': next}
                    return HttpResponseRedirect(reverse('sms_code', args))
            else:
                messages.error(request, "Invalid username or password.")
                return render_to_response('accounts/login.html',
                                          {'form': AuthenticationForm(),
                                           'next': next},
                                          RequestContext(request))
        else:
            print("Error with the POST form", )
            return render_to_response('accounts/login.html',
                                      {'form': form,
                                       'next': next},
                                      RequestContext(request))
    else:
        if access_field in request.session:
            access_key = request.session[access_field]
        else:
            access_key = ""
        if settings.DEBUG:
            print("in sms_login. Setting up Form [", access_key, "]")

        form = AuthenticationForm(initial={access_field: access_key})
    if settings.DEBUG:
        # print(form)
        print("Dropping to render_to_response in sms_login")

    return render_to_response('accounts/login.html', {'form': form,
                                                      'next': next},
                              RequestContext(request))
Esempio n. 4
0
def sms_login(request, *args, **kwargs):

    # Check session variables to find information carried forward.
    access_field = settings.USERNAME_FIELD
    # This is the key field name. Probably username or email

    if access_field in request.session:
        if request.session[access_field] != "":
            access_key = request.session[access_field]
        else:
            access_key = ""
    else:
        access_key = ""
    if settings.DEBUG:
        # print(request.GET)
        print("SMS_LOGIN.GET:", access_field, ":[%s]" % (access_key))
        # print(request.POST)
        print(args)
        print(kwargs)
    next = ""

    # Passing next parameter through to form
    if request.GET:
        if 'next' in request.GET:
            next = request.GET['next']
            next = request.get_full_path().split('next=')[1]

    if settings.DEBUG:
        print("We got a next value of:", next)
        print("full path = ", request.get_full_path())
    if request.method == 'POST':
        form = AuthenticationForm(request.POST)
        if request.POST['login'].lower() == 'resend code':
            if settings.DEBUG:
                print("Resending Code for %s" % request.POST[access_field])
            # form = SMSCodeForm(request.POST)
            # form.username = request.POST['username']
            request.session[access_field] = request.POST[access_field]
            return HttpResponseRedirect(reverse('accounts:sms_code'))
        if form.is_valid():
            print("Authenticating...")
            access_key = form.cleaned_data[access_field]  # .lower()
            password = form.cleaned_data['password']  # .lower()
            sms_code = form.cleaned_data['sms_code']

            if not validate_sms(access_key=access_key, smscode=sms_code):
                messages.error(request, "Invalid Access Code.")
                return render_to_response('accounts/login.html', {
                    'form': AuthenticationForm(),
                    'next': next
                }, RequestContext(request))
            # DONE: Trying to handle LDAP Errors. eg. Not available
            try:
                user = authenticate(username=access_key, password=password)
            except (ldap3.LDAPBindError, ldap3.LDAPSASLPrepError,
                    ldap3.LDAPSocketOpenError):
                print("We got an LDAP Error - Bind:", dir(ldap3.LDAPBindError),
                      "\nSASL Prep:", ldap3.LDAPSASLPrepError,
                      "\nSocketOpenError:", ldap3.LDAPSocketOpenError)
                messages.error(
                    request, "We had a problem reaching the Directory Server")
                return render_to_response('accounts/login.html',
                                          {'next': next},
                                          RequestContext(request))

            #######

            if user is not None:

                if user.is_active:
                    django_login(request, user)

                    # DONE: Set a session variable to identify as
                    # master account and not a subacc
                    session_device(request, "True", Session="auth_master")

                    # DONE: Now Send a message on login
                    if user.notify_activity in "ET":
                        send_activity_message(request, user)
                    # Otherwise don't send a message
                    if next != "":
                        if settings.DEBUG:
                            print("About to redirect to:", next)
                            print("QUERY_DICT:", dict(request.POST.items()))
                            print("but what about kwargs", kwargs)
                        return HttpResponseRedirect(next)
                    else:
                        return HttpResponseRedirect(reverse('home'))
                else:

                    messages.error(request, "Your account is not active.")
                    args = {'next': next}
                    return HttpResponseRedirect(reverse('sms_code', args))
            else:
                messages.error(request, "Invalid username or password.")
                return render_to_response('accounts/login.html', {
                    'form': AuthenticationForm(),
                    'next': next
                }, RequestContext(request))
        else:
            print("Error with the POST form", )
            return render_to_response('accounts/login.html', {
                'form': form,
                'next': next
            }, RequestContext(request))
    else:
        if access_field in request.session:
            access_key = request.session[access_field]
        else:
            access_key = ""
        if settings.DEBUG:
            print("in sms_login. Setting up Form [", access_key, "]")

        form = AuthenticationForm(initial={access_field: access_key})
    if settings.DEBUG:
        # print(form)
        print("Dropping to render_to_response in sms_login")

    return render_to_response('accounts/login.html', {
        'form': form,
        'next': next
    }, RequestContext(request))
Esempio n. 5
0
def sms_login(request, *args, **kwargs):
    if 'email' in request.session:
        if request.session['email'] != "":
            email = request.session['email']
        else:
            email = ""
    else:
        email = ""
    if settings.DEBUG:
        print(request.GET)
        print("SMS_LOGIN.GET:email:[%s]" % (email))
        print(request.POST)
        print(args)

    if request.method == 'POST':
        form = AuthenticationForm(request.POST)
        if request.POST['login'].lower() == 'resend code':
            if settings.DEBUG:
                print("Resending Code for %s" % request.POST['email'])
            #form = SMSCodeForm(request.POST)
            #form.email = request.POST['email']
            request.session['email'] = request.POST['email']
            return HttpResponseRedirect(reverse('accounts:sms_code'))
        if form.is_valid():
            #print "Authenticate"
            email = form.cleaned_data['email']
            password = form.cleaned_data['password']
            sms_code = form.cleaned_data['sms_code']
            if not validate_sms(username=email, smscode=sms_code):
                messages.error(request, "Invalid Access Code.")
                return render_to_response('accounts/login.html',
                                          {'form': AuthenticationForm()},
                                          RequestContext(request))

            user = authenticate(username=email, password=password)

            if user is not None:

                if user.is_active:
                    django_login(request, user)
                    return HttpResponseRedirect(reverse('home'))
                else:

                    messages.error(request, "Your account is not active.")
                    return HttpResponseRedirect(reverse('sms_code'))
            else:
                messages.error(request, "Invalid username or password.")
                return render_to_response('accounts/login.html',
                                          {'form': AuthenticationForm()},
                                          RequestContext(request))
        else:
            return render_to_response('accounts/login.html', {'form': form},
                                      RequestContext(request))
    else:
        if 'email' in request.session:
            email = request.session['email']
        else:
            email = ""
        if settings.DEBUG:
            print("in sms_login. Setting up Form [", email, "]")
        form = AuthenticationForm(initial={
            'email': email,
        })
    if settings.DEBUG:
        print(form)
    return render_to_response('accounts/login.html', {'form': form},
                              RequestContext(request))
Esempio n. 6
0
def sms_login(request, *args, **kwargs):
    if 'email' in request.session:
        if request.session['email'] != "":
            email = request.session['email']
        else:
            email = ""
    else:
        email = ""
    if settings.DEBUG:
        # print(request.GET)
        print("SMS_LOGIN.GET:email:[%s]" % (email))
        # print(request.POST)
        print(args)

    if request.method == 'POST':
        form = AuthenticationForm(request.POST)
        if request.POST['login'].lower() == 'resend code':
            if settings.DEBUG:
                print("Resending Code for %s" % request.POST['email'])
            # form = SMSCodeForm(request.POST)
            # form.email = request.POST['email']
            request.session['email'] = request.POST['email']
            return HttpResponseRedirect(reverse('accounts:sms_code'))
        if form.is_valid():
            # print("Authenticate")
            email = form.cleaned_data['email'].lower()
            password = form.cleaned_data['password'].lower()
            sms_code = form.cleaned_data['sms_code']
            if not validate_sms(username=email, smscode=sms_code):
                messages.error(request, "Invalid Access Code.")
                return render_to_response('accounts/login.html',
                                          {'form': AuthenticationForm()},
                                          RequestContext(request))
            # DONE: Trying to handle LDAP Errors. eg. Not available
            try:
                user = authenticate(username=email, password=password)
            except (ldap3.LDAPBindError,
                    ldap3.LDAPSASLPrepError,
                    ldap3.LDAPSocketOpenError):
                print("We got an LDAP Error - Bind:",dir(ldap3.LDAPBindError),
                    "\nSASL Prep:", ldap3.LDAPSASLPrepError,
                    "\nSocketOpenError:",ldap3.LDAPSocketOpenError)
                messages.error(request, "We had a problem reaching the Directory Server")
                return render_to_response('accounts/login.html',
                                      RequestContext(request))

            #######

            if user is not None:

                if user.is_active:
                    django_login(request, user)

                    # DONE: Set a session variable to identify as
                    # master account and not a subacc

                    session_device(request,
                                   "True",
                                   Session="auth_master")
                    # DONE: Now Send a message on login
                    if user.notify_activity in "ET":
                        send_activity_message(request,
                                              user)
                    # Otherwise don't send a message

                    return HttpResponseRedirect(reverse('home'))
                else:

                    messages.error(request, "Your account is not active.")
                    return HttpResponseRedirect(reverse('sms_code'))
            else:
                messages.error(request, "Invalid username or password.")
                return render_to_response('accounts/login.html',
                                          {'form': AuthenticationForm()},
                                          RequestContext(request))
        else:
            return render_to_response('accounts/login.html',
                                      {'form': form},
                                      RequestContext(request))
    else:
        if 'email' in request.session:
            email = request.session['email']
        else:
            email = ""
        if settings.DEBUG:
            print("in sms_login. Setting up Form [", email, "]")
        form = AuthenticationForm(initial={'email': email, })
    if settings.DEBUG:
        # print(form)
        print("Dropping to render_to_response in sms_login")
    return render_to_response('accounts/login.html', {'form': form},
                              RequestContext(request))
Esempio n. 7
0
def sms_login(request, *args, **kwargs):
    if 'email' in request.session:
        if request.session['email'] != "":
            email = request.session['email']
        else:
            email = ""
    else:
        email = ""
    if settings.DEBUG:
        print(request.GET)
        print("SMS_LOGIN.GET:email:[%s]" % (email))
        print(request.POST)
        print(args)

    if request.method == 'POST':
        form = AuthenticationForm(request.POST)
        if request.POST['login'].lower() == 'resend code':
            if settings.DEBUG:
                print("Resending Code for %s" % request.POST['email'])
            #form = SMSCodeForm(request.POST)
            #form.email = request.POST['email']
            request.session['email'] = request.POST['email']
            return HttpResponseRedirect(reverse('accounts:sms_code'))
        if form.is_valid():
            #print "Authenticate"
            email = form.cleaned_data['email']
            password = form.cleaned_data['password']
            sms_code = form.cleaned_data['sms_code']
            if not validate_sms(username=email, smscode=sms_code):
                messages.error(request, "Invalid Access Code.")
                return render_to_response('accounts/login.html',
                                          {'form': AuthenticationForm()},
                              RequestContext(request))

            user=authenticate(username=email, password=password)

            if user is not None:

                if user.is_active:
                    django_login(request, user)
                    return HttpResponseRedirect(reverse('home'))
                else:

                    messages.error(request, "Your account is not active.")
                    return HttpResponseRedirect(reverse('sms_code'))
            else:
                messages.error(request, "Invalid username or password.")
                return render_to_response('accounts/login.html',
                                          {'form': AuthenticationForm()},
                              RequestContext(request))
        else:
            return render_to_response('accounts/login.html', {'form': form},
                              RequestContext(request))
    else:
        if 'email' in request.session:
            email = request.session['email']
        else:
            email = ""
        if settings.DEBUG:
            print("in sms_login. Setting up Form [", email, "]")
        form = AuthenticationForm(initial={'email': email, })
    if settings.DEBUG:
        print(form)
    return render_to_response('accounts/login.html', {'form': form},
                              RequestContext(request))
Esempio n. 8
0
def sms_login(request, *args, **kwargs):

    # Step 2 of the login process.
    if settings.USERNAME_FIELD in request.session:
        if request.session[settings.USERNAME_FIELD] != "":
            key_field = request.session[settings.USERNAME_FIELD]
        else:
            key_field = ""
    else:
        key_field = ""
    if settings.DEBUG:
        # print(request.GET)
        print("SMS_LOGIN.GET:%s:[%s]" % (settings.USERNAME_FIELD, key_field))
        # print(request.POST)
        print("args:", args)

    if request.method == 'POST':
        form = AuthenticationForm(request.POST)
        if request.POST['login'].lower() == 'resend code':
            if settings.DEBUG:
                print("Resending Code for %s" % request.POST[settings.USERNAME_FIELD])
            # form = SMSCodeForm(request.POST)
            # form.email = request.POST['email']
            request.session[settings.USERNAME_FIELD] = request.POST[settings.USERNAME_FIELD]
            return HttpResponseRedirect(reverse('accounts:sms_code'))
        if form.is_valid():
            print("Authenticating...")
            key_field = form.cleaned_data[settings.USERNAME_FIELD].lower()
            password = form.cleaned_data['password'].lower()
            sms_code = form.cleaned_data['sms_code']
            if settings.DEBUG:
                print("working with ", key_field)
            if not validate_sms(username=key_field, smscode=sms_code):
                messages.error(request, "Invalid Access Code.")
                if settings.DEBUG:
                    print("Going to sms_login loop back")
                return render_to_response('accounts/login.html',
                                          {'form': AuthenticationForm()},
                                          RequestContext(request))
            # DONE: Trying to handle LDAP Errors. eg. Not available
            check = User.objects.get(user=key_field)
            if settings.DEBUG:
                print("checking with ", key_field, "/", check)
            try:
                # user = authenticate(user=key_field, password=password)
                user = authenticate(email=check.email, password=password)
                if settings.DEBUG:
                    print("Authenticated User:"******"We got an LDAP Error - Bind:",dir(ldap3.LDAPBindError),
                    "\nSASL Prep:", ldap3.LDAPSASLPrepError,
                    "\nSocketOpenError:",ldap3.LDAPSocketOpenError)
                messages.error(request, "We had a problem reaching the Directory Server")
                return render_to_response('accounts/login.html',
                                      RequestContext(request))

            #######
            if settings.DEBUG:
                print("authentication with", user)

            if user is not None:

                if user.is_active:
                    django_login(request, user)

                    # DONE: Set a session variable to identify as
                    # master account and not a subacc

                    # session_device(request,
                    #                "True",
                    #                Session="auth_master")
                    # DONE: Now Send a message on login
                    request.session['auth_master']= "True"
                    if user.notify_activity in "ET":
                        send_activity_message(request,
                                              user)
                    # Otherwise don't send a message

                    return HttpResponseRedirect(reverse('home'))
                else:

                    messages.error(request, "Your account is not active.")
                    return HttpResponseRedirect(reverse('sms_code'))
            else:
                messages.error(request, "Invalid username or password.")
                return render_to_response('accounts/login.html',
                                          {'form': AuthenticationForm()},
                                          RequestContext(request))
        else:
            return render_to_response('accounts/login.html',
                                      {'form': form},
                                      RequestContext(request))
    else:
        if settings.USERNAME_FIELD in request.session:
            key_field = request.session[settings.USERNAME_FIELD]
        else:
            key_field = ""
        if settings.DEBUG:
            print("in sms_login. Setting up Form [", settings.USERNAME_FIELD, "]")
        form = AuthenticationForm(initial={settings.USERNAME_FIELD: key_field, })
    if settings.DEBUG:
        # print(form)
        print("Dropping to render_to_response in sms_login")
    return render_to_response('accounts/login.html', {'form': form},
                              RequestContext(request))