Skip to content

Commit

Permalink
Allow cameras with no authentication - danger
Browse files Browse the repository at this point in the history
  • Loading branch information
TojikCZ committed Oct 26, 2023
1 parent 7913938 commit 5bb151d
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 14 deletions.
14 changes: 9 additions & 5 deletions prusa/link/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ def __init__(self, settings_file):
self.service_local = Model(
self.get_section('service::local',
(('enable', int, 1), ('username', str, ''),
('digest', str, ''), ('api_key', str, ''))))
('digest', str, ''), ('api_key', str, ''),
('auth', bool, True))))

Settings.instance = self

Expand Down Expand Up @@ -329,10 +330,13 @@ def is_wizard_needed(self, camera_mode=False):
"""
Is there a reason for the wizard to be shown?
"""
interested_in = [
self.service_local["username"],
self.service_local["digest"],
]
interested_in = []

if self.service_local["auth"]:
interested_in.extend([
self.service_local["username"],
self.service_local["digest"],
])
if not camera_mode:
interested_in.append(self.printer["type"])
return not all(interested_in)
Expand Down
5 changes: 2 additions & 3 deletions prusa/link/templates/camera_wizard.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ <h2 class="align-center">
<div class="col">
<a id="restore_btn" href={{ "/wizard/restore" | prefixed }} class="btn btn-outline-light full-width" onmouseover="changeIconImage('img/reset-icon-black.svg')" onmouseout="changeIconImage('img/reset-icon-white.svg')">Restore settings <img id="restore_icon" height="25" src="img/reset-icon-white.svg"></a>
</div>
<div class="col-sm-auto"><a href={{ "/wizard/credentials" | prefixed }} class="btn btn-outline-light full-width">Setup credentials | NEXT <img src="img/arrow-right.svg" height="16" /></a>
</div>
</div>
<div class="col-sm-auto"><a href={{ "/wizard/no-auth" | prefixed }} class="btn btn-outline-light full-width">No authentication [DANGER] <img src="img/arrow-right.svg" height="16" /></a></div>
<div class="col-sm-auto"><a href={{ "/wizard/credentials" | prefixed }} class="btn btn-outline-light full-width">Setup credentials | NEXT <img src="img/arrow-right.svg" height="16" /></a></div>
</div>
</div>
</div>
Expand Down
9 changes: 9 additions & 0 deletions prusa/link/web/camera_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ def wizard_credentials(req):
wizard=app.wizard)


@app.route('/wizard/no-auth')
@check_ready
def wizard_no_auth(req):
"""Credentials configuration."""
app.wizard.auth = False
app.wizard.write_settings(app.settings)
redirect_with_proxy(req, '/')


@app.route('/wizard/credentials', method=state.METHOD_POST)
@check_ready
def wizard_credentials_post(req):
Expand Down
16 changes: 16 additions & 0 deletions prusa/link/web/lib/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
from functools import wraps

import poorwsgi
from poorwsgi import state
from poorwsgi.digest import check_credentials, hexdigest
from poorwsgi.response import HTTPException, Response
Expand All @@ -28,6 +29,18 @@
SAME_DIGEST = "Nothing to change. All credentials are same as old ones"


def optional_auth(func):
"""Used for optional authentication of index page"""

@wraps(func)
def handler(req, *args, **kwargs):
if app.settings is None or app.settings.service_local["auth"]:
return poorwsgi.digest.check_digest(REALM)(func)(
req, *args, **kwargs)
return func(req, *args, **kwargs)
return handler


def check_digest(req):
"""Check HTTP Digest.
Expand Down Expand Up @@ -56,6 +69,9 @@ def check_api_digest(func):

@wraps(func)
def handler(req, *args, **kwargs):
if not app.settings.service_local["auth"]:
return func(req, *args, **kwargs)

prusa_link = app.daemon.prusa_link
if not prusa_link or not prusa_link.printer:
raise HTTPException(state.HTTP_SERVICE_UNAVAILABLE)
Expand Down
9 changes: 6 additions & 3 deletions prusa/link/web/lib/wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def __init__(self, _app):
self.serial = None

# auth
self.auth = True
self.username = _app.settings.service_local.username
self.digest = None
self.restored_digest = False
Expand Down Expand Up @@ -178,9 +179,11 @@ def check_connect(self):

def write_settings(self, settings):
"""Write settings configuration."""
# auth
settings.service_local.digest = self.digest
settings.service_local.username = self.username
settings.service_local.auth = self.auth
if self.auth:
# auth
settings.service_local.digest = self.digest
settings.service_local.username = self.username

# network
settings.network.hostname = self.net_hostname
Expand Down
5 changes: 2 additions & 3 deletions prusa/link/web/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from gcode_metadata import get_metadata
from pkg_resources import working_set # type: ignore
from poorwsgi import state
from poorwsgi.digest import check_digest
from poorwsgi.response import (
EmptyResponse,
FileResponse,
Expand All @@ -34,7 +33,7 @@
update_prusalink,
)
from ..printer_adapter.job import Job, JobState
from .lib.auth import REALM, check_api_digest, check_config
from .lib.auth import check_api_digest, check_config, optional_auth
from .lib.core import app
from .lib.files import fill_printfile_data, gcode_analysis, get_os_path
from .lib.view import package_to_api
Expand Down Expand Up @@ -73,7 +72,7 @@ def instance(req):

@app.route('/', method=state.METHOD_GET)
@check_config
@check_digest(REALM)
@optional_auth
def index(req):
"""Return status page"""
# pylint: disable=unused-argument
Expand Down

0 comments on commit 5bb151d

Please sign in to comment.