feat(auth): Implement multi-user login support

- Add an `accounts` dictionary in `AuthHandler` to store multiple user account information.
- Modify login logic to support multiple user account verification.
- Update environment variable example, add description for `AUTH_ACCOUNTS` variable.
- Adjust authentication status check logic, use `auth_handler.accounts` to determine if authentication is configured.
This commit is contained in:
Milin
2025-03-24 14:34:31 +08:00
parent e2737e4412
commit 4dfdb9032c
4 changed files with 18 additions and 21 deletions

View File

@@ -350,10 +350,8 @@ def create_app(args):
@app.get("/auth-status", dependencies=[Depends(optional_api_key)])
async def get_auth_status():
"""Get authentication status and guest token if auth is not configured"""
username = os.getenv("AUTH_USERNAME")
password = os.getenv("AUTH_PASSWORD")
if not (username and password):
if not auth_handler.accounts:
# Authentication not configured, return guest token
guest_token = auth_handler.create_token(
username="guest", role="guest", metadata={"auth_mode": "disabled"}
@@ -377,10 +375,7 @@ def create_app(args):
@app.post("/login", dependencies=[Depends(optional_api_key)])
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
username = os.getenv("AUTH_USERNAME")
password = os.getenv("AUTH_PASSWORD")
if not (username and password):
if not auth_handler.accounts:
# Authentication not configured, return guest token
guest_token = auth_handler.create_token(
username="guest", role="guest", metadata={"auth_mode": "disabled"}
@@ -393,8 +388,8 @@ def create_app(args):
"core_version": core_version,
"api_version": __api_version__,
}
if form_data.username != username or form_data.password != password:
username = form_data.username
if auth_handler.accounts.get(username) != form_data.password:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED, detail="Incorrect credentials"
)