Support multiple accounts
All checks were successful
Build and Publish college-bot-v2 / build_and_publish (push) Successful in 13s

This commit is contained in:
Nexus 2024-06-06 00:37:21 +01:00
parent b8d5df6bbf
commit 41e2daa265
Signed by: nex
GPG key ID: 0FA334385D0B689F

View file

@ -31,11 +31,19 @@ USERNAME = os.getenv("WEB_USERNAME", os.urandom(32).hex())
PASSWORD = os.getenv("WEB_PASSWORD", os.urandom(32).hex())
accounts = {
USERNAME: PASSWORD
}
if os.path.exists("./web-accounts.json"):
with open("./web-accounts.json") as f:
accounts.update(json.load(f))
def check_credentials(credentials: HTTPBasicCredentials = Depends(security)):
err = HTTPException(status_code=401, detail="Unauthorized")
if credentials.username != USERNAME:
if credentials.username not in accounts:
raise err
if not secrets.compare_digest(credentials.password, PASSWORD):
if not secrets.compare_digest(credentials.password, accounts[credentials.username]):
raise err
return credentials