def logout(request): ''' Logout functionality ''' auth_token = request.COOKIES.get("auth", None) email = request.COOKIES.get("email", None) try: if auth_token != None and email != None: redis_obj = Redis() old_auth_token = redis_obj.get_value("email:%s:auth.token" %(email)) new_auth_token = get_auth_token() key = "email:%s:auth.token" % (email) redis_obj.set_value(key, new_auth_token) key = "auth.token:%s:email" % (new_auth_token) redis_obj.set_value(key, email) redis_obj.remove_key("auth.token:%s:email" % (old_auth_token)) user_id = get_userId(request) key = "auth.token:%s:userId" %(new_auth_token) redis_obj.set_value(key, user_id) redis_obj.remove_key("auth.token:%s:userId" %(old_auth_token)) key = "userId:%d:auth.token" %(user_id) redis_obj.set_value(key, new_auth_token) except: pass return HttpResponseRedirect('/')
def login(request, redirect_uri='/home'): ''' login functionality which returns a empty form when given a GET request or validates the authentication when given a POST request ''' if request.method == "POST": login_form = LoginForm(data=request.POST) if login_form.is_valid(): login_form_cleaned = login_form.cleaned_data email = login_form_cleaned['email'] redis_obj = Redis() if account_existence(redis_obj, email) == 1: user_id = get_key(redis_obj, email) password = get_password(redis_obj, user_id) if password == encrypt_password(login_form_cleaned['password']): auth_token = get_auth_token() update_auth_token(redis_obj, auth_token, user_id, email) redirect_uri = request.POST.get('redirect_uri', '/home') response = HttpResponseRedirect(redirect_uri) max_age = 7 * 24 * 60 * 60 expires = datetime.datetime.strftime(datetime.datetime.utcnow() + datetime.timedelta(seconds=max_age), "%a, %d-%b-%Y %H:%M:%S GMT") response.set_cookie('auth', auth_token, max_age=max_age, expires=expires) response.set_cookie('email', email, max_age=max_age, expires=expires) return response redirect_uri = request.POST.get('redirect_uri', '/home') signup_form = SignUpForm() return render_to_response(HOME_PAGE_TEMPLATE_PATH, { 'login_form':login_form, 'signup_form':signup_form, 'redirect_uri':redirect_uri, 'signin_error':'invalid username or password' }, context_instance=RequestContext(request)) login_form = LoginForm() signup_form = SignUpForm() return render_to_response(HOME_PAGE_TEMPLATE_PATH, { 'login_form':login_form, 'signup_form':signup_form, 'redirect_uri':redirect_uri }, context_instance=RequestContext(request))
def register(request): ''' signup functionality which returns a empty form when given a GET request or validates n creates an entry in db with POST request ''' if request.method == "POST": signup_form = SignUpForm(data=request.POST) if signup_form.is_valid(): signup_form_cleaned = signup_form.cleaned_data login_form = LoginForm() if signup_form_cleaned['password'] != signup_form_cleaned['password_confirmation']: return render_to_response(HOME_PAGE_TEMPLATE_PATH, { 'login_form':login_form, 'signup_form':signup_form, 'signup_error':'Passwords doesnt match' }, context_instance=RequestContext(request)) if username_exists(signup_form_cleaned['username']) == 1: return render_to_response(HOME_PAGE_TEMPLATE_PATH, { 'login_form':login_form, 'signup_form':signup_form, 'signup_error':'Username already exists' }, context_instance=RequestContext(request)) if email_exists(signup_form_cleaned['email']) == 1: return render_to_response(HOME_PAGE_TEMPLATE_PATH, { 'login_form':login_form, 'signup_form':signup_form, 'signup_error':'Email id has already taken' }, context_instance=RequestContext(request)) password = encrypt_password(signup_form_cleaned['password']) password_confirmation = encrypt_password(signup_form_cleaned['password_confirmation']) signup_form_cleaned['auth_token'] = get_auth_token() store_user_info(signup_form_cleaned) response = HttpResponseRedirect('/home') max_age = 7 * 24 * 60 * 60 expires = datetime.datetime.strftime(datetime.datetime.utcnow() + datetime.timedelta(seconds=max_age), "%a, %d-%b-%Y %H:%M:%S GMT") response.set_cookie('auth', signup_form_cleaned['auth_token'], max_age=max_age, expires=expires) response.set_cookie('email', signup_form_cleaned['email'], max_age=max_age, expires=expires) return response login_form = LoginForm() return render_to_response(HOME_PAGE_TEMPLATE_PATH, { 'login_form':login_form, 'signup_form':signup_form }, context_instance=RequestContext(request)) login_form = LoginForm() signup_form = SignUpForm() return render_to_response(HOME_PAGE_TEMPLATE_PATH, { 'login_form':login_form, 'signup_form':signup_form }, context_instance=RequestContext(request))