11import uuid
22import secrets
3- from datetime import datetime , timezone
43from sqlmodel import Session , select
54from app .core .security import (
6- verify_password ,
75 get_password_hash ,
86 encrypt_api_key ,
97 decrypt_api_key ,
108)
11- import base64
12- from cryptography .hazmat .primitives import hashes
13- from cryptography .hazmat .primitives .kdf .pbkdf2 import PBKDF2HMAC
149from app .core import settings
1510from app .core .util import now
16-
1711from app .models .api_key import APIKey , APIKeyPublic
1812
1913
@@ -25,7 +19,7 @@ def generate_api_key() -> tuple[str, str]:
2519
2620
2721def create_api_key (
28- session : Session , organization_id : uuid . UUID , user_id : uuid .UUID
22+ session : Session , organization_id : int , user_id : uuid .UUID , project_id : int
2923) -> APIKeyPublic :
3024 """
3125 Generates a new API key for an organization and associates it with a user.
@@ -42,6 +36,7 @@ def create_api_key(
4236 key = encrypted_key , # Store the encrypted raw key
4337 organization_id = organization_id ,
4438 user_id = user_id ,
39+ project_id = project_id ,
4540 )
4641
4742 session .add (api_key )
@@ -75,32 +70,6 @@ def get_api_key(session: Session, api_key_id: int) -> APIKeyPublic | None:
7570 return None
7671
7772
78- def get_api_keys_by_organization (
79- session : Session , organization_id : uuid .UUID
80- ) -> list [APIKeyPublic ]:
81- """
82- Retrieves all active API keys associated with an organization.
83- Returns the API keys in their original format.
84- """
85- api_keys = session .exec (
86- select (APIKey ).where (
87- APIKey .organization_id == organization_id , APIKey .is_deleted == False
88- )
89- ).all ()
90-
91- raw_keys = []
92- for api_key in api_keys :
93- api_key_dict = api_key .model_dump ()
94-
95- decrypted_key = decrypt_api_key (api_key .key )
96-
97- api_key_dict ["key" ] = decrypted_key
98-
99- raw_keys .append (APIKeyPublic .model_validate (api_key_dict ))
100-
101- return raw_keys
102-
103-
10473def delete_api_key (session : Session , api_key_id : int ) -> None :
10574 """
10675 Soft deletes (revokes) an API key by marking it as deleted.
@@ -137,23 +106,18 @@ def get_api_key_by_value(session: Session, api_key_value: str) -> APIKeyPublic |
137106 return None
138107
139108
140- def get_api_key_by_user_org (
141- db : Session , organization_id : int , user_id : int
142- ) -> APIKey | None :
143- """Get an API key by user and organization ID."""
109+ def get_api_key_by_project ( session : Session , project_id : int ) -> APIKeyPublic | None :
110+ """
111+ Retrieves the single API key associated with a project.
112+ """
144113 statement = select (APIKey ).where (
145- APIKey .organization_id == organization_id ,
146- APIKey .user_id == user_id ,
147- APIKey .is_deleted == False ,
114+ APIKey .project_id == project_id , APIKey .is_deleted == False
148115 )
149- api_key = db .exec (statement ).first ()
116+ api_key = session .exec (statement ).first ()
150117
151118 if api_key :
152119 api_key_dict = api_key .model_dump ()
120+ api_key_dict ["key" ] = decrypt_api_key (api_key .key )
121+ return APIKeyPublic .model_validate (api_key_dict )
153122
154- decrypted_key = decrypt_api_key (api_key .key )
155-
156- api_key_dict ["key" ] = decrypted_key
157-
158- return APIKey .model_validate (api_key_dict )
159123 return None
0 commit comments