def render_to_response(self, context, **response_kwargs): response = super(BoardView, self).render_to_response(context, **response_kwargs) if self.request.user.is_authenticated: response.set_cookie(key='token', value=self.request.user.auth_token) return response
def tests(request, label): room, created = Gameroom.objects.get_or_create(label = label) messages = room.messages.all()[0] print room.label response = render(request, "cards_db/tests.html", {'room': room, 'messages':messages,}) response.set_cookie('label', room.label) response.set_cookie('username', request.user.username) return response
def render_to_response(self, context, **response_kwargs): response = super(HomeView, self).render_to_response(context, **response_kwargs) if self.request.user.is_authenticated(): if self.request.COOKIES.get("jwt", None) is None: pass response.set_cookie(key="jwt", value=convert_sessionid_to_jwt( self.request.user), domain="localhost") return response
def login(request): name = request.GET['name'] password = request.GET['password'] query = User.objects.filter(name=name,password=password) if len(query) == 0: return JsonResponse({'info':'user not find','noLogin':True}) else: print 'set cookie' response = JsonResponse({'info':query[0].name + ' login','noLogin':False}) response.set_cookie('user',query[0].id,3600) return response
def get(self, request): cookies = request.COOKIES if cookies.get('refresh'): users = UserDetails.objects.all() if cookies.get('access'): return render(request, 'app/home.html', {'users': users}) access = validate_refresh_and_new_token(cookies.get('refresh')) print(access) response = render(request, 'app/home.html', {'users': users}) response.set_cookie('access', access, expires=300) return response return redirect('/login')
def loginAuth(request): serializer = LoginSerializer(data=request.data, context={'request': request}) serializer.is_valid(raise_exception=True) user = serializer.validated_data['user'] token = jwt_encode(user) data = {'user': serializer.data.get("username"), 'token': token} response = Response(data, status=status.HTTP_200_OK) from rest_framework_jwt.settings import api_settings as jwt_settings if jwt_settings.JWT_AUTH_COOKIE: from datetime import datetime expiration = (datetime.utcnow() + jwt_settings.JWT_EXPIRATION_DELTA) response.set_cookie(jwt_settings.JWT_AUTH_COOKIE, token, expires=expiration, httponly=True) return response
def post(self, request): user = authenticate(request, username=request.POST['username'], password=request.POST['pswd']) print(user) if user: login(request, user) refresh = get_user_token(user) response = redirect('/home') response.set_cookie('access', refresh['access'], expires=300) response.set_cookie('refresh', refresh['refresh'], expires=24 * 60 * 60) return response return render(request, 'app/login.html', {'error': 'Incorrect username or password'})
def render_to_response(self, context, **response_kwargs): """ If user is logged in, redirect them to their feed """ response = super(AccessSubdomainsTemplateView, self).render_to_response(context, **response_kwargs) if not self.request.user.has_perm('auth.access_subdomains'): raise Http404 days_expire = 7 max_age = days_expire * 24 * 60 * 60 expires = datetime.strftime( datetime.utcnow() + timedelta(seconds=max_age), "%a, %d-%b-%Y %H:%M:%S GMT") response.set_cookie('access_subdomains', 'true', max_age=max_age, expires=expires, domain=settings.COOKIE_ACCESS_DOMAIN) return response
def embed_bokeh(request, bokeh_app): """Render the requested app from the bokeh server""" # http://bokeh.pydata.org/en/0.12.3/docs/reference/embed.html # TODO: test if bokeh server is reachable bokeh_script = autoload_server(None, app_path="/{}".format(bokeh_app), url=bokeh_url) template = loader.get_template('dashboard/embed_bokeh.html') context = {'bokeh_script': bokeh_script, 'bokeh_app': bokeh_app} response = HttpResponse(template.render(context, request)) # Save full url path in the HTTP response, so that the bokeh # app can use this info, e.g: # http://localhost:8000/dashboard/AMx/?metric=AM1&ci_dataset=cfht&ci_id=452 response.set_cookie('django_full_path', request.get_full_path()) return response
def login(request): if request.method == 'GET': idToken = request.COOKIES.get('idToken') if idToken: info = auth.get_account_info(idToken) if info: return redirect(reverse('projects')) context = {'f': LoginForm()}; return render(request, 'login.html', context); else: f = LoginForm(request.POST); if not f.is_valid(): return render(request, 'login.html', {'f': f}); else: email = f.cleaned_data['email'] password = f.cleaned_data['password'] user = auth.sign_in_with_email_and_password(email, password) # response = (request,'index.html') # django.http.HttpResponse response = redirect(reverse('projects', kwargs={})); response.set_cookie(key='idToken', value=user['idToken']) return response
def logout_view(request): logout(request) response = redirect('/login') response.set_cookie('access', '', expires=1) response.set_cookie('refresh', '', expires=1) return response