-
Notifications
You must be signed in to change notification settings - Fork 0
Implement license key generation with batch metadata and error handling #48
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,3 +16,6 @@ dist/ | |
| *.log | ||
| coverage.xml | ||
| .coverage.* | ||
| .fastembed_cache | ||
| .database | ||
| .amdb | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from cryptography.hazmat.primitives.asymmetric.ed25519 import ( | ||
| Ed25519PrivateKey, | ||
| Ed25519PublicKey, | ||
| ) | ||
| from cryptography.hazmat.primitives.serialization import ( | ||
| load_pem_private_key, | ||
| load_pem_public_key, | ||
| ) | ||
| from cryptography.utils import Buffer | ||
|
|
||
|
|
||
| __all__ = ["load_private_ed25519_key", "load_public_ed25519_key"] | ||
|
|
||
|
|
||
| def load_private_ed25519_key( | ||
| pem: Buffer, password: bytes | None = None | ||
| ) -> Ed25519PrivateKey: | ||
| key = load_pem_private_key(pem, password=password) | ||
| if not isinstance(key, Ed25519PrivateKey): | ||
| raise TypeError("Provided key is not an Ed25519 private key") | ||
| return key | ||
|
|
||
|
|
||
| def load_public_ed25519_key(pem: bytes) -> Ed25519PublicKey: | ||
| key = load_pem_public_key(pem) | ||
| if not isinstance(key, Ed25519PublicKey): | ||
| raise TypeError("Provided key is not an Ed25519 public key") | ||
| return key | ||
|
Comment on lines
+17
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial Type signature inconsistency between private and public key loaders.
♻️ Proposed fix for type consistency-def load_public_ed25519_key(pem: bytes) -> Ed25519PublicKey:
+def load_public_ed25519_key(pem: Buffer) -> Ed25519PublicKey:
key = load_pem_public_key(pem)
if not isinstance(key, Ed25519PublicKey):
raise TypeError("Provided key is not an Ed25519 public key")
return key🤖 Prompt for AI Agents |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import ClassVar | ||
| from uuid import UUID | ||
|
|
||
| import uuid6 | ||
| from pydantic import BaseModel, ConfigDict, computed_field, field_validator | ||
|
|
||
| from app.internal import base32_crockford | ||
|
|
||
|
|
||
| __all__ = ["ActivationCode"] | ||
|
|
||
|
|
||
| class ActivationCode(BaseModel): | ||
| model_config = ConfigDict(validate_assignment=True) | ||
|
|
||
| LENGTH: ClassVar[int] = 30 | ||
| GROUP: ClassVar[int] = 5 | ||
|
|
||
| code: str | ||
|
|
||
| @field_validator("code") | ||
| @classmethod | ||
| def validate_code(cls, v: str) -> str: | ||
| # TODO: need the proper error handling here. | ||
| normalized = base32_crockford.normalize(v) | ||
|
|
||
| if len(normalized) != cls.LENGTH: | ||
| raise ValueError("Activation code must contain 30 symbols") | ||
|
|
||
| base32_crockford.decode(normalized, checksum=True) | ||
|
|
||
| return "-".join( | ||
| normalized[i : i + cls.GROUP] | ||
| for i in range(0, cls.LENGTH, cls.GROUP) | ||
| ) | ||
|
|
||
| @computed_field | ||
| @property | ||
| def uuid(self) -> UUID: | ||
| flat = base32_crockford.normalize(self.code) | ||
| n = base32_crockford.decode(flat, checksum=True) | ||
| return UUID(int=n) | ||
|
Comment on lines
+39
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial Redundant normalization and decode in The ♻️ Option: Store decoded value during validationOne approach is to store the decoded integer as a private attribute during validation, then use it in the + _decoded_int: int | None = None # Private field for cached decode result
+
`@field_validator`("code")
`@classmethod`
def validate_code(cls, v: str) -> str:
normalized = base32_crockford.normalize(v)
if len(normalized) != cls.LENGTH:
raise ValueError("Activation code must contain 30 symbols")
- base32_crockford.decode(normalized, checksum=True)
+ # Decode is performed for checksum validation; result used by uuid property
+ base32_crockford.decode(normalized, checksum=True)
return "-".join(
normalized[i : i + cls.GROUP]
for i in range(0, cls.LENGTH, cls.GROUP)
)Note: Pydantic's 🤖 Prompt for AI Agents |
||
|
|
||
| @classmethod | ||
| def generate(cls, uuid: UUID | None = None) -> ActivationCode: | ||
| if uuid is None: | ||
| uuid = uuid6.uuid7() | ||
| if not isinstance(uuid, UUID): | ||
| raise TypeError(f"uuid cannot be of type {uuid.__class__.__name__}") | ||
| if uuid.version != 7: # noqa: PLR2004 | ||
| raise TypeError( | ||
| f"uuid must be a UUID version 7, got {uuid.version}" | ||
| ) | ||
|
Comment on lines
+46
to
+55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial Redundant type check after None handling. The ♻️ Suggested refactor to clarify intent `@classmethod`
def generate(cls, uuid: UUID | None = None) -> ActivationCode:
+ if uuid is not None and not isinstance(uuid, UUID):
+ raise TypeError(f"uuid cannot be of type {uuid.__class__.__name__}")
if uuid is None:
uuid = uuid6.uuid7()
- if not isinstance(uuid, UUID):
- raise TypeError(f"uuid cannot be of type {uuid.__class__.__name__}")
if uuid.version != 7: # noqa: PLR2004
raise TypeError(
f"uuid must be a UUID version 7, got {uuid.version}"
)🤖 Prompt for AI Agents
Comment on lines
+52
to
+55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial Consider The UUID type is correct; only the version value is wrong. ♻️ Proposed change if uuid.version != 7: # noqa: PLR2004
- raise TypeError(
+ raise ValueError(
f"uuid must be a UUID version 7, got {uuid.version}"
)Note: This would require updating the corresponding test to expect 🤖 Prompt for AI Agents |
||
|
|
||
| encoded = base32_crockford.encode(uuid.int, checksum=True) | ||
| encoded = encoded.rjust(cls.LENGTH, "0") | ||
|
|
||
| return cls(code=encoded) | ||
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.
🧩 Analysis chain
🏁 Script executed:
Repository: eggzec/permit
Length of output: 5047
Remove
.fastembed_cachefrom .gitignore — it's not a project dependency.The verification shows FastEmbed is neither listed in
pyproject.tomldependencies nor referenced in the codebase. This pattern appears to be out of scope for the license key generation PR. Additionally,.amdbis unidentified and lacks any references in the codebase. The.databasepattern is unclear given the project uses PostgreSQL (server-based) rather than file-based databases.🤖 Prompt for AI Agents