Skip to content

Commit

Permalink
temporary
Browse files Browse the repository at this point in the history
  • Loading branch information
amadolid committed Oct 18, 2024
1 parent 5309d45 commit b213ca4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
4 changes: 2 additions & 2 deletions jac-cloud/jac_cloud/jaseci/models/webhook.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Jaseci Models."""

from dataclasses import dataclass
from dataclasses import dataclass, field
from datetime import datetime
from typing import Any, Generator, Mapping, cast

Expand All @@ -13,7 +13,7 @@
class Webhook:
"""User Base Model."""

id: ObjectId
id: ObjectId = field(default_factory=ObjectId)
root_id: ObjectId
walkers: list[str]
nodes: list[str]
Expand Down
26 changes: 21 additions & 5 deletions jac-cloud/jac_cloud/jaseci/routers/webhook.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
"""User APIs."""
"""Webhook APIs."""

from typing import cast

from bson import ObjectId

from fastapi import APIRouter, Request, status
from fastapi.exceptions import HTTPException
Expand All @@ -7,7 +11,7 @@
from passlib.hash import pbkdf2_sha512

from ..dtos import GenerateKey
from ..models import Webhook
from ..models import User, Webhook
from ..security import (
authenticator,
create_code,
Expand All @@ -22,9 +26,21 @@
@router.post(
"/generate-key", status_code=status.HTTP_201_CREATED, dependencies=authenticator
)
def generate_key(req: GenerateKey) -> ORJSONResponse:
def generate_key(req: Request, gen_key: GenerateKey) -> ORJSONResponse:
"""Generate key API."""
import pdb
root_id: ObjectId = req._user.root_id # type: ignore[attr-defined]

user: BaseUser = User.Collection.find_by_email(req.email) # type: ignore
if not user or not pbkdf2_sha512.verify(req.password, user.password):
raise HTTPException(status_code=400, detail="Invalid Email/Password!")

if RESTRICT_UNVERIFIED_USER and not user.is_activated:
User.send_verification_code(create_code(user.id), req.email)
raise HTTPException(
status_code=400,
detail="Account not yet verified! Resending verification code...",
)

pdb.set_trace()
user_json = user.serialize()
token = create_token(user_json)
return ORJSONResponse(content={"token": 1, "user": 1})
20 changes: 19 additions & 1 deletion jac-cloud/jac_cloud/jaseci/security/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from jwt import decode, encode

from ..datasources.redis import CodeRedis, TokenRedis
from ..models.user import User as BaseUser
from ..models import User as BaseUser, Webhook
from ..utils import logger, random_string, utc_timestamp
from ...core.architype import NodeAnchor

Expand Down Expand Up @@ -104,4 +104,22 @@ def authenticate(request: Request) -> None:
raise HTTPException(status_code=401)


def create_key(root_id: ObjectId, expiration: dict[str, int]) -> str:
"""Generate token for current user."""
exp = utc_timestamp(**expiration)
Webhook(
root_id=root_id
)
key = {
"expiration"
}
key[] =

user["state"] = random_string(8)
token = encrypt(user)
if TokenRedis.hset(f"{user['id']}:{token}", True):
return token
raise HTTPException(500, "Token Creation Failed!")


authenticator = [Depends(HTTPBearer()), Depends(authenticate)]

0 comments on commit b213ca4

Please sign in to comment.