-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
125 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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,24 @@ | ||
"""Jaseci User DTOs.""" | ||
|
||
from typing import Literal | ||
|
||
from annotated_types import Len | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
from typing_extensions import Annotated | ||
|
||
|
||
class Expiration(BaseModel): | ||
"""Key Expiration.""" | ||
|
||
count: Annotated[int, Field(strict=True, gt=0, default=60)] = 60 | ||
interval: Literal["seconds", "minutes", "hours", "days"] = "days" | ||
|
||
|
||
class GenerateKey(BaseModel): | ||
"""User Creation Request Model.""" | ||
|
||
walkers: Annotated[list[str], Len(min_length=1)] | ||
nodes: list[str] = Field(default_factory=list) | ||
expiration: Expiration = Field(default_factory=Expiration) |
This file contains 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 @@ | ||
"""Jaseci Models.""" | ||
|
||
from .user import NO_PASSWORD, User | ||
from .webhook import Webhook | ||
|
||
__all__ = ["NO_PASSWORD", "User"] | ||
__all__ = ["NO_PASSWORD", "User", "Webhook"] |
This file contains 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,53 @@ | ||
"""Jaseci Models.""" | ||
|
||
from dataclasses import dataclass | ||
from datetime import datetime | ||
from typing import Any, Generator, Mapping, cast | ||
|
||
from bson import ObjectId | ||
|
||
from ..datasources.collection import Collection as BaseCollection | ||
|
||
|
||
@dataclass(kw_only=True) | ||
class Webhook: | ||
"""User Base Model.""" | ||
|
||
id: ObjectId | ||
root_id: ObjectId | ||
walkers: list[str] | ||
nodes: list[str] | ||
expiration: datetime | ||
key: str | ||
|
||
class Collection(BaseCollection["Webhook"]): | ||
""" | ||
User collection interface. | ||
This interface is for User's Management. | ||
You may override this if you wish to implement different structure | ||
""" | ||
|
||
__collection__ = "webhook" | ||
__indexes__ = [{"keys": ["token"], "unique": True}] | ||
|
||
@classmethod | ||
def __document__(cls, doc: Mapping[str, Any]) -> "Webhook": | ||
""" | ||
Return parsed Webhook from document. | ||
This the default User parser after getting a single document. | ||
You may override this to specify how/which class it will be casted/based. | ||
""" | ||
doc = cast(dict, doc) | ||
return Webhook(id=doc.pop("_id"), **doc) | ||
|
||
@classmethod | ||
def find_by_root_id(cls, root_id: ObjectId) -> Generator["Webhook", None, None]: | ||
"""Retrieve webhook via root_id.""" | ||
return cls.find({"root_id": root_id}) | ||
|
||
@classmethod | ||
def find_by_key(cls, key: str) -> "Webhook | None": | ||
"""Retrieve webhook via root_id.""" | ||
return cls.find_one({"key": key}) |
This file contains 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 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,30 @@ | ||
"""User APIs.""" | ||
|
||
from fastapi import APIRouter, Request, status | ||
from fastapi.exceptions import HTTPException | ||
from fastapi.responses import ORJSONResponse | ||
|
||
from passlib.hash import pbkdf2_sha512 | ||
|
||
from ..dtos import GenerateKey | ||
from ..models import Webhook | ||
from ..security import ( | ||
authenticator, | ||
create_code, | ||
create_token, | ||
invalidate_token, | ||
verify_code, | ||
) | ||
|
||
router = APIRouter(prefix="/webhook", tags=["webhook"]) | ||
|
||
|
||
@router.post( | ||
"/generate-key", status_code=status.HTTP_201_CREATED, dependencies=authenticator | ||
) | ||
def generate_key(req: GenerateKey) -> ORJSONResponse: | ||
"""Generate key API.""" | ||
import pdb | ||
|
||
pdb.set_trace() | ||
return ORJSONResponse(content={"token": 1, "user": 1}) |
This file contains 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