Commit f40bbc98 authored by arun.uday's avatar arun.uday

AssetManager-V1.0- Not reviewed

Updated decryption of function form handlers to move utils
parent bfbd3e4d
from pydantic import BaseSettings, Field
PROJECT_NAME = "IotManager"
class _Services(BaseSettings):
HOST: str = Field(default="127.0.0.1", env="service_host")
PORT: int = Field(default=8000, env="service_port")
class _Databases(BaseSettings):
MONGO_URI: str
Services = _Services()
Databases = _Databases()
__all__ = [
"PROJECT_NAME",
"Services",
"Databases",
]
......@@ -3,17 +3,19 @@ from fastapi.responses import JSONResponse
from fastapi import status
from scripts.errors import ErrorMessages
from scripts.schemas.default_responses import DefaultFailureResponse, DefaultResponse
from scripts.utils.security.password_decryption_util import DecryptPassword
class LoginHandlers:
def __init__(self):
self.obj_login_handler = NormalLogin()
self.pass_decrypt = DecryptPassword()
self.login_type = ""
def normal_login(self, user_data, request):
self.login_type = "normal"
# decrypting the password from the UI
decrypted_password = self.obj_login_handler.password_decrypt(user_data.password)
decrypted_password = self.pass_decrypt.password_decrypt(user_data.password)
# validating the received inputs empty or not
response = self.obj_login_handler.user_data_validation(
......
from __future__ import annotations
import base64
from datetime import datetime
from Cryptodome.Cipher import AES
from passlib.context import CryptContext
from validate_email import validate_email
from scripts.config import Services, Secrets
from scripts.database.mongo.mongo_login import MongoUser
from scripts.errors import ErrorMessages
from scripts.logging.logger import logger
......@@ -16,40 +13,12 @@ from scripts.utils.security.apply_encrytion_util import create_token
class NormalLogin:
def __init__(self):
self.mongo_user = MongoUser()
self.pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
self.db_user_data = None
self.db_user_data = None
self.dt = datetime.now()
self.time_dt = datetime.now()
@staticmethod
def un_pad(s):
# un_padding the encrypted password
return s[:-ord(s[len(s) - 1:])]
def password_decrypt(self, password):
try:
# encoding the Key
key = Secrets.KEY_ENCRYPTION.encode(Services.ENCODING_TYPE)
# decoding the received password
enc = base64.b64decode(password)
# mode for the decryption
mode = AES.MODE_CBC
# getting the initialization vector
iv = enc[:AES.block_size]
# decoding with AES
cipher = AES.new(key, mode, iv)
# decoding the password
data = cipher.decrypt(enc[AES.block_size:])
if len(data) == 0:
raise ValueError("Decrypted data is empty")
# removing the padding to get the password
data = self.un_pad(data)
return data.decode(Services.ENCODING_TYPE)
except Exception as e:
logger.exception(e)
@staticmethod
def user_data_validation(username, password) -> dict | None:
try:
......
......@@ -892,3 +892,70 @@ TypeError: 'LoginRequest' object is not subscriptable
2023-03-23 11:48:14 - INFO - [MainThread:<module>(): 37] - App Starting at 0.0.0.0:8671
2023-03-23 11:49:01 - INFO - [MainThread:<module>(): 37] - App Starting at 0.0.0.0:8671
2023-03-23 11:49:48 - INFO - [MainThread:<module>(): 37] - App Starting at 0.0.0.0:8671
2023-03-23 11:58:26 - INFO - [MainThread:<module>(): 37] - App Starting at 0.0.0.0:8671
2023-03-23 11:58:29 - ERROR - [MainThread:password_decrypt(): 37] - '_Services' object has no attribute 'ENCODING_TYPE'
Traceback (most recent call last):
File "E:\Git\meta-services\scripts\utils\security\password_decryption_util.py", line 20, in password_decrypt
key = Secrets.KEY_ENCRYPTION.encode(Services.ENCODING_TYPE)
AttributeError: '_Services' object has no attribute 'ENCODING_TYPE'
2023-03-23 11:58:29 - ERROR - [MainThread:db_password_matching(): 67] - secret must be unicode or bytes, not None
Traceback (most recent call last):
File "E:\Git\meta-services\scripts\core\handlers\normal_login.py", line 61, in db_password_matching
if not self.pwd_context.verify(password, self.db_user_data["password"]):
File "E:\Git\meta-services\venv\lib\site-packages\passlib\context.py", line 2347, in verify
return record.verify(secret, hash, **kwds)
File "E:\Git\meta-services\venv\lib\site-packages\passlib\utils\handlers.py", line 787, in verify
validate_secret(secret)
File "E:\Git\meta-services\venv\lib\site-packages\passlib\utils\handlers.py", line 122, in validate_secret
raise exc.ExpectedStringError(secret, "secret")
TypeError: secret must be unicode or bytes, not None
2023-03-23 11:58:29 - ERROR - [MainThread:login_default(): 38] - cannot unpack non-iterable NoneType object
Traceback (most recent call last):
File "E:\Git\meta-services\scripts\services\v1\iot_manager_services.py", line 31, in login_default
return login_mapper[login_type](user_data, request)
File "E:\Git\meta-services\scripts\core\handlers\login_handler.py", line 30, in normal_login
response, data = self.obj_login_handler.db_password_matching(self.login_type, user_data,
TypeError: cannot unpack non-iterable NoneType object
2023-03-23 12:01:23 - INFO - [MainThread:<module>(): 37] - App Starting at 0.0.0.0:8671
2023-03-23 12:01:24 - ERROR - [MainThread:<module>(): 41] - cannot import name 'Services' from 'scripts.constants' (E:\Git\meta-services\scripts\constants\__init__.py)
Traceback (most recent call last):
File "E:\Git\meta-services\app.py", line 39, in <module>
uvicorn.run("main:app", host=arguments["bind"], port=int(arguments["port"]))
File "E:\Git\meta-services\venv\lib\site-packages\uvicorn\main.py", line 568, in run
server.run()
File "E:\Git\meta-services\venv\lib\site-packages\uvicorn\server.py", line 59, in run
return asyncio.run(self.serve(sockets=sockets))
File "C:\Users\arun.uday\AppData\Local\Programs\Python\Python39\lib\asyncio\runners.py", line 44, in run
return loop.run_until_complete(main)
File "C:\Users\arun.uday\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 647, in run_until_complete
return future.result()
File "E:\Git\meta-services\venv\lib\site-packages\uvicorn\server.py", line 66, in serve
config.load()
File "E:\Git\meta-services\venv\lib\site-packages\uvicorn\config.py", line 471, in load
self.loaded_app = import_from_string(self.app)
File "E:\Git\meta-services\venv\lib\site-packages\uvicorn\importer.py", line 24, in import_from_string
raise exc from None
File "E:\Git\meta-services\venv\lib\site-packages\uvicorn\importer.py", line 21, in import_from_string
module = importlib.import_module(module_str)
File "C:\Users\arun.uday\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "E:\Git\meta-services\main.py", line 17, in <module>
from scripts.services import router
File "E:\Git\meta-services\scripts\services\__init__.py", line 2, in <module>
from scripts.services import v1
File "E:\Git\meta-services\scripts\services\v1\__init__.py", line 3, in <module>
from scripts.services.v1 import iot_manager_services
File "E:\Git\meta-services\scripts\services\v1\iot_manager_services.py", line 4, in <module>
from scripts.core.handlers.login_handler import LoginHandlers
File "E:\Git\meta-services\scripts\core\handlers\login_handler.py", line 6, in <module>
from scripts.utils.security.password_decryption_util import DecryptPassword
File "E:\Git\meta-services\scripts\utils\security\password_decryption_util.py", line 6, in <module>
from scripts.constants import Services
ImportError: cannot import name 'Services' from 'scripts.constants' (E:\Git\meta-services\scripts\constants\__init__.py)
2023-03-23 12:01:37 - INFO - [MainThread:<module>(): 37] - App Starting at 0.0.0.0:8671
import base64
from Cryptodome.Cipher import AES
from scripts.config import Secrets, Services
from scripts.logging.logger import logger
class DecryptPassword:
@staticmethod
def un_pad(s):
# un_padding the encrypted password
return s[:-ord(s[len(s) - 1:])]
def password_decrypt(self, password):
try:
# encoding the Key
key = Secrets.KEY_ENCRYPTION.encode(Services.ENCODING_TYPE)
# decoding the received password
enc = base64.b64decode(password)
# mode for the decryption
mode = AES.MODE_CBC
# getting the initialization vector
iv = enc[:AES.block_size]
# decoding with AES
cipher = AES.new(key, mode, iv)
# decoding the password
data = cipher.decrypt(enc[AES.block_size:])
if len(data) == 0:
raise ValueError("Decrypted data is empty")
# removing the padding to get the password
data = self.un_pad(data)
return data.decode(Services.ENCODING_TYPE)
except Exception as e:
logger.exception(e)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment