# ktaka-ccmp/oauth2-passkey architecture

> Architecture diagram of the GitHub repository ktaka-ccmp/oauth2-passkey, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ktaka-ccmp/oauth2-passkey
- Repository: https://github.com/ktaka-ccmp/oauth2-passkey

## Overview

- Frontend/Client Layer: Represent the demo applications (demo-integration, demo-oauth2, demo-passkey) that provide UI pages and interactions using templates (Jinja2-style, with .j2 files) alongside related javascript static files.
- Authentication Coordination Layer: Highlight the core library (oauth2_passkey) which consolidates several modules:
- OAuth2 Module: Covers configuration, storage, and processing for OAuth2 authentication, including integration (like Google).
- Passkey Module: Handles passkey credential registration, authentication flows (attestation, assertions), and related origin validations.
- Session & UserDB Modules: Manage session boundaries, context token verification, and user database interactions. Group components such as libsession, libstorage, and userdb (which manage state and persistence).
- Storage & Database Layer: Include separate representations for the different data stores:
- SQL Databases (PostgreSQL, SQLite) and their associated Docker Compose configurations.
- Redis for caching (with a dedicated cache store implementation).
- Clear_db_cache.sh and monitor_db.sh as auxiliary maintenance scripts.
- Configuration & Deployment: Mark the configuration files (Cargo.toml for each project, dot.env.example) and deployment scripts (docker-compose.yaml files present under db directories) that ensure consistency of the environment and schema checks during database initialization.

##### Relationships and Interactions
- How the web server (oauth2_passkey_axum) invokes the coordination layer (oauth2_passkey) which, in turn, interacts with the storage/data store modules.
- Session boundaries and middleware validation flows, for example, the validation logic that differentiates between “create new user” versus “add to existing user.”
- Interaction flows for external OAuth2 providers and signals (e.g., signalAllAcceptedCredentials, signalCurrentUserDetails, and signalUnknownCredential) that communicate between the relying party and authenticators.

##### Architectural Patterns and Design Principles
- Modular and Layered Design: The project is split into multiple coordinated libraries (modules for OAuth2, passkey, session, storage) that get consolidated into one cohesive authentication ecosystem (often referred to as libauth in the README).
- Separation of Concerns: Clearly separate the frontend (UI and demo pages), backend authentication logic (coordinators and modules), and persistence layers.
- Middleware-Based Protection: There’s an emphasis on middleware (e.g., is_authorized middleware) ensuring that different pages and endpoints enforce correct session/user context.

##### Technologies and Frameworks
- Rust as the programming language with Cargo for dependency management.
- Axum framework for building the HTTP server and routing middleware.
- Docker Compose for containerizing PostgreSQL, Redis, and SQLite configurations.
- WebAuthn/Passkey and OAuth2 fundamentals as implemented in separate modules, along with integration of self-signed TLS certificates for demo environments.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend/Client Layer
    subgraph "Frontend/Client Layer"
        FI["Demo Integration"]
        FO["Demo OAuth2"]
        FP["Demo Passkey"]
    end

    %% Web Server & Routing Layer
    subgraph "Web Server & Routing Layer"
        AS["Axum Server (Main Logic)"]
        RM["Routes & Middleware"]
        PH["Page Handlers & Static Assets"]
    end

    %% Authentication Coordination Layer
    subgraph "Authentication Coordination Layer"
        AC["Coordination Module"]
        O2["OAuth2 Module"]
        PK["Passkey Module"]
        SM["Session Module"]
        ST["Storage/Cache Module"]
        UDB["UserDB Module"]
        UF["Utility Functions"]
    end

    %% Storage & Database Layer
    subgraph "Storage & Database Layer"
        PG["PostgreSQL DB"]
        RS["Redis Cache"]
        SQ["SQLite DB"]
        CDB["Clear DB Cache Script"]
        MDB["Monitor DB Script"]
    end

    %% Configuration & Deployment
    subgraph "Configuration & Deployment"
        CFG["Cargo.toml & .env"]
        DC["Docker Compose Files"]
        DOC["Documentation"]
    end

    %% External Component
    X["External OAuth2 Providers"]:::external

    %% Connections between layers
    FI -->|"HTTP"| AS
    FO -->|"HTTP"| AS
    FP -->|"HTTP"| AS

    AS -->|"Routes"| RM
    RM -->|"StaticPages"| PH

    AS -->|"Invoke"| AC

    AC -->|"Handle_OAuth2"| O2
    AC -->|"Handle_Passkey"| PK
    AC -->|"Manage_Session"| SM
    AC -->|"Manage_Storage"| ST
    AC -->|"Lookup_UserDB"| UDB
    AC -->|"Utilize_Utils"| UF

    O2 -->|"ExternalAuth"| X

    ST -->|"Query_DB"| PG
    ST -->|"Cache_Data"| RS
    ST -->|"Query_DB"| SQ

    %% Configuration support connections
    CFG --- AS
    CFG --- AC
    CFG --- PG

    DC --- PG
    DC --- RS
    DC --- SQ

    DOC --- CFG

    %% Styles for color coding
    classDef frontend fill:#cce5ff,stroke:#000,stroke-width:2px;
    classDef webserver fill:#d4edda,stroke:#000,stroke-width:2px;
    classDef auth fill:#fff3cd,stroke:#000,stroke-width:2px;
    classDef storage fill:#f8d7da,stroke:#000,stroke-width:2px;
    classDef config fill:#d1c4e9,stroke:#000,stroke-width:2px;
    classDef external fill:#e2e3e5,stroke:#000,stroke-width:2px;

    class FI,FO,FP frontend;
    class AS,RM,PH webserver;
    class AC,O2,PK,SM,ST,UDB,UF auth;
    class PG,RS,SQ,CDB,MDB storage;
    class CFG,DC,DOC config;

    %% Click Events for Frontend/Client Layer
    click FI "https://github.com/ktaka-ccmp/oauth2-passkey/tree/master/demo-integration/"
    click FO "https://github.com/ktaka-ccmp/oauth2-passkey/tree/master/demo-oauth2/"
    click FP "https://github.com/ktaka-ccmp/oauth2-passkey/tree/master/demo-passkey/"

    %% Click Events for Web Server & Routing Layer
    click AS "https://github.com/ktaka-ccmp/oauth2-passkey/blob/master/oauth2_passkey_axum/src/lib.rs"
    click RM "https://github.com/ktaka-ccmp/oauth2-passkey/blob/master/oauth2_passkey_axum/src/middleware.rs"
    click PH "https://github.com/ktaka-ccmp/oauth2-passkey/tree/master/oauth2_passkey_axum/src/pages/"

    %% Click Events for Authentication Coordination Layer
    click AC "https://github.com/ktaka-ccmp/oauth2-passkey/tree/master/oauth2_passkey/src/coordination/"
    click O2 "https://github.com/ktaka-ccmp/oauth2-passkey/tree/master/oauth2_passkey/src/oauth2/"
    click PK "https://github.com/ktaka-ccmp/oauth2-passkey/tree/master/oauth2_passkey/src/passkey/"
    click SM "https://github.com/ktaka-ccmp/oauth2-passkey/tree/master/oauth2_passkey/src/session/"
    click ST "https://github.com/ktaka-ccmp/oauth2-passkey/tree/master/oauth2_passkey/src/storage/"
    click UDB "https://github.com/ktaka-ccmp/oauth2-passkey/tree/master/oauth2_passkey/src/userdb/"
    click UF "https://github.com/ktaka-ccmp/oauth2-passkey/blob/master/oauth2_passkey/src/utils.rs"

    %% Click Events for Storage & Database Layer
    click PG "https://github.com/ktaka-ccmp/oauth2-passkey/tree/master/db/postgresql/"
    click RS "https://github.com/ktaka-ccmp/oauth2-passkey/tree/master/db/redis/"
    click SQ "https://github.com/ktaka-ccmp/oauth2-passkey/tree/master/db/sqlite/"
    click CDB "https://github.com/ktaka-ccmp/oauth2-passkey/blob/master/clear_db_cache.sh"
    click MDB "https://github.com/ktaka-ccmp/oauth2-passkey/blob/master/monitor_db.sh"

    %% Click Events for Configuration & Deployment
    click CFG "https://github.com/ktaka-ccmp/oauth2-passkey/blob/master/Cargo.toml"
    click DC "https://github.com/ktaka-ccmp/oauth2-passkey/blob/master/db/postgresql/docker-compose.yaml"
    click DOC "https://github.com/ktaka-ccmp/oauth2-passkey/blob/master/docs/architecture.md"
```
