# triledinh159/2fa-mqtt architecture

> Architecture diagram of the GitHub repository triledinh159/2fa-mqtt, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/triledinh159/2fa-mqtt
- Repository: https://github.com/triledinh159/2fa-mqtt

## Overview

##### Project Type and Purpose
- The project is focused on enhancing MQTT security by integrating OTP (one-time passwords) and AES encryption. It clearly involves multi-component interactions where authentication, secure data transmission, and encryption are key.
- The project uses both MQTT Client code and a broker setup (EC2-EMQX) along with Lambda functions that appear to handle backend validation processes (e.g., verifying credentials and finding email records in a DB).

##### Analyzing the File Structure
- Client Directory:
- Contains two scripts, pub.py and sub.py. These represent the MQTT client’s publishing and subscribing functionalities.
- EC2-EMQX Directory:
- Contains an index.html and source.js, which likely serve as a monitoring dashboard or a lightweight UI for viewing status or controlling the EMQX (MQTT broker) running on an EC2 instance.
- Lambda Directory:
- Contains findEmailinDB.py and verify_func.py, which suggest that serverless functions are implemented for processes like looking up email credentials and verifying authentication (possibly OTP verification or similar processes).

- Main Components:
- MQTT Clients (represented by the pub.py and sub.py scripts).
- MQTT Broker (represented by the EC2-EMQX component on an AWS EC2 instance hosting EMQX). Use this to illustrate the core message broker/security gateway.
- Authentication Services (represented by the Lambda functions). These serverless components handle OTP generation/verification, AES-based payload encryption, and possibly DB lookups for securing the authentication flow.

- Relationships and Interactions:
- MQTT Clients connect to and communicate with the EMQX broker.
- The EMQX broker interacts with the Lambda functions to perform additional checks: e.g., upon receiving a message, it might trigger a Lambda function to verify user credentials via OTP and AES encryption.
- Any client publishing messages may have their payloads encrypted (AES) and require OTP validation, ensuring messages are secure from the client to the broker.

- Architectural Patterns and Design Principles:
- A clear separation of concerns is used:
▪ Client code for message publishing/subscribing.
▪ Broker (EC2-EMQX) ensures secure communication.
▪ Serverless functions (Lambda) provide modular and scalable authentication and encryption services.
- The design leverages event-driven principles where MQTT messages can trigger authentication processes.

- Technologies, Frameworks, and Libraries:
- MQTT and EMQX for message brokering.
- AWS Lambda for serverless, on-demand authentication.
- OTP and AES mechanisms for enhanced security (likely implemented within the code of Python-based Lambda functions).
- A web-based interface (using index.html and source.js) could be used for monitoring or administration of the broker.

## Diagram (Mermaid)

```mermaid
flowchart TD

    %% MQTT Clients Layer
    subgraph "MQTT Clients (pub/sub)"
        Pub("Publisher (pub.py)"):::clientStyle
        Sub("Subscriber (sub.py)"):::clientStyle
    end

    %% EMQX Broker Layer
    Broker("EMQX Broker (EC2)"):::brokerStyle

    %% Authentication Services Layer
    subgraph "Authentication Services (Lambda Functions)"
        Find("findEmailinDB.py"):::lambdaStyle
        Verify("verify_func.py"):::lambdaStyle
    end

    %% Data Flow from Clients to Broker
    Pub -->|"PublishEncryptedPayload"| Broker
    Broker -->|"SendMessageDelivery"| Sub

    %% Data Flow from Broker to Authentication Services and back
    Broker -->|"OTPVerificationRequest"| Find
    Broker -->|"OTPVerificationRequest"| Verify
    Find -->|"OTPVerificationResponse"| Broker
    Verify -->|"OTPVerificationResponse"| Broker

    %% Click Events for MQTT Clients
    click Pub "https://github.com/triledinh159/2fa-mqtt/blob/main/Client/pub.py"
    click Sub "https://github.com/triledinh159/2fa-mqtt/blob/main/Client/sub.py"

    %% Click Events for EMQX Broker
    click Broker "https://github.com/triledinh159/2fa-mqtt/blob/main/EC2-EMQX/index.html"
    click Broker "https://github.com/triledinh159/2fa-mqtt/blob/main/EC2-EMQX/source.js"

    %% Click Events for Authentication Services
    click Find "https://github.com/triledinh159/2fa-mqtt/blob/main/Lambda/findEmailinDB.py"
    click Verify "https://github.com/triledinh159/2fa-mqtt/blob/main/Lambda/verify_func.py"

    %% Styles
    classDef clientStyle fill:#AED6F1,stroke:#3498DB,stroke-width:2px;
    classDef brokerStyle fill:#A9DFBF,stroke:#28B463,stroke-width:2px;
    classDef lambdaStyle fill:#F9E79F,stroke:#F1C40F,stroke-width:2px;
```
