basic -> htpasswd

This commit is contained in:
Devaev Maxim 2019-04-08 06:19:02 +03:00
parent 9243d2a00c
commit d8a5e38371
6 changed files with 17 additions and 17 deletions

View File

@ -138,9 +138,9 @@ def _get_config_scheme() -> Dict:
},
"auth": {
"type": Option("basic", type=valid_auth_type, unpack_as="auth_type"),
"basic": {
"htpasswd": Option("/etc/kvmd/htpasswd", type=valid_abs_path_exists, unpack_as="htpasswd_path"),
"type": Option("htpasswd", type=valid_auth_type, unpack_as="auth_type"),
"htpasswd": {
"file": Option("/etc/kvmd/htpasswd", type=valid_abs_path_exists, unpack_as="path"),
},
},

View File

@ -44,9 +44,9 @@ from .. import init
# =====
def _get_htpasswd_path(config: Section) -> str:
if config.kvmd.auth.type != "basic":
print("Warning: KVMD does not use basic auth", file=sys.stderr)
return config.kvmd.auth.basic.htpasswd
if config.kvmd.auth.type != "htpasswd":
print("Warning: KVMD does not use htpasswd auth", file=sys.stderr)
return config.kvmd.auth.htpasswd.file
@contextlib.contextmanager
@ -101,7 +101,7 @@ def main(argv: Optional[List[str]]=None) -> None:
(parent_parser, argv, config) = init(add_help=False, argv=argv)
parser = argparse.ArgumentParser(
prog="kvmd-htpasswd",
description="Manage KVMD users (basic auth only)",
description="Manage KVMD users (htpasswd auth only)",
parents=[parent_parser],
)
parser.set_defaults(cmd=(lambda *_: parser.print_help()))

View File

@ -32,9 +32,9 @@ from ...logging import get_logger
# =====
class AuthManager:
def __init__(self, auth_type: str, basic: Dict) -> None:
def __init__(self, auth_type: str, htpasswd: Dict) -> None:
self.__login = {
"basic": lambda: _BasicLogin(**basic),
"htpasswd": lambda: _HtpasswdLogin(**htpasswd),
}[auth_type]().login
self.__tokens: Dict[str, str] = {} # {token: user}
@ -60,11 +60,11 @@ class AuthManager:
return self.__tokens.get(token)
class _BasicLogin:
def __init__(self, htpasswd_path: str) -> None:
get_logger().info("Using basic auth %r", htpasswd_path)
self.__htpasswd_path = htpasswd_path
class _HtpasswdLogin:
def __init__(self, path: str) -> None:
get_logger().info("Using htpasswd auth file %r", path)
self.__path = path
def login(self, user: str, passwd: str) -> bool:
htpasswd = passlib.apache.HtpasswdFile(self.__htpasswd_path)
htpasswd = passlib.apache.HtpasswdFile(self.__path)
return htpasswd.check_password(user, passwd)

View File

@ -40,4 +40,4 @@ def valid_auth_token(arg: Any) -> str:
def valid_auth_type(arg: Any) -> str:
return check_string_in_list(arg, "auth type", ["basic"])
return check_string_in_list(arg, "auth type", ["htpasswd"])

View File

@ -59,7 +59,7 @@ def _run_main(htpasswd: passlib.apache.HtpasswdFile, cmd: List[str]) -> None:
"kvmd-htpasswd",
*cmd,
"--set-options",
"kvmd/auth/basic/htpasswd=" + htpasswd.path,
"kvmd/auth/htpasswd/file=" + htpasswd.path,
])

View File

@ -108,7 +108,7 @@ def test_fail__valid_auth_token(arg: Any) -> None:
print(valid_auth_token(arg))
@pytest.mark.parametrize("arg", ["BASIC ", "basic"])
@pytest.mark.parametrize("arg", ["HTPASSWD ", "htpasswd"])
def test_ok__valid_auth_type(arg: Any) -> None:
assert valid_auth_type(arg) == arg.strip().lower()