Mini Shell
import functools
import hashlib
import time
from pathlib import Path
from flask import current_app as app, redirect, url_for
from flask import g, session, abort, request
from bakmgr.api.bakauth import get_reg_details
MAX_SESSION_LENGTH = 2592000 # 30 days
HASH_PATH = Path("/etc/bakmgr/.dash_passwd")
def get_reg() -> dict:
if 'bak_reg' not in g:
g.bak_reg = get_reg_details()
return g.bak_reg
def iter_dir(dir_path: Path):
try:
for path in dir_path.iterdir():
yield path
except FileNotFoundError:
return
def login(func):
@functools.wraps(func)
def _login(*args, **kwargs):
if login_is_valid():
return func(*args, **kwargs)
return redirect(url_for('login_page', next=request.url))
return _login
def login_is_valid():
if not app.config['SECRET_KEY']:
abort(500)
try:
login_time = int(session['login_time'])
except (KeyError, ValueError):
return False
else:
return time.time() - login_time < MAX_SESSION_LENGTH
def hash_pw(password: str) -> str:
pw_bytes = bytes(password, encoding='utf-8', errors='backslashreplace')
# the purpose of the version number '1' here is to determine which hashing
# algo was used, in case we want to change it later.
return f"1:{hashlib.sha512(pw_bytes).hexdigest()}"
def read_pw_hash():
try:
line = HASH_PATH.read_text('ascii')
ver, hashed = line.split(':', maxsplit=1)
except (ValueError, OSError):
return None
if ver == '1' and len(hashed) == 128:
return line
return None
Zerion Mini Shell 1.0