Skip to content

Commit

Permalink
Merge pull request #78 from open-dynaMIX/fix_htpasswd
Browse files Browse the repository at this point in the history
fix(auth): fix handling missing htpasswd
  • Loading branch information
open-dynaMIX committed Jun 27, 2020
2 parents f696ae6 + 5692370 commit cda1300
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
10 changes: 7 additions & 3 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def test_disablers(mpv_instance, v4_works, v6_works):
@pytest.mark.parametrize(
"mpv_instance,auth,status_code",
[
(get_script_opts({"htpasswd_path": "/tmp/.htpasswd"}), None, 401,),
(get_script_opts({"htpasswd_path": "/tmp/.htpasswd"}), None, 401),
(
get_script_opts({"htpasswd_path": "/tmp/.htpasswd"}),
HTTPBasicAuth("user", "wrong"),
Expand All @@ -310,12 +310,16 @@ def test_disablers(mpv_instance, v4_works, v6_works):
HTTPBasicAuth("user", "secret"),
200,
),
(get_script_opts({"htpasswd_path": "/app/.does-not-exist"}), None, 200,),
(get_script_opts({"htpasswd_path": "/app/.does-not-exist"}), None, None),
],
indirect=["mpv_instance"],
)
def test_auth(htpasswd, mpv_instance, auth, status_code):
resp = requests.get(get_uri("api/status"), auth=auth)
try:
resp = requests.get(get_uri("api/status"), auth=auth, timeout=0.5)
except requests.exceptions.ReadTimeout:
assert status_code is None
return
assert resp.status_code == status_code


Expand Down
48 changes: 27 additions & 21 deletions webui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -544,11 +544,15 @@ local function listen(server, passwd)
end

local function get_passwd(path)
if path ~= nil then
if path ~= '' then
if file_exists(path) then
return lines_from(path)
else
mp.msg.error("Error: Provided .htpasswd could not be found!")
msg = "Provided .htpasswd could not be found!"
mp.msg.error("Error: " .. msg)
message = function() mp.osd_message(MSG_PREFIX .. msg .. "\nwebui is disabled.", 5) end
mp.register_event("file-loaded", message)
return 1
end
end
end
Expand Down Expand Up @@ -582,24 +586,26 @@ end
local passwd = get_passwd(options.htpasswd_path)
local servers = init_servers()

if next(servers) == nil then
error_msg = "Error: Couldn't spawn server on port " .. options.port
message = function() mp.msg.error(error_msg); mp.osd_message(MSG_PREFIX .. error_msg, 5) end
else
for _, server in pairs(servers) do
server:settimeout(0)
mp.add_periodic_timer(0.2, function() listen(server, passwd) end)
end
startup_msg = ("v" .. VERSION .. "\nServing on "
.. concatkeys(servers, ':' .. options.port .. ' and ')
.. ":" .. options.port
)
message = function() mp.osd_message(MSG_PREFIX .. startup_msg, 5) end
mp.msg.info(startup_msg)
if passwd ~= nil then
mp.msg.info('Found .htpasswd file. Basic authentication is enabled.')
if passwd ~= 1 then
if next(servers) == nil then
error_msg = "Error: Couldn't spawn server on port " .. options.port
message = function() mp.msg.error(error_msg); mp.osd_message(MSG_PREFIX .. error_msg, 5) end
else
for _, server in pairs(servers) do
server:settimeout(0)
mp.add_periodic_timer(0.2, function() listen(server, passwd) end)
end
startup_msg = ("v" .. VERSION .. "\nServing on "
.. concatkeys(servers, ':' .. options.port .. ' and ')
.. ":" .. options.port
)
message = function() mp.osd_message(MSG_PREFIX .. startup_msg, 5) end
mp.msg.info(startup_msg)
if passwd ~= nil then
mp.msg.info('Found .htpasswd file. Basic authentication is enabled.')
end
end
end

mp.register_event("file-loaded", message)
mp.register_event("file-loaded", function() mp.unregister_event(message) end)
mp.register_event("file-loaded", message)
mp.register_event("file-loaded", function() mp.unregister_event(message) end)
end

0 comments on commit cda1300

Please sign in to comment.