-
Notifications
You must be signed in to change notification settings - Fork 1
support multiple API keys per team #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| from app.models.user import User | ||
| from app.models.team import Team, TeamMembership, TeamRole | ||
| from app.models.api_key import ApiKey | ||
| from app.models.log import Log, LogLevel | ||
|
|
||
| __all__ = ["User", "Team", "TeamMembership", "TeamRole", "Log", "LogLevel"] | ||
| __all__ = ["User", "Team", "TeamMembership", "TeamRole", "ApiKey", "Log", "LogLevel"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| from tortoise import fields | ||
| from tortoise.models import Model | ||
| import secrets | ||
| import hashlib | ||
|
|
||
|
|
||
| class ApiKey(Model): | ||
| id = fields.UUIDField(pk=True) | ||
| team = fields.ForeignKeyField("models.Team", related_name="api_keys", on_delete=fields.CASCADE) | ||
| label = fields.CharField(max_length=255, default="") | ||
| api_key_hash = fields.CharField(max_length=255, unique=True, index=True) | ||
| api_key_prefix = fields.CharField(max_length=20) | ||
| created_at = fields.DatetimeField(auto_now_add=True) | ||
|
|
||
| class Meta: | ||
| table = "api_keys" | ||
|
|
||
| @staticmethod | ||
| def generate_api_key() -> tuple[str, str, str]: | ||
| """Generate a new API key. Returns (full_key, hash, prefix).""" | ||
| random_part = secrets.token_urlsafe(32) | ||
| prefix = f"sl_{secrets.token_urlsafe(4)}" | ||
| full_key = f"{prefix}_{random_part}" | ||
| key_hash = hashlib.sha256(full_key.encode()).hexdigest() | ||
| return full_key, key_hash, prefix | ||
|
|
||
| @staticmethod | ||
| def hash_api_key(key: str) -> str: | ||
| """Hash an API key for comparison.""" | ||
| return hashlib.sha256(key.encode()).hexdigest() | ||
|
|
||
| @classmethod | ||
| async def get_team_by_api_key(cls, api_key: str): | ||
| """Find a team by API key.""" | ||
| key_hash = cls.hash_api_key(api_key) | ||
| row = await cls.filter(api_key_hash=key_hash).select_related("team").first() | ||
| if row is None: | ||
| return None | ||
| return row.team |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,18 @@ | ||
| from app.schemas.auth import Token, TokenPayload, LoginRequest, RefreshRequest | ||
| from app.schemas.user import UserCreate, UserUpdate, UserResponse | ||
| from app.schemas.team import TeamCreate, TeamUpdate, TeamResponse, TeamWithKey, MembershipCreate, MembershipResponse | ||
| from app.schemas.team import ( | ||
| TeamCreate, TeamUpdate, TeamResponse, TeamCreateResponse, | ||
| ApiKeyCreate, ApiKeyResponse, ApiKeyWithSecret, | ||
| MembershipCreate, MembershipResponse, | ||
| ) | ||
| from app.schemas.log import LogCreate, LogBatchCreate, LogResponse, LogSearchParams, UserIdBackfillRequest, UserIdBackfillResponse | ||
|
|
||
| __all__ = [ | ||
| "Token", "TokenPayload", "LoginRequest", "RefreshRequest", | ||
| "UserCreate", "UserUpdate", "UserResponse", | ||
| "TeamCreate", "TeamUpdate", "TeamResponse", "TeamWithKey", "MembershipCreate", "MembershipResponse", | ||
| "TeamCreate", "TeamUpdate", "TeamResponse", "TeamCreateResponse", | ||
| "ApiKeyCreate", "ApiKeyResponse", "ApiKeyWithSecret", | ||
| "MembershipCreate", "MembershipResponse", | ||
| "LogCreate", "LogBatchCreate", "LogResponse", "LogSearchParams", | ||
| "UserIdBackfillRequest", "UserIdBackfillResponse", | ||
| ] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Manual key creation could hit an unhandled
IntegrityErroron concurrent duplicate.Lines 230-232 pre-check for a duplicate hash, but there's a TOCTOU window — a concurrent request with the same key could pass the check and then fail on the
UNIQUEconstraint at insert time, producing a raw 500. Consider catchingIntegrityErroraround thecreatecall.Wrap create in IntegrityError handler
📝 Committable suggestion
🧰 Tools
🪛 Ruff (0.15.1)
[warning] 216-216: Unused function argument:
admin(ARG001)
🤖 Prompt for AI Agents