# stratoff/dynamic-permissions architecture

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

- Interactive diagram: https://gitdiagram.com/stratoff/dynamic-permissions
- Repository: https://github.com/stratoff/dynamic-permissions

## Overview

##### Project Type and Purpose
- The README indicates that this middleware adds dynamic permissions for KrakenD.
- Its purpose is to integrate with KrakenD by intercepting API requests, checking JWT payloads, and validating permissions dynamically.
- This isn’t a full-stack application but a standalone middleware component with examples to demonstrate its usage.

##### File Structure
- Core Files: Files like config.go, jwt.go, permissions.go, and gin.go indicate separation of concerns (configuration setup, JWT handling, permissions logic, and possibly integration with the Gin framework).
- Example Directory: Contains krakend.json and a sample main.go that illustrate how to integrate/configure the middleware with KrakenD.
- Test Files: jwt_test.go and permissions_test.go hint that these functionalities are modular and have corresponding unit tests.
- Go Modules: go.mod and go.sum show dependency management.

##### Main Components
a. Middleware Integration Component:
- This represents how the middleware sits in the KrakenD pipeline.
b. Configuration Component (config.go):
- Handles reading and managing configuration parameters.
c. JWT Handling Component (jwt.go):
- Parses and validates the JWT payload.
d. Permissions Evaluation Component (permissions.go):
- Implements the dynamic permissions logic based on the JWT contents.
- Role in deciding if access should be granted.
e. Optional Integration Module (gin.go):
- If the middleware supports integration with the Gin framework, show it as an adapter for this specific web framework.
f. Example Usage:
- Example folder showing a sample configuration (krakend.json) and a sample application runner (main.go) that demonstrates the middleware configuration and invocation.

##### Relationships and Interactions
- KrakenD routes the request through the middleware component.
- The middleware consults the configuration module to determine settings.
- The request’s JWT is passed to the JWT handling module to extract necessary information.
- The permissions module uses data from the JWT to evaluate dynamic permissions and issues an allow/deny decision.
- If applicable, show an alternative path where the Gin integration adapter manages similar functionality for Gin-based servers.

##### Architectural Patterns and Design Principles
- Separation of concerns by isolating configuration, authentication (JWT), and authorization (permissions).
- Use modular design principles: each Go file represents a discrete module with well-defined responsibilities.
- Extensibility through “Next Steps” noted in the README (e.g., flexible parsing, caching enhancements).
- That unit tests exist for JWT and permission functionalities, which can also be mentioned as “tested modules” in your design.

## Diagram (Mermaid)

```mermaid
flowchart TD
    R("Incoming API Request"):::request
    KG("KrakenD API Gateway"):::gateway
    KM("KrakenD Middleware"):::middleware
    CM("Config Manager"):::config
    JWT("JWT Parser"):::jwt
    PE("Permissions Evaluator"):::permissions
    AD("Access Decision"):::decision
    GI("Gin Integration Adapter"):::gin

    subgraph "Example Usage"
        E1("krakend.json"):::example
        E2("main.go"):::example
    end

    subgraph "Tests"
        JT("jwt_test.go"):::jwt
        PT("permissions_test.go"):::permissions
    end

    R --> KG
    KG --> KM
    KM --> CM
    KM --> JWT
    JWT --> PE
    PE --> AD
    R --- GI
    GI --> KM
    KM --> E1
    KM --> E2
    JWT --> JT
    PE --> PT

    click CM "https://github.com/stratoff/dynamic-permissions/blob/master/config.go"
    click JWT "https://github.com/stratoff/dynamic-permissions/blob/master/jwt.go"
    click PE "https://github.com/stratoff/dynamic-permissions/blob/master/permissions.go"
    click GI "https://github.com/stratoff/dynamic-permissions/blob/master/gin.go"
    click E1 "https://github.com/stratoff/dynamic-permissions/blob/master/example/krakend.json"
    click E2 "https://github.com/stratoff/dynamic-permissions/blob/master/example/main.go"
    click JT "https://github.com/stratoff/dynamic-permissions/blob/master/jwt_test.go"
    click PT "https://github.com/stratoff/dynamic-permissions/blob/master/permissions_test.go"

    classDef request fill:#FFF9C4,stroke:#000000,stroke-width:2px;
    classDef gateway fill:#B3E5FC,stroke:#000000,stroke-width:2px;
    classDef middleware fill:#C0C0C0,stroke:#000000,stroke-width:2px;
    classDef config fill:#A8E6CF,stroke:#000000,stroke-width:2px;
    classDef jwt fill:#FFD3B6,stroke:#000000,stroke-width:2px;
    classDef permissions fill:#FFAAA5,stroke:#000000,stroke-width:2px;
    classDef decision fill:#CF94DA,stroke:#000000,stroke-width:2px;
    classDef gin fill:#D5E1DF,stroke:#000000,stroke-width:2px;
    classDef example fill:#E1BEE7,stroke:#000000,stroke-width:2px;
```
