# ssaisriharsha/otp-authentication architecture

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

- Interactive diagram: https://gitdiagram.com/ssaisriharsha/otp-authentication
- Repository: https://github.com/ssaisriharsha/otp-authentication

## Overview

##### Project Type and Purpose
- Type: Standalone backend microservice (Spring Boot monolith).
- Purpose: Expose REST endpoints to (a) generate OTPs for a given mobile number, (b) verify submitted OTPs.
- API Consumers: mobile apps, web front-ends, other backend services.

##### Main Components

1. Clients (External)
– Mobile App
– Web App
– Other Services

2. API Layer (Presentation)
– VerificationController
– Exception Handler (VerificationControllerAdvice)

3. Business Logic Layer
– VerificationService (core OTP generation & validation)

4. Data Layer
– In-memory store (e.g., ConcurrentHashMap) or external cache/DB (Redis, SQL) for persisting OTPs

5. Integration Layer (External Services)
– SMS Gateway (e.g., Twilio) for sending OTPs

6. Cross-Cutting Configuration
– CORS Config (CorsConfig)
– Spring Security (if extended)
– application.properties (configuration)

##### Relationships and Data Flows
1. Client → API Layer
- “POST /otp” to request OTP
- “POST /otp/verify” to validate OTP
2. API Layer → Business Logic
- Controller calls VerificationService
3. Business Logic → Data Layer
- Store generated OTP with timestamp
- Retrieve and compare OTP on verify
4. Business Logic → External SMS Gateway
- Send SMS with generated OTP
5. Data Layer → Business Logic
- Return stored OTP for comparison
6. API Layer → Client
- Return success/failure responses (GetOtpResponse, Validated DTO)

##### Architectural Patterns and Principles
- Layered Architecture: clear separation of Controller (API), Service (business logic), Data Store
- DTOs: used for request/response payloads (PhNo, GetOtpResponse, Validated)
- Exception-Handling Advice: centralized error handling via VerificationControllerAdvice
- Configuration as Code: CORS, security, externalized properties

##### Technologies and Frameworks
- Java 17 (or your target Java version)
- Spring Boot (autoconfiguration, embedded Tomcat)
- Maven for build and dependency management
- (Optional) SMS provider SDK (e.g., Twilio Java)
- (Optional) Redis or relational DB

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Clients
    subgraph "Clients"
        MobileApp["Mobile App"]:::external
        WebApp["Web App"]:::external
        OtherService["Other Services"]:::external
    end

    %% API Layer
    subgraph "API Layer"
        Controller["VerificationController\n(POST /otp, /otp/verify)"]:::api
        Advice["VerificationControllerAdvice"]:::api
        subgraph "DTOs"
            PhNo["PhNo DTO"]:::api
            Validator["Validator DTO"]:::api
            GetOtpResponse["GetOtpResponse DTO"]:::api
            Validated["Validated DTO"]:::api
        end
    end

    %% Business Logic
    subgraph "Business Logic"
        Service["VerificationService"]:::service
    end

    %% Data Layer
    subgraph "Data Layer"
        InMemory["In-Memory Store\n(ConcurrentHashMap)"]:::data
    end

    %% Integration Layer
    subgraph "Integration Layer"
        SMS["SMS Gateway (Twilio)"]:::external
    end

    %% Configuration and Entry
    subgraph "Configuration"
        Entry["MobileOtpAuthenticationApplication"]:::config
        Cors["CorsConfig"]:::config
        AppProps["application.properties"]:::config
    end

    %% Tests
    subgraph "Test Suite"
        Test["MobileOtpAuthenticationApplicationTests"]:::config
    end

    %% Data Flows
    MobileApp -->|POST /otp| Controller
    WebApp -->|POST /otp| Controller
    OtherService -->|POST /otp| Controller

    MobileApp -->|POST /otp/verify| Controller
    WebApp -->|POST /otp/verify| Controller
    OtherService -->|POST /otp/verify| Controller

    Controller -->|"generateOTP(PhNo)"| Service
    Controller -->|"validateOTP(Validator)"| Service

    Service -->|"store OTP"| InMemory
    Service -->|"retrieve OTP"| InMemory
    InMemory -->|"return OTP"| Service

    Service -->|"sendSMS(otp)"| SMS

    Service -->|"return GetOtpResponse, Validated"| Controller
    Controller -->|"response DTOs"| MobileApp
    Controller -->|"response DTOs"| WebApp
    Controller -->|"response DTOs"| OtherService

    %% Configuration Flows
    Entry -->|initializes| Controller
    Entry -->|initializes| Advice
    Entry -->|initializes| Service
    Entry -->|loads| Cors
    AppProps -->|configures| Entry
    AppProps -->|configures| Cors
    Cors -->|applies CORS| Controller

    Test -->|validates| Entry

    %% Click Events
    click Controller "https://github.com/ssaisriharsha/otp-authentication/blob/main/src/main/java/com/levyug/MobileOTPAuthentication/Controller/VerificationController.java"
    click Advice "https://github.com/ssaisriharsha/otp-authentication/blob/main/src/main/java/com/levyug/MobileOTPAuthentication/ExceptionHandler/VerificationControllerAdvice.java"
    click Service "https://github.com/ssaisriharsha/otp-authentication/blob/main/src/main/java/com/levyug/MobileOTPAuthentication/Service/VerificationService.java"
    click PhNo "https://github.com/ssaisriharsha/otp-authentication/blob/main/src/main/java/com/levyug/MobileOTPAuthentication/DTO/PhNo.java"
    click Validator "https://github.com/ssaisriharsha/otp-authentication/blob/main/src/main/java/com/levyug/MobileOTPAuthentication/DTO/Validator.java"
    click GetOtpResponse "https://github.com/ssaisriharsha/otp-authentication/blob/main/src/main/java/com/levyug/MobileOTPAuthentication/DTO/GetOtpResponse.java"
    click Validated "https://github.com/ssaisriharsha/otp-authentication/blob/main/src/main/java/com/levyug/MobileOTPAuthentication/DTO/Validated.java"
    click Cors "https://github.com/ssaisriharsha/otp-authentication/blob/main/src/main/java/com/levyug/MobileOTPAuthentication/SecurityConfig/CorsConfig.java"
    click Entry "https://github.com/ssaisriharsha/otp-authentication/blob/main/src/main/java/com/levyug/MobileOTPAuthentication/MobileOtpAuthenticationApplication.java"
    click AppProps "https://github.com/ssaisriharsha/otp-authentication/blob/main/src/main/resources/application.properties"
    click Test "https://github.com/ssaisriharsha/otp-authentication/blob/main/src/test/java/com/levyug/MobileOTPAuthentication/MobileOtpAuthenticationApplicationTests.java"

    %% Styles
    classDef external fill:#eeeeee,stroke:#616161,color:#000
    classDef api fill:#bbdefb,stroke:#0d47a1,color:#000
    classDef service fill:#c8e6c9,stroke:#1b5e20,color:#000
    classDef data fill:#fff9c4,stroke:#f57f17,color:#000
    classDef config fill:#e0e0e0,stroke:#424242,color:#000
```
