# yeojz/otplib architecture

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

- Interactive diagram: https://gitdiagram.com/yeojz/otplib
- Repository: https://github.com/yeojz/otplib

## Overview

Based on our analysis of the project’s file tree and the README, this is not a typical full‑stack application but rather a modular, multi‑package (monorepo) JavaScript library called “otplib” that implements HOTP/TOTP functionality. It’s designed around a core OTP functionality that is extended via a set of plug‑in packages (for cryptography, base32 encoding/decoding, etc.) and configured via various presets (for browser usage, backward compatibility with v11, default sync and async modes).

##### Main Components
- Core OTP Modules: Represented by packages such as “otplib-core” and “otplib-core-async”. These contain the essential logic for generating and verifying the time‑based (TOTP) and HMAC‑based (HOTP) tokens.
- Plugin Modules: Represented by packages like “otplib-plugin-base32-enc-dec”, “otplib-plugin-crypto”, “otplib-plugin-crypto-js”, “otplib-plugin-crypto-async-ronomon”, and “otplib-plugin-thirty-two”. These plug into the core so that developers can customize cryptographic or encoding/decoding implementations.
- Preset Packages: Such as “otplib-preset-browser”, “otplib-preset-default”, “otplib-preset-default-async”, and “otplib-preset-v11”. They offer convenience “face‑lifts” for various environments (browser vs. Node.js) or legacy support.
- CI/Build & Configuration: Files like “lerna.json”, “babel.config.json”, various webpack/rollup configs, and .circleci workflows. These are not part of the runtime architecture but crucial for the build and release pipeline.
- Testing & Documentation: The “tests” directory (with test suites for OTP, plugins, etc.) ensures that the behavior against standard RFC test vectors is validated. The “website” folder holds the demo and documentation site.

##### Relationships and Interactions
- The documentation/website module interacts directly with the runtime output of the presets and core modules (as consumers of the API) to provide interactive demos.

##### Architectural Patterns and Design Principles
- Modularity: The system is set up as a monorepo (via Lerna) to manage multiple inter‑dependent packages. Emphasize that each package encapsulates a well‑defined responsibility (core functionality, plugins, presets).
- Plug‑in Architecture: The core OTP logic is made extensible with pluggable cryptography and encoding/decoding modules, which allow different libraries (for Node or browser environments) to be easily swapped in.
- Separation of Concerns: There’s a clear separation between:
- Business logic (the OTP generation/verification algorithms),
- Environmental wiring (different presets for Node.js, browser, legacy support),
- Configuration & build tooling (bundlers and config files), and
- Testing and CI (ensuring compliance with relevant standards like RFC 4226 & 6238).

##### Final Touches
- Components are clearly labeled.

– How core OTP logic is organized,
– The role that plugins play in providing cryptography and encoding functionalities,
– How presets adapt and expose a unified API for different runtime environments,
– And how CI, testing, and documentation support the overall development lifecycle.

This detailed breakdown will give you and any other software engineers a clear vision of the internal workings and extendability of “otplib” as well as its maintenance and distribution pipeline.

## Diagram (Mermaid)

```mermaid
flowchart TD

    %% External Supporting Nodes
    CI["CI/Build & Configuration"]:::ci
    TS["Test Suites"]:::test
    DOC["Documentation & Website"]:::doc

    %% Relationships from external nodes to Monorepo
    CI -->|"configures"| Pr1
    CI -->|"builds"| Pr1
    TS -->|"validates"| C1
    DOC -->|"demonstrates"| Pr1
    TS -->|"tests"| C1
    DOC -->|"consumes"| Pr1

    %% Monorepo (Lerna) containing all packages
    subgraph "Monorepo (Lerna)"
        %% Preset Packages (Integration layer)
        subgraph "Preset Packages"
            Pr1["Browser Preset"]:::preset
            Pr2["Default Preset"]:::preset
            Pr3["Default Async Preset"]:::preset
            Pr4["v11 Preset"]:::preset
        end

        %% Core Modules (Business logic)
        subgraph "Core Modules"
            C1["otplib-core"]:::core
            C2["otplib-core-async"]:::core
        end

        %% Plugin Modules (Extension points)
        subgraph "Plugin Modules"
            P1["Base32 Encoding/Decoding Plugin"]:::plugin
            P2["Crypto Plugin"]:::plugin
            P3["Crypto JS Plugin"]:::plugin
            P4["Async Crypto Plugin (Ronomon)"]:::plugin
            P5["Thirty-Two Plugin"]:::plugin
        end
    end

    %% Preset Packages integrate with Core and Plugins
    Pr1 -->|"uses"| C1
    Pr1 -->|"uses"| P1
    Pr2 -->|"uses"| C1
    Pr2 -->|"uses"| P1
    Pr3 -->|"uses"| C1
    Pr3 -->|"uses"| P1
    Pr4 -->|"uses"| C1
    Pr4 -->|"uses"| P1

    %% Core Modules extend Plugin Modules
    C1 -->|"extends"| P1
    C1 -->|"extends"| P2
    C1 -->|"extends"| P3
    C1 -->|"extends"| P4
    C1 -->|"extends"| P5

    C2 -->|"extends"| P1
    C2 -->|"extends"| P2
    C2 -->|"extends"| P3
    C2 -->|"extends"| P4
    C2 -->|"extends"| P5

    %% Class Definitions for color coding
    classDef core fill:#add8e6,stroke:#000,stroke-width:2px;
    classDef plugin fill:#90ee90,stroke:#000,stroke-width:2px;
    classDef preset fill:#ffcc99,stroke:#000,stroke-width:2px;
    classDef ci fill:#d3d3d3,stroke:#000,stroke-width:2px;
    classDef test fill:#e6e6fa,stroke:#000,stroke-width:2px;
    classDef doc fill:#fffacd,stroke:#000,stroke-width:2px;

    %% Click Events for Core Modules
    click C1 "https://github.com/yeojz/otplib/tree/master/packages/otplib-core"
    click C2 "https://github.com/yeojz/otplib/tree/master/packages/otplib-core-async"

    %% Click Events for Plugin Modules
    click P1 "https://github.com/yeojz/otplib/tree/master/packages/otplib-plugin-base32-enc-dec"
    click P2 "https://github.com/yeojz/otplib/tree/master/packages/otplib-plugin-crypto"
    click P3 "https://github.com/yeojz/otplib/tree/master/packages/otplib-plugin-crypto-js"
    click P4 "https://github.com/yeojz/otplib/tree/master/packages/otplib-plugin-crypto-async-ronomon"
    click P5 "https://github.com/yeojz/otplib/tree/master/packages/otplib-plugin-thirty-two"

    %% Click Events for Preset Packages
    click Pr1 "https://github.com/yeojz/otplib/tree/master/packages/otplib-preset-browser"
    click Pr2 "https://github.com/yeojz/otplib/tree/master/packages/otplib-preset-default"
    click Pr3 "https://github.com/yeojz/otplib/tree/master/packages/otplib-preset-default-async"
    click Pr4 "https://github.com/yeojz/otplib/tree/master/packages/otplib-preset-v11"

    %% Click Events for CI/Build & Testing/Documentation
    click CI "https://github.com/yeojz/otplib/blob/master/lerna.json"
    click TS "https://github.com/yeojz/otplib/tree/master/tests"
    click DOC "https://github.com/yeojz/otplib/tree/master/website"
```
