# ltt204/identity-service architecture

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

- Interactive diagram: https://gitdiagram.com/ltt204/identity-service
- Repository: https://github.com/ltt204/identity-service

## Overview

##### Project Type & Purpose
- The project is a microservice built using Clean Architecture principles. It serves as an identity management service handling authentication (JWT 2.0 with refresh token rotation), authorization, user management, and role-based access control.
- It is implemented using Java 17, Spring Boot, Spring Security, Spring Data JPA, along with MySQL and Redis. The README emphasizes robust security practices and the separation of concerns with clearly defined layers.

##### Key Components and Layers
- Domain Layer (Core):
- This layer hosts the enterprise business rules without dependency on outer layers.
- Contains core entities (User, Role, Permission, InvalidatedToken) within the "domain/entities" directory.
- Contains interfaces for repository access and core service definitions ("domain/repositories" and "domain/services").
- Application Layer:
- Implements application-specific business logic.
- Contains Data Transfer Objects (DTOs), service implementations (like AuthService, UserService, etc.), and adapters (repository adapters) that implement domain service requirements.
- Found under "src/main/java/org/ltt204/identityservice/application/".
- Infrastructure Layer:
- Provides implementations for persistence and external integrations.
- Contains JPA entities, repository implementations, mappers for converting between domain models and persistence models, and configuration setups (e.g., security, Redis, and application initialization).
- Located in "src/main/java/org/ltt204/identityservice/infra/".
- Presentation Layer:
- Exposes REST API endpoints such as AuthenticationController, UserController, RoleController, and PermissionController.
- Contains request and response DTOs for client communications, mappers to translate between internal models and external representations, and global exception handling.
- Found in "src/main/java/org/ltt204/identityservice/presentations/".

##### External Services & Dependencies
- MySQL Database:
- Acts as the data store. The schema is initialized via SQL scripts in "src/main/resources/db/migration".
- Redis:
- Used for token blacklisting (ensuring refresh token rotation) and caching aspects.
- JWT Authentication:
- Deployed via Spring Security configurations in the infrastructure layer, including custom JWT decoders and error handling mechanisms.
- Docker & Docker Compose:
- Simplify deployment for the database components and Redis. The presence of docker-compose.yml is an indicator of containerized deployment.

##### Relationship & Data Flow
- The controller receives the HTTP request, and using request DTOs and mappers, it forwards the request to the Application layer services.
- The Application layer orchestrates business operations and interacts with Domain layer interfaces which capture the core business rules.
- For persistence or external communications, adapters in the Application layer delegate operations to the Infrastructure layer (e.g., JPA repositories, Redis operations).
- Data then flows back from the Infrastructure through to the Application, which constructs response DTOs that are returned by the Presentation layer.

##### Architectural Patterns and Design Principles
- Clean Architecture:
- Separate concerns into distinct layers (Domain, Application, Infrastructure, Presentation). Emphasize minimal dependency from inner to outer layers.
- DTOs & Mappers:
- Use these design patterns to translate between domain models and external representations.
- Adapter Pattern:
- Repository adapters in the Application layer implement domain contract interfaces.
- Security Best Practices:
- JWT-based authentication, refresh token rotation, token blacklisting using Redis, and role-based authorization enforce security boundaries.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Client
    CLIENT["Client"]:::client

    %% Presentation Layer
    subgraph "Presentation Layer"
        WC["Web Controllers"]:::presentation
        EXH["Exception Handling"]:::presentation
        DRDTO["Request/Response DTOs"]:::presentation
        EXT_MAP["Mappers (External)"]:::presentation
    end

    %% Application Layer
    subgraph "Application Layer"
        DTO["DTOs"]:::application
        SI["Service Implementations"]:::application
        RA["Repository Adapters"]:::application
        VAL["Validators"]:::application
    end

    %% Domain Layer
    subgraph "Domain Layer"
        DE["Domain Entities"]:::domain
        DR["Domain Repo Interfaces"]:::domain
        DS["Domain Service Interfaces"]:::domain
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        JE["JPA Entities"]:::infrastructure
        JR["JPA Repositories"]:::infrastructure
        MAP["Mappers (Domain-Persistence)"]:::infrastructure
        SEC["Security Components"]:::infrastructure
        INIT["App Init & Redis Config"]:::infrastructure
    end

    %% External Dependencies
    subgraph "External Dependencies"
        MYSQL["MySQL Database"]:::database
        REDIS["Redis Service"]:::database
        DOCKER["Docker Environment"]:::docker
    end

    %% Data Flow Connections
    CLIENT -->|"sendsRequests"| WC
    WC -->|"callsService"| SI
    WC -->|"formatsOutput"| EXT_MAP

    SI -->|"executesBusinessLogic"| DS
    SI -->|"delegatesToValidator"| VAL
    SI -->|"callsRepositoryAdapter"| RA

    RA -->|"invokesRepository"| JR
    RA -->|"transformsData"| MAP

    JR -->|"storesData"| MYSQL
    MAP -->|"mapsEntities"| JE

    DS -->|"references"| DE
    DS -->|"accesses"| DR

    WC -->|"authChecks"| SEC
    INIT -->|"configuresCaching"| REDIS

    DOCKER -->|"deploys"| MYSQL
    DOCKER -->|"deploys"| REDIS

    %% Style Definitions
    classDef client fill:#fff2cc,stroke:#d79b00,stroke-width:2px;
    classDef presentation fill:#ffebcc,stroke:#ff9900,stroke-width:2px;
    classDef application fill:#ccffcc,stroke:#009900,stroke-width:2px;
    classDef domain fill:#ffcccc,stroke:#cc0000,stroke-width:2px;
    classDef infrastructure fill:#ccccff,stroke:#0000cc,stroke-width:2px;
    classDef database fill:#e6e6e6,stroke:#666666,stroke-width:2px;
    classDef docker fill:#d9ead3,stroke:#6aa84f,stroke-width:2px;

    %% Click Events
    click DE "https://github.com/ltt204/identity-service/tree/main/src/main/java/org/ltt204/identityservice/domain/entities"
    click DR "https://github.com/ltt204/identity-service/tree/main/src/main/java/org/ltt204/identityservice/domain/repositories"
    click DS "https://github.com/ltt204/identity-service/tree/main/src/main/java/org/ltt204/identityservice/domain/services"
    click DTO "https://github.com/ltt204/identity-service/tree/main/src/main/java/org/ltt204/identityservice/application/dtos"
    click SI "https://github.com/ltt204/identity-service/tree/main/src/main/java/org/ltt204/identityservice/application/services"
    click RA "https://github.com/ltt204/identity-service/tree/main/src/main/java/org/ltt204/identityservice/application/repositories"
    click VAL "https://github.com/ltt204/identity-service/tree/main/src/main/java/org/ltt204/identityservice/application/validator"
    click JE "https://github.com/ltt204/identity-service/tree/main/src/main/java/org/ltt204/identityservice/infra/persist/jpa/entities"
    click JR "https://github.com/ltt204/identity-service/tree/main/src/main/java/org/ltt204/identityservice/infra/persist/jpa/repositories"
    click MAP "https://github.com/ltt204/identity-service/tree/main/src/main/java/org/ltt204/identityservice/infra/persist/mappers"
    click SEC "https://github.com/ltt204/identity-service/tree/main/src/main/java/org/ltt204/identityservice/infra/security"
    click INIT "https://github.com/ltt204/identity-service/tree/main/src/main/java/org/ltt204/identityservice/infra/config"
    click WC "https://github.com/ltt204/identity-service/tree/main/src/main/java/org/ltt204/identityservice/presentations/web/controllers"
    click EXH "https://github.com/ltt204/identity-service/tree/main/src/main/java/org/ltt204/identityservice/presentations/web/advices"
    click DRDTO "https://github.com/ltt204/identity-service/tree/main/src/main/java/org/ltt204/identityservice/presentations/web/dtos"
    click EXT_MAP "https://github.com/ltt204/identity-service/tree/main/src/main/java/org/ltt204/identityservice/presentations/web/mappers"
    click MYSQL "https://github.com/ltt204/identity-service/tree/main/src/main/resources/db/migration"
    click DOCKER "https://github.com/ltt204/identity-service/blob/main/docker-compose.yml"
```
