From b213ca4eaca06566c4693941ea694a2b4874eaba Mon Sep 17 00:00:00 2001 From: "Alexie (Boyong) Madolid" Date: Sat, 19 Oct 2024 01:08:43 +0800 Subject: [PATCH] temporary --- jac-cloud/jac_cloud/jaseci/models/webhook.py | 4 +-- jac-cloud/jac_cloud/jaseci/routers/webhook.py | 26 +++++++++++++++---- .../jac_cloud/jaseci/security/__init__.py | 20 +++++++++++++- 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/jac-cloud/jac_cloud/jaseci/models/webhook.py b/jac-cloud/jac_cloud/jaseci/models/webhook.py index 88ffa9b61a..22fd41aea7 100644 --- a/jac-cloud/jac_cloud/jaseci/models/webhook.py +++ b/jac-cloud/jac_cloud/jaseci/models/webhook.py @@ -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 @@ -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] diff --git a/jac-cloud/jac_cloud/jaseci/routers/webhook.py b/jac-cloud/jac_cloud/jaseci/routers/webhook.py index 5bb0f11772..cd3e40e9c3 100644 --- a/jac-cloud/jac_cloud/jaseci/routers/webhook.py +++ b/jac-cloud/jac_cloud/jaseci/routers/webhook.py @@ -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 @@ -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, @@ -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}) diff --git a/jac-cloud/jac_cloud/jaseci/security/__init__.py b/jac-cloud/jac_cloud/jaseci/security/__init__.py index 64bfa642dd..d316f9767c 100644 --- a/jac-cloud/jac_cloud/jaseci/security/__init__.py +++ b/jac-cloud/jac_cloud/jaseci/security/__init__.py @@ -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 @@ -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)]