Skip to content

Commit cd06eb1

Browse files
committed
fix: chatbots use user_id as FK
1 parent 913f83a commit cd06eb1

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

server/api/auth.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from fastapi import APIRouter, Depends, Header
99
from pydantic import BaseModel
1010
from commons import config as c
11-
from commons.utils import get_user_from_jwt
11+
from commons.utils import get_user_from_jwt, get_user_id_from_jwt
1212

1313

1414
auth_router = APIRouter(prefix="", tags=["authentication"])
@@ -31,7 +31,7 @@ class SignUpModal(BaseModel):
3131
def login(auth: AuthModel, db: Session = Depends(database.db_session)):
3232
user: User = db.query(User).filter(User.username == auth.username).first() # type: ignore
3333
if user is not None and sha256_crypt.verify(auth.password, user.password): # type: ignore
34-
token = jwt.encode(payload={"username": auth.username}, key=c.JWT_SECRET)
34+
token = jwt.encode(payload={"username": auth.username, "userid": user.id}, key=c.JWT_SECRET)
3535
response = {"msg": "success", "token": token}
3636
else:
3737
response = {"msg": "failed"}
@@ -41,12 +41,14 @@ def login(auth: AuthModel, db: Session = Depends(database.db_session)):
4141
@auth_router.post("/get_user_info", status_code=200)
4242
def decode_token(token: Annotated[str, Header()]):
4343
username = None
44+
userid = None
4445
try:
4546
username = get_user_from_jwt(token)
47+
userid = get_user_id_from_jwt(token)
4648
except Exception as e:
4749
logger.exception(e)
4850
response = {"msg": "failed"}
49-
response = {"msg": "success", "username": username}
51+
response = {"msg": "success", "username": username, "user_id": userid}
5052
return response
5153

5254

@@ -70,7 +72,7 @@ def sign_up(auth: SignUpModal, db: Session = Depends(database.db_session)):
7072
user: User = User(username=auth.username, email=auth.email, password=sha256_crypt.hash(auth.password)) # type: ignore
7173
db.add(user)
7274
db.commit()
73-
token = jwt.encode(payload={"username": auth.username}, key=c.JWT_SECRET)
75+
token = jwt.encode(payload={"username": auth.username, "userid": user.id}, key=c.JWT_SECRET)
7476
response = {"msg": "success", "token": token}
7577
else:
7678
response = {"msg": "failed"}

server/api/chatbot.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from sqlalchemy.orm import Session
55
from fastapi import APIRouter, Depends, Header
66
from pydantic import BaseModel
7-
from commons.utils import get_user_from_jwt, verify_user
7+
from commons.utils import get_user_from_jwt, verify_user, get_user_id_from_jwt
88
from commons import config as c
99

1010
logger = c.get_logger(__name__)
@@ -21,8 +21,9 @@ class ChatBotModel(BaseModel):
2121
def create_chatbot(inputs: ChatBotModel, token: Annotated[str, Header()], db: Session = Depends(database.db_session)):
2222
username = get_user_from_jwt(token)
2323
verify_user(username)
24+
user_id = get_user_id_from_jwt(token)
2425
try:
25-
chatbot = ChatBot(name=inputs.name, created_by=username, dag=inputs.dag)
26+
chatbot = ChatBot(name=inputs.name, created_by=user_id, dag=inputs.dag)
2627
db.add(chatbot)
2728
db.commit()
2829
response = {"msg": "success", "chatbot": chatbot.to_dict()}

server/commons/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@ def get_user_from_jwt(token):
5858
return payload.get("username")
5959

6060

61+
def get_user_id_from_jwt(token):
62+
try:
63+
payload = jwt.decode(
64+
token,
65+
key=c.JWT_SECRET,
66+
algorithms=["HS256"],
67+
)
68+
except Exception as e:
69+
logger.exception("Could not decide JWT token")
70+
raise HTTPException(status_code=401, detail="Could not decode JWT token")
71+
return payload.get("userid")
72+
73+
6174
def verify_user(username):
6275
db = db_session()
6376
user = db.query(User).filter(User.username == username).first()

0 commit comments

Comments
 (0)