Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Basic Authentication #75

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions config/shared_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"__title_Bjorn__": "Settings",
"manual_mode": false,
"websrv": true,
"web_auth_enabled": false,
"web_auth_user": "bjorn",
"web_auth_pass": "bjorn",
"web_increment ": false,
"debug_mode": true,
"scan_vuln_running": false,
Expand Down Expand Up @@ -74,7 +77,8 @@
],
"mac_scan_blacklist": [
"00:11:32:c4:71:9b",
"00:11:32:c4:71:9a"
"00:11:32:c4:71:9a",
"2c:cf:67:a7:0a:81"
],
"ip_scan_blacklist": [
"192.168.1.1",
Expand Down Expand Up @@ -103,5 +107,8 @@
"timewait_telnet": 0,
"timewait_ftp": 0,
"timewait_sql": 0,
"timewait_rdp": 0
}
"timewait_rdp": 0,
"web_auth": true,
"web_auth_username": "bjorn",
"web_auth_password": "test2"
}
3 changes: 3 additions & 0 deletions shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ def get_default_config(self):
"__title_Bjorn__": "Settings",
"manual_mode": False,
"websrv": True,
"web_auth_enabled": False,
"web_auth_user": "bjorn",
"web_auth_pass": "bjorn",
"web_increment ": False,
"debug_mode": True,
"scan_vuln_running": False,
Expand Down
26 changes: 26 additions & 0 deletions webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import os
import gzip
import io
import base64
from logger import Logger
from init_shared import shared_data
from utils import WebUtils
Expand All @@ -19,6 +20,12 @@
# Set the path to the favicon
favicon_path = os.path.join(shared_data.webdir, '/images/favicon.ico')

# Set Basic Auth variables
web_auth = (shared_data.config["web_auth_enabled"] and shared_data.config["web_auth_enabled"] == True)
web_auth_user = shared_data.config["web_auth_user"]
web_auth_pass = shared_data.config["web_auth_pass"]


class CustomHandler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
self.shared_data = shared_data
Expand Down Expand Up @@ -56,9 +63,24 @@ def serve_file_gzipped(self, file_path, content_type):
content = file.read()
self.send_gzipped_response(content, content_type)

def basic_auth_check(self):
return self.headers.get('Authorization') == None or self.headers.get('Authorization') != 'Basic ' + base64.b64encode(f"{web_auth_user}:{web_auth_pass}".encode()).decode()

def do_AUTHHEAD(self):
logger.info("Sending Auth Header")
self.send_response(401)
self.send_header('WWW-Authenticate','Basic realm="BJORN"')
self.send_header('Content-Type','text/html')
self.end_headers()

def do_GET(self):
# Check Web Auth
if web_auth and self.basic_auth_check():
self.do_AUTHHEAD()
return
# Handle GET requests. Serve the HTML interface and the EPD image.
if self.path == '/index.html' or self.path == '/':
#logger.info("Serving Index Page")
self.serve_file_gzipped(os.path.join(self.shared_data.webdir, 'index.html'), 'text/html')
elif self.path == '/config.html':
self.serve_file_gzipped(os.path.join(self.shared_data.webdir, 'config.html'), 'text/html')
Expand Down Expand Up @@ -116,6 +138,10 @@ def do_GET(self):
super().do_GET()

def do_POST(self):
# Check Web Auth
if web_auth and self.basic_auth_check():
self.do_AUTHHEAD()
return
# Handle POST requests for saving configuration, connecting to Wi-Fi, clearing files, rebooting, and shutting down.
if self.path == '/save_config':
self.web_utils.save_configuration(self)
Expand Down