# prebor97/auth-service architecture

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

- Interactive diagram: https://gitdiagram.com/prebor97/auth-service
- Repository: https://github.com/prebor97/auth-service

## Overview

##### Project Type and Purpose
- This is a microservice specifically focused on authentication/authorization. The structure follows a typical Spring Boot application design. The “auth-service” handles user registration, login, role management, and likely token-based authentication (as suggested by the JwtUtil in the util package).
- With controllers, services, repositories, DTOs, and exception handling spread across the package structure, the primary purpose is to encapsulate the authentication logic and provide a RESTful API for user-related operations.

##### File Structure
- Top-level directories:
- "src/main/java": Contains the main application code.
- "src/main/resources": Contains configuration files (like application.properties).
- "src/test": Contains test classes.
- Within "com/prebs/auth_service", you have several packages:
- config: Defines configuration classes (e.g., SecurityConfig, OpenAiConfig) that set up security and possibly external API integrations.
- controller: Exposes API endpoints via AdminController and AuthController.
- dto: Contains request and response objects for data transfer, ensuring decoupling between API and internal model.
- enums: Enumerates user roles used across the application.
- exception: Custom exceptions and a global exception handler for centralized error management.
- model: Contains domain models like the User class.
- repository: Interfaces with the data store for user persistence.
- service: Implements business logic (e.g., handling registration, login) in UserService.
- util: Utility/helper classes like JwtUtil and MapUtil.
- The Maven configuration (pom.xml, mvnw, etc.) suggests standard build and dependency management, while testing is performed using JUnit (based on test structure).

##### Key Components to Feature
a. Main Components:
- Entry Point: AuthServiceApplication (the Spring Boot main method).
- Controllers: AuthController and AdminController that expose REST endpoints.
- Service Layer: UserService, performing business logic and coordinating between controllers and repositories.
- Data Access Layer: UserRepository, interfacing with the database.
- Model: User (the central domain entity).
- Configuration Layer: SecurityConfig and OpenAiConfig that set up application-specific configurations (security and integration with external services like OpenAI, if applicable).
- Utility Components: JwtUtil for token management and MapUtil for helper functionalities.
- Exception Handling: GlobalExceptionHandler and custom exceptions (e.g., EmailPasswordException, PasswordMismatchException) to manage error flow.

b. Relationships and Interactions:
- The UI or client sends HTTP requests to the controllers.
- Controllers call the service layer to process authentication logic.
- The service layer performs operations, sometimes utilizing utility components (like JwtUtil) and accessing the repository for database interactions.
- Any configuration or security aspects are applied through the configuration classes.
- Exception handling is done centrally through GlobalExceptionHandler.

c. Architectural Patterns and Design Principles:
- Follows a layered architecture (controller, service, repository) typical in Spring Boot applications.
- Clear separation of concerns with DTOs used to decouple external API contracts from internal models.
- Centralized exception management to handle various error cases.
- Emphasis on security and token management via the SecurityConfig and JwtUtil.

d. Relevant Technologies/Frameworks:
- Java and Spring Boot as the primary framework.
- Maven for dependency and build management.
- Likely a relational database (inferred from the repository pattern, though specifics aren’t detailed here).
- Possibly integration with external services (OpenAiConfig hints at connectivity with external APIs).

##### Details
- From the client to controllers (incoming HTTP requests).
- From controllers to services (business logic handling).
- From services to repositories (database operations).
- Backflows such as exception handling using the GlobalExceptionHandler.
- Rectangles for application layers (controllers, services, repositories).
- Configuration layer (SecurityConfig, OpenAiConfig) as these are critical to the authentication process.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client
    Client("Client/API Consumer"):::client

    %% Entry Point
    App("AuthServiceApplication"):::entrypoint

    %% Controller Layer
    subgraph "Controller Layer"
        AuthCtrl("AuthController"):::controller
        AdminCtrl("AdminController"):::controller
    end

    %% Service Layer
    subgraph "Service Layer"
        UserSvc("UserService"):::service
    end

    %% Data Access Layer
    subgraph "Data Access Layer"
        UserRepo("UserRepository"):::repository
    end

    %% Domain Model
    UserModel("User (Model)"):::model

    %% Configuration Layer
    subgraph "Configuration Layer"
        SecConfig("SecurityConfig"):::config
        OpenAIConfig("OpenAiConfig"):::config
    end

    %% Utility Components
    subgraph "Utility Components"
        JwtUtil("JwtUtil"):::util
        MapUtil("MapUtil"):::util
    end

    %% Data Transfer Objects
    subgraph "Data Transfer Objects"
        DTOReq("DTO: Request (Registration,Login,Role)"):::dto
        DTORes("DTO: Response"):::dto
    end

    %% Exception Handling
    subgraph "Exception Handling"
        GlobalEx("GlobalExceptionHandler"):::exception
        CustEx("Custom Exceptions"):::exception
    end

    %% Arrows/Interactions
    Client -->|"HTTPRequest"| App
    App -->|"Initializes"| AuthCtrl
    App -->|"Initializes"| AdminCtrl
    AuthCtrl -->|"Calls"| UserSvc
    AdminCtrl -->|"Calls"| UserSvc
    UserSvc -->|"Accesses"| UserRepo
    UserSvc -->|"Utilizes"| JwtUtil
    UserSvc -->|"Utilizes"| MapUtil
    UserSvc -->|"OperatesOn"| UserModel
    AuthCtrl -->|"Consumes"| DTOReq
    AuthCtrl -->|"RespondsWith"| DTORes
    AdminCtrl -->|"Consumes"| DTOReq
    AdminCtrl -->|"RespondsWith"| DTORes
    AuthCtrl -->|"ErrorHandledBy"| GlobalEx
    AdminCtrl -->|"ErrorHandledBy"| GlobalEx
    UserSvc -->|"ErrorHandledBy"| GlobalEx
    CustEx --- GlobalEx
    App -->|"Configures"| SecConfig
    App -->|"Configures"| OpenAIConfig

    %% Click Events
    click App "https://github.com/prebor97/auth-service/blob/main/src/main/java/com/prebs/auth_service/AuthServiceApplication.java"
    click AuthCtrl "https://github.com/prebor97/auth-service/blob/main/src/main/java/com/prebs/auth_service/controller/AuthController.java"
    click AdminCtrl "https://github.com/prebor97/auth-service/blob/main/src/main/java/com/prebs/auth_service/controller/AdminController.java"
    click UserSvc "https://github.com/prebor97/auth-service/blob/main/src/main/java/com/prebs/auth_service/service/UserService.java"
    click UserRepo "https://github.com/prebor97/auth-service/blob/main/src/main/java/com/prebs/auth_service/repository/UserRepository.java"
    click UserModel "https://github.com/prebor97/auth-service/blob/main/src/main/java/com/prebs/auth_service/model/User.java"
    click SecConfig "https://github.com/prebor97/auth-service/blob/main/src/main/java/com/prebs/auth_service/config/SecurityConfig.java"
    click OpenAIConfig "https://github.com/prebor97/auth-service/blob/main/src/main/java/com/prebs/auth_service/config/OpenAiConfig.java"
    click JwtUtil "https://github.com/prebor97/auth-service/blob/main/src/main/java/com/prebs/auth_service/util/JwtUtil.java"
    click MapUtil "https://github.com/prebor97/auth-service/blob/main/src/main/java/com/prebs/auth_service/util/MapUtil.java"
    click GlobalEx "https://github.com/prebor97/auth-service/blob/main/src/main/java/com/prebs/auth_service/exception/GlobalExceptionHandler.java"
    click DTOReq "https://github.com/prebor97/auth-service/tree/main/src/main/java/com/prebs/auth_service/dto/request/"
    click DTORes "https://github.com/prebor97/auth-service/tree/main/src/main/java/com/prebs/auth_service/dto/response/"
    click CustEx "https://github.com/prebor97/auth-service/tree/main/src/main/java/com/prebs/auth_service/exception/"

    %% Styling
    classDef client fill:#ffd700,stroke:#333,stroke-width:2px;
    classDef entrypoint fill:#ff6347,stroke:#333,stroke-width:2px;
    classDef controller fill:#87ceeb,stroke:#333,stroke-width:2px;
    classDef service fill:#98fb98,stroke:#333,stroke-width:2px;
    classDef repository fill:#dda0dd,stroke:#333,stroke-width:2px;
    classDef model fill:#f4a460,stroke:#333,stroke-width:2px;
    classDef config fill:#f08080,stroke:#333,stroke-width:2px;
    classDef util fill:#afeeee,stroke:#333,stroke-width:2px;
    classDef dto fill:#d3d3d3,stroke:#333,stroke-width:2px;
    classDef exception fill:#ffb6c1,stroke:#333,stroke-width:2px;
```
