# kech37/otp-authentication architecture

> Architecture diagram of the GitHub repository kech37/otp-authentication, 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/kech37/otp-authentication
- Repository: https://github.com/kech37/otp-authentication

## Overview

##### Main Components
- OTP Generator: This module encapsulates the creation of one-time passwords (relying on an external otp-generator package).
- Cache Service: Manages OTP storage with the default implementation using Redis. Note the abstraction available (from src/@types/cache-service-abstract.ts) to allow custom implementations.
- Delivery Service: Responsible for sending OTPs. There are two delivery options:
- Email Delivery using Nodemailer (via src/lib/nodemailer-delivery-service.ts) along with configuration settings for mail options.
- SMS Delivery using Twilio (via src/lib/twilio-delivery-service.ts).
- Central OTP Authentication Class: This core module (found in src/lib/otp-authentication.ts) orchestrates the OTP creation, storage, and delivery processes.
- External Dependencies: These include Redis, Nodemailer, Twilio, and the underlying npm otp-generator package.

##### Out Relationships and Interactions
- The OTP Authentication component calls on the OTP Generator to produce an OTP.
- It then interacts with the Cache Service to store the OTP (with a TTL – Time-To-Live – for security).
- Depending on the chosen method (email or SMS), the OTP Authentication component uses either the Email or SMS Delivery Service to send the OTP to the user.
- From the OTP Authentication module to the OTP Generator (“Generate OTP”).
- From OTP Authentication to Cache Service (“Store OTP” and “Retrieve OTP”).
- From OTP Authentication to Delivery Service (Email or SMS) (“Send OTP”).
- Flows from external components like the Redis server, SMTP server, or Twilio’s API indicating actual interactions when sending or fetching data.

##### Architectural Patterns & Design Principles
- Separation of Concerns: Each major functionality (OTP generation, cache management, and delivery) is encapsulated in its own module. This separation should be visually represented as distinct layers or blocks.
- Dependency Injection: The constructors for EmailOTPAuthentication and SMSOTPAuthentication take configuration options for both the cache and delivery services. Illustrate this flow as “injected dependencies” which allow easy swapping of implementations.

- Be detailed: ensure that every component’s role—how it generates, stores, or transmits the OTP—is captured.
- Finally, emphasize the orchestration flow: when a request arrives (via an Express endpoint or similar), showing the full lifecycle from OTP generation, caching, delivery, and subsequent verification.

## Diagram (Mermaid)

```mermaid
graph TD
    subgraph "Core Library Components"
        Client["Client/Express Application"]:::client
        Auth["OTP Authentication Coordinator"]:::internal
        Gen["OTP Generator (uses otp-generator package)"]:::internal
        CacheAbstract["Cache Service (Abstract)"]:::abstract
        RedisImpl["Cache Service – Redis (Default)"]:::internal
        DeliveryAbstract["Delivery Service (Abstract)"]:::abstract
        EmailDelivery["Delivery Service – Email (Nodemailer)"]:::internal
        SMSDelivery["Delivery Service – SMS (Twilio)"]:::internal
    end

    subgraph "External Systems"
        RedisServer["Redis Server"]:::external
        SMTPServer["SMTP Server"]:::external
        TwilioAPI["Twilio API"]:::external
    end

    Client -->|"requestsOTP"| Auth
    Auth -->|"generateOTP"| Gen
    Auth -->|"storeOTP"| CacheAbstract
    CacheAbstract -->|"implementedBy"| RedisImpl
    RedisImpl -->|"cacheOps"| RedisServer
    Auth -->|"sendOTP"| DeliveryAbstract
    DeliveryAbstract -->|"email"| EmailDelivery
    DeliveryAbstract -->|"sms"| SMSDelivery
    EmailDelivery -->|"connectsTo"| SMTPServer
    SMSDelivery -->|"connectsTo"| TwilioAPI

    click Gen "https://github.com/kech37/otp-authentication/blob/master/src/lib/otp-authentication.ts"
    click Auth "https://github.com/kech37/otp-authentication/blob/master/src/lib/otp-authentication.ts"
    click CacheAbstract "https://github.com/kech37/otp-authentication/blob/master/src/@types/cache-service-abstract.ts"
    click RedisImpl "https://github.com/kech37/otp-authentication/blob/master/src/lib/redis-cache-service.ts"
    click DeliveryAbstract "https://github.com/kech37/otp-authentication/blob/master/src/@types/delivery-service-abstract.ts"
    click EmailDelivery "https://github.com/kech37/otp-authentication/blob/master/src/lib/nodemailer-delivery-service.ts"
    click SMSDelivery "https://github.com/kech37/otp-authentication/blob/master/src/lib/twilio-delivery-service.ts"

    classDef internal fill:#BBDEFB,stroke:#0D47A1,stroke-width:2px;
    classDef external fill:#C8E6C9,stroke:#1B5E20,stroke-width:2px;
    classDef client fill:#FFCDD2,stroke:#B71C1C,stroke-width:2px;
    classDef abstract fill:#FFF9C4,stroke:#F57F17,stroke-width:2px;
```
