Store key_prefix for readable key display instead of masked hash
The last-4 of the SHA-256 hash was meaningless for identification. Now storing the first 12 chars of the plaintext key as key_prefix, displayed as 'sk-aBcDeFgH••••••••' — consistent with what the user sees at creation time and how GitHub/OpenAI handle it.
This commit is contained in:
@@ -40,6 +40,7 @@ def create_api_key(
|
||||
db_key = APIKey(
|
||||
name=name,
|
||||
key=_hash_api_key(raw_key),
|
||||
key_prefix=raw_key[:12],
|
||||
expires_at=expires_at,
|
||||
daily_tokens=daily_tokens,
|
||||
monthly_tokens=monthly_tokens,
|
||||
|
||||
@@ -10,6 +10,7 @@ class APIKey(Base):
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
name = Column(String)
|
||||
key = Column(String, unique=True, index=True)
|
||||
key_prefix = Column(String)
|
||||
is_active = Column(Boolean, default=True)
|
||||
created_at = Column(DateTime(timezone=True), default=_now)
|
||||
expires_at = Column(DateTime(timezone=True), nullable=True)
|
||||
|
||||
@@ -14,6 +14,7 @@ class APIKey(BaseModel):
|
||||
id: int
|
||||
name: str
|
||||
key: str
|
||||
key_prefix: Optional[str] = None
|
||||
is_active: bool
|
||||
created_at: datetime
|
||||
expires_at: Optional[datetime] = None
|
||||
|
||||
Reference in New Issue
Block a user