# g8row/authserver architecture

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

- Interactive diagram: https://gitdiagram.com/g8row/authserver
- Repository: https://github.com/g8row/authserver

## Overview

##### Project Type and Purpose
- This is a stand‐alone networked Authentication Server (no web GUI), built with Java NIO for nonblocking I/O.
- It provides session management, user/ban management, command parsing, and file‐based persistence for users and audit logs.

##### File Structure
- Top-level:
- MainClient.java (CLI client)
- MainServer.java (NIO-based server entry point)
- data/ (file-based database and logs)
- src/bg/sofia/uni/fmi/mjt/authserver/… (packages by feature: server, client, database, session, user, ban, log, response, exception)
- test/… (unit tests per feature)
- Configuration: none external; database is a text file (data/db.txt). Logs live under data/log and data/clientLogs.
- Build: standard Java project (no Maven/Gradle visible).

##### Extract Architectural Insights from README
- Java NIO library is used for networking.
- Key responsibilities: accept connections, parse commands, manage domain state (sessions, users, bans), persist data, produce logs, and send responses.

##### Main Components & Their Relationships
a. Client (MainClient)
- Initiates TCP connection to the server, sends text commands, receives text responses, writes its own client log.
b. Server (MainServer + Server class)
- Network layer: Java NIO Selector handles multiple channels.
- Command layer: Command parser reads input, maps to Command enum.
- Dispatcher: routes commands to appropriate Manager.
c. Business‐Logic Modules (each implements an “Api” interface)
- UserManager / UserManagerApi (create, find users)
- SessionManager / SessionManagerApi (create, validate sessions)
- BanManager / BanManagerApi (track banned clients)
- DatabaseManager / DatabaseManagerApi (load/save users to data/db.txt)
- AuditLog / AuditLogApi (append events to data/log/*.txt)
d. Persistence
- data/db.txt (users and credentials)
- data/log/… (audit log entries)
- data/clientLogs/… (client‐side logs)
e. Response (Response class)
- Encapsulates standard server replies.

Interactions:
- Client → Server (TCP Channel)
- Server’s Selector → accepts new connections → reads bytes → decodes to text → Command parser → returns a Command object
- Dispatcher → invokes the matching ManagerApi → may call DatabaseManager, SessionManager, BanManager or UserManager → returns data or throws exception
- AuditLog records each successful/failed operation → writes to file
- Server → wraps result in Response → writes back to client channel

##### Architectural Patterns & Design Principles
- Layered architecture:
1. Network layer (Java NIO Selector, Channel)
2. Command parsing layer
3. Business‐logic layer (managers)
4. Persistence layer (file I/O)
- Dependency inversion: business modules depend on interfaces (e.g., DatabaseManagerApi)
- Single Responsibility per package (user, session, ban, log)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client (CLI)" 
        direction TB
        MainClient["MainClient"]:::network
        ClientImpl["Client"]:::network
    end

    %% Server Layer
    subgraph "Server"
        direction TB
        MainServer["MainServer"]:::network
        ServerCore["Server"]:::network
    end

    %% Parsing Layer
    CommandParser["Command"]:::parsing

    %% Dispatcher
    Dispatcher["Dispatcher (in Server)"]:::network

    %% Business Logic Layer
    subgraph "Business Logic"
        direction TB
        UMApi["UserManagerApi"]:::logic
        UM["UserManager"]:::logic
        SMApi["SessionManagerApi"]:::logic
        SM["SessionManager"]:::logic
        BMApi["BanManagerApi"]:::logic
        BM["BanManager"]:::logic
        DBApi["DatabaseManagerApi"]:::logic
        DB["DatabaseManager"]:::logic
        ALApi["AuditLogApi"]:::logic
        AL["AuditLog"]:::logic
    end

    %% Persistence Layer
    subgraph "Persistence Layer"
        direction TB
        DBFile["data/db.txt"]:::persistence
        AuditLogDir["data/log/"]:::persistence
        ClientLogDir["data/clientLogs/"]:::persistence
    end

    %% Response
    Response["Response"]:::response

    %% Exceptions
    subgraph "Exceptions"
        direction TB
        EXInvalidCommand["InvalidCommandException"]:::exception
        EXInvalidSession["InvalidSessionException"]:::exception
        EXUserExists["UserExistsException"]:::exception
        EXUserNotFound["UserNotFoundException"]:::exception
        EXPermission["PermissionException"]:::exception
        EXDatabase["DatabaseException"]:::exception
        EXHash["HashException"]:::exception
    end

    %% Connections
    MainClient -->|"COMMAND_TEXT"| MainServer
    ClientImpl --> MainClient
    MainServer --> ServerCore
    ServerCore --> CommandParser
    CommandParser --> Dispatcher
    Dispatcher -->|calls| UMApi
    UMApi --> UM
    Dispatcher -->|calls| SMApi
    SMApi --> SM
    Dispatcher -->|calls| BMApi
    BMApi --> BM
    Dispatcher -->|calls| DBApi
    DBApi --> DB
    Dispatcher -->|calls| ALApi
    ALApi --> AL
    UM --> DBFile
    SM --> DBFile
    BM --> DBFile
    DB --> DBFile
    AL --> AuditLogDir
    Dispatcher --> Response
    Response -->|"RESPONSE_TEXT"| MainClient

    %% Exception flows
    UM -->|throws| EXUserExists
    UM -->|throws| EXUserNotFound
    SM -->|throws| EXInvalidSession
    CommandParser -->|throws| EXInvalidCommand
    DB -->|throws| EXDatabase
    UM -->|throws| EXHash

    %% Click Events
    click MainClient "https://github.com/g8row/authserver/blob/master/src/MainClient.java"
    click ClientImpl "https://github.com/g8row/authserver/blob/master/src/bg/sofia/uni/fmi/mjt/authserver/client/Client.java"
    click MainServer "https://github.com/g8row/authserver/blob/master/src/MainServer.java"
    click ServerCore "https://github.com/g8row/authserver/blob/master/src/bg/sofia/uni/fmi/mjt/authserver/server/Server.java"
    click CommandParser "https://github.com/g8row/authserver/blob/master/src/bg/sofia/uni/fmi/mjt/authserver/server/Command.java"
    click UMApi "https://github.com/g8row/authserver/blob/master/src/bg/sofia/uni/fmi/mjt/authserver/user/UserManagerApi.java"
    click UM "https://github.com/g8row/authserver/blob/master/src/bg/sofia/uni/fmi/mjt/authserver/user/UserManager.java"
    click SMApi "https://github.com/g8row/authserver/blob/master/src/bg/sofia/uni/fmi/mjt/authserver/session/SessionManagerApi.java"
    click SM "https://github.com/g8row/authserver/blob/master/src/bg/sofia/uni/fmi/mjt/authserver/session/SessionManager.java"
    click BMApi "https://github.com/g8row/authserver/blob/master/src/bg/sofia/uni/fmi/mjt/authserver/ban/BanManagerApi.java"
    click BM "https://github.com/g8row/authserver/blob/master/src/bg/sofia/uni/fmi/mjt/authserver/ban/BanManager.java"
    click DBApi "https://github.com/g8row/authserver/blob/master/src/bg/sofia/uni/fmi/mjt/authserver/database/DatabaseManagerApi.java"
    click DB "https://github.com/g8row/authserver/blob/master/src/bg/sofia/uni/fmi/mjt/authserver/database/DatabaseManager.java"
    click ALApi "https://github.com/g8row/authserver/blob/master/src/bg/sofia/uni/fmi/mjt/authserver/log/AuditLogApi.java"
    click AL "https://github.com/g8row/authserver/blob/master/src/bg/sofia/uni/fmi/mjt/authserver/log/AuditLog.java"
    click EXInvalidCommand "https://github.com/g8row/authserver/blob/master/src/bg/sofia/uni/fmi/mjt/authserver/exception/InvalidCommandException.java"
    click EXInvalidSession "https://github.com/g8row/authserver/blob/master/src/bg/sofia/uni/fmi/mjt/authserver/exception/InvalidSessionException.java"
    click EXUserExists "https://github.com/g8row/authserver/blob/master/src/bg/sofia/uni/fmi/mjt/authserver/exception/UserExistsException.java"
    click EXUserNotFound "https://github.com/g8row/authserver/blob/master/src/bg/sofia/uni/fmi/mjt/authserver/exception/UserNotFoundException.java"
    click EXPermission "https://github.com/g8row/authserver/blob/master/src/bg/sofia/uni/fmi/mjt/authserver/exception/PermissionException.java"
    click EXDatabase "https://github.com/g8row/authserver/blob/master/src/bg/sofia/uni/fmi/mjt/authserver/exception/DatabaseException.java"
    click EXHash "https://github.com/g8row/authserver/blob/master/src/bg/sofia/uni/fmi/mjt/authserver/exception/HashException.java"

    %% Styles
    classDef network fill:#E3F2FD,stroke:#90CAF9,stroke-width:1px
    classDef parsing fill:#FFF9C4,stroke:#FFF176,stroke-width:1px
    classDef logic fill:#E8F5E9,stroke:#A5D6A7,stroke-width:1px
    classDef persistence fill:#FFEBEE,stroke:#EF9A9A,stroke-width:1px
    classDef response fill:#F3E5F5,stroke:#CE93D8,stroke-width:1px
    classDef exception fill:#FFECB3,stroke:#FFD54F,stroke-width:1px
```
