# amuzcorp/authapi architecture

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

- Interactive diagram: https://gitdiagram.com/amuzcorp/authapi
- Repository: https://github.com/amuzcorp/authapi

## Overview

##### Main Components
- Plugin Bootstrap & Registration:
- plugin.php – This is the entry point for the plugin that integrates with XpressEngine3.
- composer.json – Manages dependencies.
- Service Layer:
- The primary service (AuthApiService in src/AuthApiService.php) that is registered and obtained via app('amuz.authapi').
- ApiMigration.php (likely used for any DB migrations related to API keys).
- Command Line Interface (CLI):
- The src/Commands directory houses several artisan commands (ActivateApiKey, DeactivateApiKey, DeleteApiKey, GenerateApiKey, ListApiKeys) which provide direct management of API keys.
- Controllers & Middleware:
- src/Controller.php – Handles incoming requests.
- src/Middleware/AuthorizeApiKey.php – Ensures that API key authorization is enforced.
- Data Models:
- src/Models/ApiKey.php, ApiKeyAccessEvent.php, and ApiKeyAdminEvent.php – Represent the data and events tied to API keys.
- Presentation Layer:
- The views (Blade templates in views/ folder) provide the UI components for creating or listing API keys.
- Assets:
- Contains style.css for frontend styling.

##### Relationships and Interactions
- The Plugin Bootstrap (plugin.php) initializes the AuthAPI plugin and registers the AuthApiService with the application container.
- The AuthApiService is the core that other parts (controllers, commands) use to create, delete, or toggle API keys. It encapsulates business logic.
- CLI Commands interact with the AuthApiService directly to perform actions (activation, deletion, listing, etc.) based on the artisan command calls.
- The Controller likely acts as a bridge between HTTP requests (coming from users or external API calls) and the underlying service.
- Middleware (AuthorizeApiKey) intercepts requests to ensure authorized API access before reaching controllers.
- Models represent and persist the state of API keys and their access events.
- Views provide a web interface for administrators and end-users to interact with the API key management functions.

##### Architectural Patterns and Design Principles
- Separation of Concerns: The project separates concerns by isolating API key management, business logic, request handling, and presentation.
- Service-oriented Design: The core functionality is implemented in a unified service that is leveraged by both HTTP controllers and CLI commands.
- MVC-Like structure: Although it is a plugin, it still aligns with the MVC approach – models (under src/Models), controllers (src/Controller.php), and view templates.
- Middleware usage: Cleanly separates authorization logic from request processing.
- Extensibility: The README describes hooks (via XeRegister) to allow third-party plugins to register REST services and verify permissions.

##### Relevant Technologies, Frameworks, and Libraries
- PHP as the main language.
- Blade templating engine for views.
- Artisan CLI (typical with Laravel or similar platforms) suggests conventions inspired by Laravel.
- Eloquent ORM for model operations (as seen from the usage in README).
- XpressEngine3 integration as the host platform for the plugin.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Plugin Bootstrapping
    subgraph "Plugin Bootstrapping"
        PB1["Plugin Entry Point (plugin.php)"]:::bootstrap
        PB2["Dependency Manager (composer.json)"]:::bootstrap
    end

    %% Service Layer
    subgraph "Service Layer"
        SL1["AuthApiService (src/AuthApiService.php)"]:::service
        SL2["DB Migration (src/ApiMigration.php)"]:::service
    end

    %% HTTP Request Handling Layer
    subgraph "HTTP Layer"
        HL1["Request Controller (src/Controller.php)"]:::controller
        HL2["Authorize Middleware (src/Middleware/AuthorizeApiKey.php)"]:::middleware
    end

    %% CLI Commands Layer
    subgraph "CLI Layer"
        CLI1["ActivateApiKey (src/Commands/ActivateApiKey.php)"]:::cli
        CLI2["DeactivateApiKey (src/Commands/DeactivateApiKey.php)"]:::cli
        CLI3["DeleteApiKey (src/Commands/DeleteApiKey.php)"]:::cli
        CLI4["GenerateApiKey (src/Commands/GenerateApiKey.php)"]:::cli
        CLI5["ListApiKeys (src/Commands/ListApiKeys.php)"]:::cli
    end

    %% Data Models (Persistence)
    subgraph "Persistence Layer"
        DM1["API Key Model (src/Models/ApiKey.php)"]:::model
        DM2["API Key Access Event Model (src/Models/ApiKeyAccessEvent.php)"]:::model
        DM3["API Key Admin Event Model (src/Models/ApiKeyAdminEvent.php)"]:::model
    end

    %% Presentation and Assets
    subgraph "Presentation Layer"
        PV1["Create API Key View (views/create.blade.php)"]:::view
        PV2["List API Keys View (views/index.blade.php)"]:::view
    end

    subgraph "Assets"
        AS1["Frontend Styling (assets/style.css)"]:::asset
    end

    %% Interactions
    %% Plugin Bootstrapping registers Service Layer
    PB1 -->|"registers"| SL1
    PB1 -->|"initializes"| SL2

    %% HTTP Flow: HTTP request goes to Controller, through Middleware then to Service Layer
    HL1 -->|"passes through"| HL2
    HL2 -->|"calls"| SL1
    SL1 -->|"utilizes"| DM1
    SL1 -->|"logs events to"| DM2
    SL1 -->|"logs admin actions to"| DM3

    %% CLI Flow: CLI Commands invoke Service Layer
    CLI1 -->|"invokes"| SL1
    CLI2 -->|"invokes"| SL1
    CLI3 -->|"invokes"| SL1
    CLI4 -->|"invokes"| SL1
    CLI5 -->|"invokes"| SL1

    %% Controller renders Views
    HL1 -->|"renders"| PV1
    HL1 -->|"renders"| PV2

    %% External Integration (via XeRegister)
    PB1 ---|"integrates with"| HL1

    %% Styles applied to Presentation
    PV1 ---|"styled by"| AS1
    PV2 ---|"styled by"| AS1

    %% Click Events for Plugin Bootstrapping
    click PB1 "https://github.com/amuzcorp/authapi/blob/main/plugin.php"
    click PB2 "https://github.com/amuzcorp/authapi/blob/main/composer.json"

    %% Click Events for Service Layer
    click SL1 "https://github.com/amuzcorp/authapi/blob/main/src/AuthApiService.php"
    click SL2 "https://github.com/amuzcorp/authapi/blob/main/src/ApiMigration.php"

    %% Click Events for CLI Commands Layer
    click CLI1 "https://github.com/amuzcorp/authapi/blob/main/src/Commands/ActivateApiKey.php"
    click CLI2 "https://github.com/amuzcorp/authapi/blob/main/src/Commands/DeactivateApiKey.php"
    click CLI3 "https://github.com/amuzcorp/authapi/blob/main/src/Commands/DeleteApiKey.php"
    click CLI4 "https://github.com/amuzcorp/authapi/blob/main/src/Commands/GenerateApiKey.php"
    click CLI5 "https://github.com/amuzcorp/authapi/blob/main/src/Commands/ListApiKeys.php"
    click CLI1 "https://github.com/amuzcorp/authapi/tree/main/src/Commands"
    click CLI2 "https://github.com/amuzcorp/authapi/tree/main/src/Commands"
    click CLI3 "https://github.com/amuzcorp/authapi/tree/main/src/Commands"
    click CLI4 "https://github.com/amuzcorp/authapi/tree/main/src/Commands"
    click CLI5 "https://github.com/amuzcorp/authapi/tree/main/src/Commands"

    %% Click Events for HTTP Layer
    click HL1 "https://github.com/amuzcorp/authapi/blob/main/src/Controller.php"
    click HL2 "https://github.com/amuzcorp/authapi/blob/main/src/Middleware/AuthorizeApiKey.php"

    %% Click Events for Persistence Layer
    click DM1 "https://github.com/amuzcorp/authapi/blob/main/src/Models/ApiKey.php"
    click DM2 "https://github.com/amuzcorp/authapi/blob/main/src/Models/ApiKeyAccessEvent.php"
    click DM3 "https://github.com/amuzcorp/authapi/blob/main/src/Models/ApiKeyAdminEvent.php"

    %% Click Events for Presentation Layer
    click PV1 "https://github.com/amuzcorp/authapi/blob/main/views/create.blade.php"
    click PV2 "https://github.com/amuzcorp/authapi/blob/main/views/index.blade.php"
    click PV1 "https://github.com/amuzcorp/authapi/tree/main/views"
    click PV2 "https://github.com/amuzcorp/authapi/tree/main/views"

    %% Click Event for Assets
    click AS1 "https://github.com/amuzcorp/authapi/blob/main/assets/style.css"

    %% Class Definitions (Colors)
    classDef bootstrap fill:#f9f,stroke:#333,stroke-width:2px;
    classDef service fill:#cfc,stroke:#333,stroke-width:2px;
    classDef controller fill:#ccf,stroke:#333,stroke-width:2px;
    classDef middleware fill:#fcc,stroke:#333,stroke-width:2px;
    classDef cli fill:#fc9,stroke:#333,stroke-width:2px;
    classDef model fill:#cff,stroke:#333,stroke-width:2px;
    classDef view fill:#ffc,stroke:#333,stroke-width:2px;
    classDef asset fill:#d9d,stroke:#333,stroke-width:2px;
```
