Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add login with username & password #44

Merged
merged 3 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ robonomics-interface = "^1.2.2"
ecs-logging = "^2.0.0"
aioprometheus = "^22.5.0"
pycups = "^2.0.1"
pydantic-settings = "^2.2.1"
passlib = {extras = ["bcrypt"], version = "^1.7.4"}
pydantic_settings = "^2.2.1"


[tool.poetry.dev-dependencies]
mypy = "^0.971"
Expand Down
6 changes: 6 additions & 0 deletions src/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class EmployeeModel(BaseModel):

class EmployeeWCardModel(EmployeeModel):
rfid_card_id: str | None
username: str | None


class AdditionalDetail:
Expand Down Expand Up @@ -70,6 +71,11 @@ class EmployeeID(BaseModel):
employee_rfid_card_no: str


class EmployeeCreds(BaseModel):
employee_username: str
employee_password: str


class UnitOut(GenericResponse):
unit_internal_id: str | None

Expand Down
12 changes: 12 additions & 0 deletions src/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ def get_employee_by_card_id(employee_data: models.EmployeeID) -> models.Employee
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(e)) from e


def get_employee_by_username(employee_data: models.EmployeeCreds) -> models.EmployeeWCardModel:
PaTara43 marked this conversation as resolved.
Show resolved Hide resolved
try:
employee: Employee = employee_wrapper.get_employee_by_username(
username=employee_data.employee_username, password=employee_data.employee_password
)
return models.EmployeeWCardModel(**asdict(employee))

except EmployeeNotFoundError as e:
messenger.warning(translation("NoEmployee"))
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(e)) from e


def get_schema_by_id(schema_id: str) -> models.ProductionSchema:
"""get the specified production schema"""
try:
Expand Down
6 changes: 5 additions & 1 deletion src/employee/Employee.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
from dataclasses import dataclass



@dataclass
class Employee:
rfid_card_id: str
name: str
position: str
rfid_card_id: str | None = None
username: str | None = None
passport_code: str = ""

def __post_init__(self) -> None:
if not self.username and not self.rfid_card_id:
raise ValueError("needs username or card id")
if not self.passport_code:
self.passport_code = self.get_passport_code()

Expand Down
33 changes: 30 additions & 3 deletions src/employee/employee_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
from src.feecc_workbench.exceptions import EmployeeNotFoundError
from src.feecc_workbench.Types import Document
from .Employee import Employee
from passlib.context import CryptContext

pwd_context = CryptContext(schemes=["bcrypt"])


class EmployeeWrapper:
Expand All @@ -14,15 +17,39 @@ class EmployeeWrapper:
def get_employee_by_card_id(self, card_id: str) -> Employee:
"""find the employee with the provided RFID card id"""
filters = {"rfid_card_id": card_id}
projection = {"_id": 0}
employee_data: list[Document] | None = BaseMongoDbWrapper.find(collection=self.collection, filters=filters, projection=projection)
projection = {"_id": 0, "hashed_password": 0}
employee_data = BaseMongoDbWrapper.find_one(collection=self.collection, filters=filters, projection=projection)

if employee_data is None:
message = f"No employee with card ID {card_id}"
logger.error(message)
raise EmployeeNotFoundError(message)

return Employee(**employee_data[0])
return Employee(**employee_data)

@time_execution
def get_employee_by_username(self, username: str, password: str) -> Employee:
"""find the employee with the provided RFID card id"""
filters = {"username": username}
projection = {"_id": 0}
employee_data = BaseMongoDbWrapper.find_one(collection=self.collection, filters=filters, projection=projection)

if employee_data is None:
message = f"No employee with username {username}"
logger.error(message)
raise EmployeeNotFoundError(message)

if not bool(pwd_context.verify(password, employee_data["hashed_password"])):
message = f"Incorrect password for username {username}"
logger.error(message)
raise EmployeeNotFoundError(message)

return Employee(
rfid_card_id=employee_data["rfid_card_id"],
name=employee_data["name"],
position=employee_data["position"],
username=employee_data["username"],
)


employee_wrapper = EmployeeWrapper()
30 changes: 28 additions & 2 deletions src/routers/employee_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from loguru import logger
from starlette import status

from src.dependencies import get_employee_by_card_id
from src.dependencies import get_employee_by_card_id, get_employee_by_username
from src.database import models as mdl
from src.employee.Employee import Employee
from src.feecc_workbench.exceptions import StateForbiddenError
Expand All @@ -26,13 +26,39 @@ def get_employee_data(
)


@router.post("/login-creds", response_model=mdl.EmployeeOut)
def log_in_creds(employee: mdl.EmployeeWCardModel = Depends(get_employee_by_username)) -> mdl.EmployeeOut:
try:
WORKBENCH.log_in(
Employee(
rfid_card_id=employee.rfid_card_id,
name=employee.name,
position=employee.position,
username=employee.username,
)
)
return mdl.EmployeeOut(
status_code=status.HTTP_200_OK, detail="Employee logged in successfully", employee_data=employee
)

except StateForbiddenError as e:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail=str(e)) from e


@router.post("/log-in", response_model=mdl.EmployeeOut)
def log_in_employee(
employee: mdl.EmployeeWCardModel = Depends(get_employee_by_card_id), # noqa: B008
) -> mdl.EmployeeOut:
"""handle logging in the Employee at a given Workbench"""
try:
WORKBENCH.log_in(Employee(rfid_card_id=employee.rfid_card_id, name=employee.name, position=employee.position))
WORKBENCH.log_in(
Employee(
rfid_card_id=employee.rfid_card_id,
name=employee.name,
position=employee.position,
username=employee.username,
)
)
return mdl.EmployeeOut(
status_code=status.HTTP_200_OK, detail="Employee logged in successfully", employee_data=employee
)
Expand Down
Loading