Milin
commited on
Commit
·
35d4f12
1
Parent(s):
4a586b4
feat(auth): Implement multi-user login support
Browse files- 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.
- env.example +5 -6
- lightrag/api/auth.py +8 -3
- lightrag/api/lightrag_server.py +4 -9
- lightrag/api/utils_api.py +1 -3
env.example
CHANGED
@@ -151,9 +151,8 @@ QDRANT_URL=http://localhost:16333
|
|
151 |
### Redis
|
152 |
REDIS_URI=redis://localhost:6379
|
153 |
|
154 |
-
### For
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
WHITELIST_PATHS=/login,/health # white list
|
|
|
151 |
### Redis
|
152 |
REDIS_URI=redis://localhost:6379
|
153 |
|
154 |
+
### For JWT Auth
|
155 |
+
# AUTH_ACCOUNTS='admin:admin123,user1:pass456' # username:password,username:password
|
156 |
+
# TOKEN_SECRET=Your-Key-For-LightRAG-API-Server # JWT key
|
157 |
+
# TOKEN_EXPIRE_HOURS=4 # expire duration
|
158 |
+
# WHITELIST_PATHS= # white list
|
|
lightrag/api/auth.py
CHANGED
@@ -20,9 +20,14 @@ class AuthHandler:
|
|
20 |
self.secret = os.getenv("TOKEN_SECRET", "4f85ds4f56dsf46")
|
21 |
self.algorithm = "HS256"
|
22 |
self.expire_hours = int(os.getenv("TOKEN_EXPIRE_HOURS", 4))
|
23 |
-
self.guest_expire_hours = int(
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
def create_token(
|
28 |
self,
|
|
|
20 |
self.secret = os.getenv("TOKEN_SECRET", "4f85ds4f56dsf46")
|
21 |
self.algorithm = "HS256"
|
22 |
self.expire_hours = int(os.getenv("TOKEN_EXPIRE_HOURS", 4))
|
23 |
+
self.guest_expire_hours = int(os.getenv("GUEST_TOKEN_EXPIRE_HOURS", 2))
|
24 |
+
|
25 |
+
self.accounts = {}
|
26 |
+
auth_accounts = os.getenv("AUTH_ACCOUNTS")
|
27 |
+
if auth_accounts:
|
28 |
+
for account in auth_accounts.split(','):
|
29 |
+
username, password = account.split(':', 1)
|
30 |
+
self.accounts[username] = password
|
31 |
|
32 |
def create_token(
|
33 |
self,
|
lightrag/api/lightrag_server.py
CHANGED
@@ -350,10 +350,8 @@ def create_app(args):
|
|
350 |
@app.get("/auth-status", dependencies=[Depends(optional_api_key)])
|
351 |
async def get_auth_status():
|
352 |
"""Get authentication status and guest token if auth is not configured"""
|
353 |
-
username = os.getenv("AUTH_USERNAME")
|
354 |
-
password = os.getenv("AUTH_PASSWORD")
|
355 |
|
356 |
-
if not
|
357 |
# Authentication not configured, return guest token
|
358 |
guest_token = auth_handler.create_token(
|
359 |
username="guest", role="guest", metadata={"auth_mode": "disabled"}
|
@@ -377,10 +375,7 @@ def create_app(args):
|
|
377 |
|
378 |
@app.post("/login", dependencies=[Depends(optional_api_key)])
|
379 |
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
|
380 |
-
|
381 |
-
password = os.getenv("AUTH_PASSWORD")
|
382 |
-
|
383 |
-
if not (username and password):
|
384 |
# Authentication not configured, return guest token
|
385 |
guest_token = auth_handler.create_token(
|
386 |
username="guest", role="guest", metadata={"auth_mode": "disabled"}
|
@@ -393,8 +388,8 @@ def create_app(args):
|
|
393 |
"core_version": core_version,
|
394 |
"api_version": __api_version__,
|
395 |
}
|
396 |
-
|
397 |
-
if
|
398 |
raise HTTPException(
|
399 |
status_code=status.HTTP_401_UNAUTHORIZED, detail="Incorrect credentials"
|
400 |
)
|
|
|
350 |
@app.get("/auth-status", dependencies=[Depends(optional_api_key)])
|
351 |
async def get_auth_status():
|
352 |
"""Get authentication status and guest token if auth is not configured"""
|
|
|
|
|
353 |
|
354 |
+
if not auth_handler.accounts:
|
355 |
# Authentication not configured, return guest token
|
356 |
guest_token = auth_handler.create_token(
|
357 |
username="guest", role="guest", metadata={"auth_mode": "disabled"}
|
|
|
375 |
|
376 |
@app.post("/login", dependencies=[Depends(optional_api_key)])
|
377 |
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
|
378 |
+
if not auth_handler.accounts:
|
|
|
|
|
|
|
379 |
# Authentication not configured, return guest token
|
380 |
guest_token = auth_handler.create_token(
|
381 |
username="guest", role="guest", metadata={"auth_mode": "disabled"}
|
|
|
388 |
"core_version": core_version,
|
389 |
"api_version": __api_version__,
|
390 |
}
|
391 |
+
username = form_data.username
|
392 |
+
if auth_handler.accounts.get(username) != form_data.password:
|
393 |
raise HTTPException(
|
394 |
status_code=status.HTTP_401_UNAUTHORIZED, detail="Incorrect credentials"
|
395 |
)
|
lightrag/api/utils_api.py
CHANGED
@@ -43,9 +43,7 @@ def get_auth_dependency():
|
|
43 |
token: str = Depends(OAuth2PasswordBearer(tokenUrl="login", auto_error=False)),
|
44 |
):
|
45 |
# Check if authentication is configured
|
46 |
-
auth_configured = bool(
|
47 |
-
os.getenv("AUTH_USERNAME") and os.getenv("AUTH_PASSWORD")
|
48 |
-
)
|
49 |
|
50 |
# If authentication is not configured, skip all validation
|
51 |
if not auth_configured:
|
|
|
43 |
token: str = Depends(OAuth2PasswordBearer(tokenUrl="login", auto_error=False)),
|
44 |
):
|
45 |
# Check if authentication is configured
|
46 |
+
auth_configured = bool(auth_handler.accounts)
|
|
|
|
|
47 |
|
48 |
# If authentication is not configured, skip all validation
|
49 |
if not auth_configured:
|