# atakancelik/lockedswagger architecture

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

- Interactive diagram: https://gitdiagram.com/atakancelik/lockedswagger
- Repository: https://github.com/atakancelik/lockedswagger

## Overview

##### Project type and purpose
- This is a small ASP.NET Core “middleware library” project (distributed as a sample solution) whose sole purpose is to inject Basic Authentication protection into the Swagger UI of any ASP.NET Core Web API.
- It is not a full-stack application with separate frontend/backends; it is a single-project extension that plugs into the ASP.NET Core pipeline.

##### File structure
- Top-level solution file: `LockedSwagger.sln`
- Single project directory: `LockedSwagger/` containing:
- Controllers (default WeatherForecastController)
- `Program.cs` (ASP.NET Core host builder)
- `SwaggerBasicAuthMiddleware.cs` (the core middleware component)
- `appsettings.json` (+ Development variant) for binding Authentication:Users
- Standard .NET config (`Properties/launchSettings.json`) and project file.
- No separate “frontend” or “database” folders. No external microservices.

##### README for additional insights
- Describes how to wire up the middleware in `Startup.Configure` (or in `Program.cs` for .NET 6+ minimal hosting).
- Explains runtime behavior: on requests to `/swagger`, the middleware issues a 401 challenge if no or invalid Basic credentials are provided; otherwise it falls through to the Swagger generator.

##### Main components and how to represent them
a. ASP.NET Core Host
- Kestrel Web Server
- Middleware Pipeline
b. SwaggerBasicAuthMiddleware
- Reads configured users from IOptions bound to `appsettings.json`
- Intercepts requests whose path matches `/swagger` (Swagger endpoints)
- Validates `Authorization: Basic` header
c. Swagger Generator (Swashbuckle)
- Produces the Swagger JSON & UI
d. Configuration Store
- In-memory binding to `appsettings.json` → list of valid credentials
e. Sample Controller (WeatherForecastController)
- Represents the downstream API controllers registered in the pipeline
f. Client (Browser)
- Initiates HTTP request to `/swagger` endpoint

##### Relationships and interactions
- Client sends HTTP GET /swagger → Enters Kestrel →
1. Hits ASP.NET Core middleware pipeline
2. Reaches SwaggerBasicAuthMiddleware
- Reads Authorization header
- Checks credentials against Configuration Store
- If invalid: returns 401 Unauthorized (end of pipeline)
- If valid: calls `next()` →
3. Falls through to Swashbuckle’s Swagger generator → returns UI/JSON
- Downstream API controllers (e.g., WeatherForecastController) are unaffected by this middleware except at the Swagger UI boundary.

##### Architectural patterns and design principles
- Middleware pattern (chain of responsibility) to intercept and optionally short-circuit HTTP requests
- Configuration-as-Code (appsettings.json → IOptions) for credentials
- Separation of concerns: authentication logic lives in a reusable middleware class, not tangled with controllers
- Minimal external dependencies (pure ASP.NET Core + Swashbuckle + .NET configuration system)

## Diagram (Mermaid)

```mermaid
flowchart TD
    Browser["Browser"]:::client
    Browser -->|"HTTP GET /swagger"| Kestrel

    subgraph "ASP.NET Core Host (Kestrel + Middleware Pipeline)"
        Kestrel["Kestrel Web Server"]:::host
        Kestrel --> Pipeline["Middleware Pipeline"]:::host
        Pipeline --> AuthMiddleware["SwaggerBasicAuthMiddleware"]:::middleware
        AuthMiddleware -->|"Validate Basic Header"| ConfigStore["Configuration Store (IOptions<AuthenticationOptions>)"]:::config
        AuthMiddleware -->|"Unauthorized or call next()"| SwashGen["Swashbuckle Swagger Generator & UI"]:::swagger
        Pipeline --> ControllersGroup
    end

    subgraph "Downstream API Controllers"
        Controller["WeatherForecastController"]:::controller
        Model["WeatherForecast Model"]:::controller
    end

    SwashGen -->|"Serve Swagger UI/JSON"| Browser
    SwashGen -->|"Invoke API endpoints"| Controller
    Controller --> Model

    ConfigStore -.-> AppSettings["appsettings.json"]:::configFile
    ConfigStore -.-> AppSettingsDev["appsettings.Development.json"]:::configFile

    ProjectFile["LockedSwagger.csproj"]:::project
    LaunchSettings["launchSettings.json"]:::configFile
    HTTPReq["LockedSwagger.http"]:::other
    Solution["LockedSwagger.sln"]:::other
    Readme["README.md"]:::other

    click Kestrel "https://github.com/atakancelik/lockedswagger/blob/master/LockedSwagger/Program.cs"
    click AuthMiddleware "https://github.com/atakancelik/lockedswagger/blob/master/LockedSwagger/SwaggerBasicAuthMiddleware.cs"
    click AppSettings "https://github.com/atakancelik/lockedswagger/blob/master/LockedSwagger/appsettings.json"
    click AppSettingsDev "https://github.com/atakancelik/lockedswagger/blob/master/LockedSwagger/appsettings.Development.json"
    click SwashGen "https://github.com/atakancelik/lockedswagger/blob/master/LockedSwagger/Program.cs"
    click Controller "https://github.com/atakancelik/lockedswagger/blob/master/LockedSwagger/Controllers/WeatherForecastController.cs"
    click Model "https://github.com/atakancelik/lockedswagger/blob/master/LockedSwagger/WeatherForecast.cs"
    click ProjectFile "https://github.com/atakancelik/lockedswagger/blob/master/LockedSwagger/LockedSwagger.csproj"
    click LaunchSettings "https://github.com/atakancelik/lockedswagger/blob/master/LockedSwagger/Properties/launchSettings.json"
    click HTTPReq "https://github.com/atakancelik/lockedswagger/blob/master/LockedSwagger/LockedSwagger.http"
    click Solution "https://github.com/atakancelik/lockedswagger/blob/master/LockedSwagger.sln"
    click Readme "https://github.com/atakancelik/lockedswagger/blob/master/README.md"

    classDef host fill:#cce5ff,stroke:#333,stroke-width:1px
    classDef middleware fill:#d4edda,stroke:#333,stroke-width:1px
    classDef config fill:#fff3cd,stroke:#333,stroke-width:1px
    classDef swagger fill:#ffe5b4,stroke:#333,stroke-width:1px
    classDef controller fill:#e2e3e5,stroke:#333,stroke-width:1px
    classDef project fill:#f0e68c,stroke:#333,stroke-width:1px
    classDef configFile fill:#fff8dc,stroke:#333,stroke-width:1px
    classDef other fill:#f5f5f5,stroke:#333,stroke-width:1px
    classDef client fill:#badcff,stroke:#333,stroke-width:1px
```
