feat(auth): add token authentication with SHA-256 hashed storage
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Share — local file sharing server (HTTPS)."""
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import ssl
|
||||
import sys
|
||||
import threading
|
||||
from pathlib import Path
|
||||
|
||||
@@ -13,6 +15,11 @@ from server import Handler, ShareServer
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) > 1 and sys.argv[1] == "hash":
|
||||
token = sys.argv[2] if len(sys.argv) > 2 else input("Token: ")
|
||||
print(hashlib.sha256(token.encode()).hexdigest())
|
||||
return
|
||||
|
||||
conf = load_config()
|
||||
|
||||
port = int(conf["port"])
|
||||
@@ -29,12 +36,18 @@ def main():
|
||||
html = (BASE_DIR / "static" / "index.html").read_bytes()
|
||||
html = html.replace(b"<!--CFG-->", cfg_tag)
|
||||
|
||||
cert_dir = BASE_DIR / ".certs"
|
||||
cert, key = ensure_cert(cert_dir, extra_ips=san_list)
|
||||
if conf["cert"] and conf["key"]:
|
||||
cert, key = Path(conf["cert"]), Path(conf["key"])
|
||||
else:
|
||||
cert_dir = BASE_DIR / ".certs"
|
||||
cert, key = ensure_cert(cert_dir, extra_ips=san_list)
|
||||
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
|
||||
ctx.load_cert_chain(cert, key)
|
||||
|
||||
server = ShareServer(("0.0.0.0", port), Handler, upload_dir=upload_dir, html=html, ssl_ctx=ctx)
|
||||
token = conf["token"]
|
||||
# If token looks like a SHA-256 hash, pass it as pre-hashed
|
||||
is_hash = len(token) == 64 and all(c in "0123456789abcdef" for c in token)
|
||||
server = ShareServer(("0.0.0.0", port), Handler, upload_dir=upload_dir, html=html, ssl_ctx=ctx, token=token, token_is_hash=is_hash)
|
||||
|
||||
print("\n Share running")
|
||||
print(f" Local: https://localhost:{port}")
|
||||
|
||||
Reference in New Issue
Block a user