Module: check_mk
Branch: master
Commit: 99b45efd9626ff2031ef685126c7e84f65d92ed0
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=99b45efd9626ff…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Mon Jan 7 11:10:28 2019 +0100
Added Response subclass for simplified cookie setting.
Change-Id: I430b33bafeb2d440de6e7876caf4d5a5fec065b1
---
cmk/gui/htmllib.py | 6 ++----
cmk/gui/http.py | 15 +++++++++++++++
cmk/gui/login.py | 14 +++-----------
tests/unit/cmk/gui/conftest.py | 5 ++---
tests/unit/cmk/gui/old/classes.py | 5 ++---
tests/unit/cmk/gui/test_http.py | 17 ++++-------------
web/app/index.wsgi | 6 +-----
7 files changed, 29 insertions(+), 39 deletions(-)
diff --git a/cmk/gui/htmllib.py b/cmk/gui/htmllib.py
index 9bc06f2..6d43700 100644
--- a/cmk/gui/htmllib.py
+++ b/cmk/gui/htmllib.py
@@ -1089,8 +1089,7 @@ class html(HTMLGenerator):
# TODO: Make private
self.mobile = bool(self.var("mobile"))
# Persist the explicitly set state in a cookie to have it maintained through
further requests
- self.response.set_cookie(
- "mobile", str(int(self.mobile)),
secure=html.request.is_ssl_request, httponly=True)
+ self.response.set_http_cookie("mobile", str(int(self.mobile)))
elif self.request.has_cookie("mobile"):
self.mobile = self.request.cookie("mobile", "0") ==
"1"
@@ -1552,8 +1551,7 @@ class html(HTMLGenerator):
cookie_lang = self.request.cookie("language")
if cookie_lang != lang:
if lang is not None:
- self.response.set_cookie(
- "language", lang, secure=self.request.is_ssl_request,
httponly=True)
+ self.response.set_http_cookie("language", lang)
else:
self.del_language_cookie()
diff --git a/cmk/gui/http.py b/cmk/gui/http.py
index d031099..0aafcad 100644
--- a/cmk/gui/http.py
+++ b/cmk/gui/http.py
@@ -30,6 +30,7 @@ import re
import six
import werkzeug.http
+import werkzeug.wrappers
import cmk.gui.log as log
from cmk.gui.i18n import _
@@ -245,3 +246,17 @@ class Request(object):
def uploaded_file(self, varname, default=None):
return self.uploads.get(varname, default)
+
+
+class Response(werkzeug.wrappers.Response):
+ # NOTE: Currently we rely on a *relavtive* Location header in redirects!
+ autocorrect_location_header = False
+
+ def __init__(self, is_secure, *args, **kwargs):
+ super(Response, self).__init__(*args, **kwargs)
+ self._is_secure = is_secure
+
+ def set_http_cookie(self, key, value, secure=None):
+ if secure is None:
+ secure = self._is_secure
+ super(Response, self).set_cookie(key, value, secure=secure, httponly=True)
diff --git a/cmk/gui/login.py b/cmk/gui/login.py
index b62321e..bc186f4 100644
--- a/cmk/gui/login.py
+++ b/cmk/gui/login.py
@@ -176,19 +176,11 @@ def create_auth_session(username):
def set_auth_cookie(username):
- html.response.set_cookie(
- auth_cookie_name(),
- auth_cookie_value(username),
- secure=html.request.is_ssl_request,
- httponly=True)
+ html.response.set_http_cookie(auth_cookie_name(), auth_cookie_value(username))
def set_session_cookie(username, session_id):
- html.response.set_cookie(
- session_cookie_name(),
- session_cookie_value(username, session_id),
- secure=html.request.is_ssl_request,
- httponly=True)
+ html.response.set_http_cookie(session_cookie_name(), session_cookie_value(username,
session_id))
def session_cookie_name():
@@ -538,7 +530,7 @@ def page_logout():
if not html.request.has_cookie('logout'):
html.response.headers[
'WWW-Authenticate'] = 'Basic realm="OMD Monitoring Site
%s"' % config.omd_site()
- html.response.set_cookie('logout', '1',
secure=html.request.is_ssl_request)
+ html.response.set_http_cookie('logout', '1')
raise FinalizeRequest(httplib.UNAUTHORIZED)
else:
html.response.delete_cookie('logout')
diff --git a/tests/unit/cmk/gui/conftest.py b/tests/unit/cmk/gui/conftest.py
index 80c6342..076e5ff 100644
--- a/tests/unit/cmk/gui/conftest.py
+++ b/tests/unit/cmk/gui/conftest.py
@@ -1,11 +1,10 @@
import pytest
import _pytest
from werkzeug.test import create_environ
-from werkzeug.wrappers import Response
import cmk.gui.config as config
import cmk.gui.htmllib as htmllib
-from cmk.gui.http import Request
+from cmk.gui.http import Request, Response
from cmk.gui.globals import html
monkeypatch = _pytest.monkeypatch.MonkeyPatch()
@@ -16,4 +15,4 @@ monkeypatch.setattr(config, "omd_site", lambda:
"NO_SITE")
def register_builtin_html():
"""This fixture registers a global htmllib.html() instance just like
the regular GUI"""
environ = dict(create_environ(), REQUEST_URI='')
- html.set_current(htmllib.html(Request(environ), Response()))
+ html.set_current(htmllib.html(Request(environ), Response(is_secure=False)))
diff --git a/tests/unit/cmk/gui/old/classes.py b/tests/unit/cmk/gui/old/classes.py
index 2d2fa72..84da527 100644
--- a/tests/unit/cmk/gui/old/classes.py
+++ b/tests/unit/cmk/gui/old/classes.py
@@ -3,16 +3,15 @@
# > py.test -s -k test_html_generator.py
from werkzeug.test import create_environ
-from werkzeug.wrappers import Response
-from cmk.gui.http import Request
+from cmk.gui.http import Request, Response
import cmk.gui.htmllib as htmllib
# A Class which can be used to simulate HTML generation in varios tests in tests/web/
class HTMLTester(htmllib.html):
def __init__(self):
environ = dict(create_environ(), REQUEST_URI='')
- super(HTMLTester, self).__init__(Request(environ), Response())
+ super(HTMLTester, self).__init__(Request(environ), Response(is_secure=False))
def context_button_test(obj, title, url, icon=None, hot=False, id_=None, bestof=None,
hover_title=None, id_in_best=False):
diff --git a/tests/unit/cmk/gui/test_http.py b/tests/unit/cmk/gui/test_http.py
index 180bca0..c35858d 100644
--- a/tests/unit/cmk/gui/test_http.py
+++ b/tests/unit/cmk/gui/test_http.py
@@ -40,29 +40,20 @@ def test_request_processing(register_builtin_html):
# html.parse_field_storage(["field1", "field2"],
handle_uploads_as_file_obj = False)
-def test_response_set_cookie(register_builtin_html):
- html.response.set_cookie("auth_SITE", "user:123456:abcdefg",
httponly=True)
+def test_response_set_http_cookie(register_builtin_html):
+ html.response.set_http_cookie("auth_SITE",
"user:123456:abcdefg")
assert html.response.headers.getlist("Set-Cookie")[-1] == \
"auth_SITE=user:123456:abcdefg; HttpOnly; Path=/"
-def test_response_set_cookie_secure(register_builtin_html, monkeypatch):
- html.response.set_cookie("auth_SITE", "user:123456:abcdefg",
secure=True, httponly=True)
+def test_response_set_http_cookie_secure(register_builtin_html, monkeypatch):
+ html.response.set_http_cookie("auth_SITE", "user:123456:abcdefg",
secure=True)
assert html.response.headers.getlist("Set-Cookie")[-1] == \
"auth_SITE=user:123456:abcdefg; Secure; HttpOnly; Path=/"
-def test_response_set_cookie_expires(register_builtin_html, monkeypatch):
- monkeypatch.setattr(time, "time", lambda: 0)
-
- html.response.set_cookie("auth_SITE", "user:123456:abcdefg",
expires=60, httponly=True)
-
- assert html.response.headers.getlist("Set-Cookie")[-1] == \
- "auth_SITE=user:123456:abcdefg; Expires=Thu, 01-Jan-1970 00:01:00 GMT;
HttpOnly; Path=/"
-
-
def test_response_del_cookie(register_builtin_html, monkeypatch):
monkeypatch.setattr(time, "time", lambda: 0)
diff --git a/web/app/index.wsgi b/web/app/index.wsgi
index 7f1fb36..f09e1da 100644
--- a/web/app/index.wsgi
+++ b/web/app/index.wsgi
@@ -28,8 +28,6 @@ import httplib
import os
import traceback
-import werkzeug.wrappers
-
import livestatus
import cmk.utils.paths
@@ -69,9 +67,7 @@ class Application(object):
self._environ = environ
self._start_response = start_response
self._request = cmk.gui.http.Request(environ)
- self._response = werkzeug.wrappers.Response()
- # NOTE: Currently we rely on a *relavtive* Location header in redirects!
- self._response.autocorrect_location_header = False
+ self._response = cmk.gui.http.Response(is_secure=self._request.is_ssl_request)
# Create an object that contains all data about the request and
# helper functions for creating valid HTML. Parse URI and