From d8dfc01cecb98aed70d8724998745f7939d36a6f Mon Sep 17 00:00:00 2001 From: Waldek Date: Mon, 24 Feb 2020 20:26:39 +0100 Subject: [PATCH] Update monitor.py Add simple web auth --- monitor.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/monitor.py b/monitor.py index ea061d0..b925879 100644 --- a/monitor.py +++ b/monitor.py @@ -708,7 +708,25 @@ class web_server(Resource): isLeaf = True def render_GET(self, request): logging.info('static website requested: %s', request) - return (index_html).encode('utf-8') + if WEB_AUTH: + user = WEB_USER.encode('utf-8') + password = WEB_PASS.encode('utf-8') + auth = request.getHeader('Authorization') + if auth and auth.split(' ')[0] == 'Basic': + decodeddata = base64.b64decode(auth.split(' ')[1]) + if decodeddata.split(b':') == [user, password]: + logging.info('Authorization OK') + return (index_html).encode('utf-8') + request.setResponseCode(401) + request.setHeader('WWW-Authenticate', 'Basic realm="realmname"') + logging.info('Someone wanted to get access without authorization') + return "


\ +
\ +

Authorization Required

".encode('utf-8') + else: + return (index_html).encode('utf-8') if __name__ == '__main__': logging.basicConfig(