Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
temporary
Browse files Browse the repository at this point in the history
  • Loading branch information
amadolid committed Mar 22, 2024
1 parent 671d2b7 commit 8214e49
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
9 changes: 3 additions & 6 deletions jaclang_jaseci/securities/authenticator.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"""Walker API Authenticator."""

from os import getenv
from random import choice
from string import ascii_letters, digits
from typing import Any, Optional, cast

from bson import ObjectId
Expand All @@ -18,12 +16,10 @@
from ..memory import CodeMemory, TokenMemory
from ..models import User
from ..plugins import Root
from ..utils import logger, utc_timestamp
from ..utils import logger, random_string, utc_timestamp


TOKEN_SECRET = getenv(
"TOKEN_SECRET", "".join(choice(ascii_letters + digits) for _ in range(50))
)
TOKEN_SECRET = getenv("TOKEN_SECRET", random_string(50))
TOKEN_ALGORITHM = getenv("TOKEN_ALGORITHM", "HS256")


Expand Down Expand Up @@ -71,6 +67,7 @@ async def verify_code(code: str) -> Optional[str]:
async def create_token(user: dict[str, Any]) -> str:
"""Generate token for current user."""
user["expiration"] = utc_timestamp(hours=int(getenv("TOKEN_TIMEOUT") or "12"))
user["state"] = random_string(8)
token = encrypt(user)
if await TokenMemory.hset(key=token, data=True):
return token
Expand Down
8 changes: 8 additions & 0 deletions jaclang_jaseci/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@
import logging
import sys
from datetime import datetime, timedelta, timezone
from random import choice
from string import ascii_letters, digits
from typing import Optional, Union, cast, get_args, get_origin

from .mail import Emailer, SendGridEmailer


def random_string(length: int) -> str:
"""Generate String with length."""
return "".join(choice(ascii_letters + digits) for _ in range(length))


def utc_datetime(**addons: int) -> datetime:
"""Get current datetime with option to add additional timedelta."""
return datetime.now(tz=timezone.utc) + timedelta(**addons)
Expand All @@ -33,6 +40,7 @@ def make_optional(cls: type) -> type:
__all__ = [
"Emailer",
"SendGridEmailer",
"random_string",
"utc_datetime",
"utc_timestamp",
"logger",
Expand Down

0 comments on commit 8214e49

Please sign in to comment.