merge authentication
This commit is contained in:
commit
37cdd41ec9
4 changed files with 78 additions and 6 deletions
|
|
@ -7,9 +7,8 @@ from fastapi.security import OAuth2PasswordBearer
|
||||||
from fastapi import Depends, HTTPException, status
|
from fastapi import Depends, HTTPException, status
|
||||||
from datetime import datetime, timedelta, timezone
|
from datetime import datetime, timedelta, timezone
|
||||||
|
|
||||||
from langflow.services.utils import get_settings_manager
|
from langflow.services.utils import get_settings_manager, get_session
|
||||||
|
|
||||||
from langflow.services.utils import get_session
|
|
||||||
from langflow.database.models.user import (
|
from langflow.database.models.user import (
|
||||||
User,
|
User,
|
||||||
get_user_by_id,
|
get_user_by_id,
|
||||||
|
|
@ -125,6 +124,23 @@ def create_user_longterm_token(db: Session = Depends(get_session)) -> dict:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def create_user_api_key(user_id: UUID) -> dict:
|
||||||
|
access_token = create_token(
|
||||||
|
data={"sub": str(user_id), "role": "api_key"},
|
||||||
|
expires_delta=timedelta(days=365 * 2),
|
||||||
|
)
|
||||||
|
|
||||||
|
return {"api_key": access_token}
|
||||||
|
|
||||||
|
|
||||||
|
def get_user_id_from_token(token: str) -> UUID:
|
||||||
|
try:
|
||||||
|
user_id = jwt.get_unverified_claims(token)["sub"]
|
||||||
|
return UUID(user_id)
|
||||||
|
except (KeyError, JWTError, ValueError):
|
||||||
|
return UUID(int=0)
|
||||||
|
|
||||||
|
|
||||||
def create_user_tokens(
|
def create_user_tokens(
|
||||||
user_id: UUID, db: Session = Depends(get_session), update_last_login: bool = False
|
user_id: UUID, db: Session = Depends(get_session), update_last_login: bool = False
|
||||||
) -> dict:
|
) -> dict:
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ from fastapi.responses import FileResponse
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
|
|
||||||
from langflow.api import router
|
from langflow.api import router
|
||||||
from langflow.routers import login, users, health
|
from langflow.routers import api_key, login, users, health
|
||||||
|
|
||||||
from langflow.interface.utils import setup_llm_caching
|
from langflow.interface.utils import setup_llm_caching
|
||||||
from langflow.services.database.utils import initialize_database
|
from langflow.services.database.utils import initialize_database
|
||||||
|
|
@ -32,6 +32,7 @@ def create_app():
|
||||||
)
|
)
|
||||||
|
|
||||||
app.include_router(login.router)
|
app.include_router(login.router)
|
||||||
|
app.include_router(api_key.router)
|
||||||
app.include_router(users.router)
|
app.include_router(users.router)
|
||||||
app.include_router(health.router)
|
app.include_router(health.router)
|
||||||
|
|
||||||
|
|
|
||||||
49
src/backend/langflow/routers/api_key.py
Normal file
49
src/backend/langflow/routers/api_key.py
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
|
||||||
|
|
||||||
|
from fastapi import APIRouter
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
router = APIRouter(tags=["APIKey"])
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/api_key/{user_id}")
|
||||||
|
def get_api_key(user_id: str):
|
||||||
|
return {
|
||||||
|
"total_count": 3,
|
||||||
|
"user_id": user_id,
|
||||||
|
"api_keys": [
|
||||||
|
{
|
||||||
|
"id": "4425707e-cce4-4d1b-a54e-bd2632064657",
|
||||||
|
"name": "my api_key name - 01",
|
||||||
|
"created_at": "2023-08-15T19:28:40.019613",
|
||||||
|
"last_used_at": "2023-08-16T18:38:20.875210",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "6fb7282b-9f2e-4efe-9bda-0c3d8f899473",
|
||||||
|
"name": "my api_key name - 02",
|
||||||
|
"created_at": "2023-08-15T19:41:30.077942",
|
||||||
|
"last_used_at": "2023-08-15T19:45:32.067899",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "c55f3b32-4920-42b6-a5cd-698b4251806e",
|
||||||
|
"name": "my api_key name - 03",
|
||||||
|
"created_at": "2023-08-15T20:29:40.577808",
|
||||||
|
"last_used_at": "2023-08-15T20:29:40.577816",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/api_key/{user_id}")
|
||||||
|
def create_api_key(user_id: str):
|
||||||
|
return {
|
||||||
|
"user_id": user_id,
|
||||||
|
"name": "my api-key 01",
|
||||||
|
"api_key": "lf-eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1YTBmODM1ZS0yMTQxLTQ2YWItYmQ4NS0yMWEzMjQ1MTE2ZDAiLCJleHAiOjE2OTIyMTUwMTN9.c_s0ZPRtjSI9yUrhi8ACIwyXf0feRLYfaeIZEbRVKQg",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/api_key/{api_key_id}")
|
||||||
|
def delete_api_key(api_key_id: str):
|
||||||
|
return {"detail": "API Key deleted"}
|
||||||
|
|
@ -42,11 +42,17 @@ class Settings(BaseSettings):
|
||||||
ACCESS_TOKEN_EXPIRE_MINUTES: int = 60
|
ACCESS_TOKEN_EXPIRE_MINUTES: int = 60
|
||||||
REFRESH_TOKEN_EXPIRE_MINUTES: int = 70
|
REFRESH_TOKEN_EXPIRE_MINUTES: int = 70
|
||||||
|
|
||||||
|
# API Key to execute /process endpoint
|
||||||
|
API_KEY_SECRET_KEY: Optional[
|
||||||
|
str
|
||||||
|
] = "b82818e0ad4ff76615c5721ee21004b07d84cd9b87ba4d9cb42374da134b841a"
|
||||||
|
API_KEY_ALGORITHM: str = "HS256"
|
||||||
|
|
||||||
# If AUTO_LOGIN = True
|
# If AUTO_LOGIN = True
|
||||||
# > The application does not request login and logs in automatically as a super user.
|
# > The application does not request login and logs in automatically as a super user.
|
||||||
AUTO_LOGIN: bool = False
|
AUTO_LOGIN: bool = True
|
||||||
FIRST_SUPERUSER: str = "superuser"
|
FIRST_SUPERUSER: str = "langflow"
|
||||||
FIRST_SUPERUSER_PASSWORD: str = "12345"
|
FIRST_SUPERUSER_PASSWORD: str = "langflow"
|
||||||
|
|
||||||
@validator("DATABASE_URL", pre=True)
|
@validator("DATABASE_URL", pre=True)
|
||||||
def set_database_url(cls, value):
|
def set_database_url(cls, value):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue