# ms-shahid/user-microservice architecture

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

- Interactive diagram: https://gitdiagram.com/ms-shahid/user-microservice
- Repository: https://github.com/ms-shahid/user-microservice

## Overview

##### Main Components
- User Microservice: This is the core component that exposes endpoints for signup, login, token validation, and user detail retrieval.
- API Layer (Controller): Represented by the UserController which exposes REST endpoints.
- Service Layer: Bound by the UserService class which contains the business logic for login, sign-up, and token validation.
- Data Access Layer: Illustrated by the repositories (UserRepository, TokenRepo, etc.) which interact with the database to persist and retrieve user, token, and possibly role information.
- Domain Models/DTOs: Include the models (User, Token, Role, BaseModel) and DTOs (SignUpRequestDto, UserResponseDto, LoginRequestDto, LogoutRequestDto) that serve as data contracts between different layers.
- Security and Configuration: Highlight configurations for BCryptPasswordEncoder (EncoderConfig), the Spring Security settings (SecurityConfig), and OAuth 2.0 aspects such as the RegisteredClientRepository.
- External Integration: Emphasize the integration point with ProductService where the token generated by User Microservice is validated and used for further processing.
- OAuth 2.0 Components: Illustrate the OAuth components (Authorization Server, Resource Server) that are intertwined with this microservice for token validation and to secure endpoints.

##### Relationships and Interactions
– API requests flow from clients (or other services) into the UserController.
– The UserController routes calls to the UserService, which then uses the repository layer for database operations.
– The security configuration (SecurityConfig & EncoderConfig) ensures that requests are properly authenticated. A custom UserDetailsService (CustomUserServiceDetails) exemplifies how user authentication is integrated into the service.
– For token validation, the UserService can be invoked via REST calls (e.g., by the ProductService) to validate tokens and return limited user info based on request DTOs.
– The OAuth 2.0 flow is seen where this service both protects APIs (by validating tokens) and acts as a client (via the RegisteredClientRepository configuration for OAuth communication).

##### Architectural Patterns and Design Principles
– Layered Architecture: Clear separation between Controller, Service, Repository, and Configuration layers enhances modularity and maintainability.
– DTO Pattern: By using dedicated DTOs for request and response, the system ensures controlled data exposure between microservices.
– Dependency Injection and Configuration Management: Spring Boot’s configuration practices are used (e.g., Beans for BCrypt and security filters).
– Security Best Practices: Incorporation of Spring Security and OAuth 2.0 ensures robust authentication/authorization mechanisms.
– Microservices Communication: The User Microservice exposes token validation as an external API for integration with other services (like ProductService).

##### Technologies and Tools
– Spring Boot framework and Spring Security (including OAuth 2.0) for authorization.
– Maven (as indicated by the pom.xml) for project builds.
– RESTful API design using controllers annotated with Spring MVC annotations.
– JPA/Hibernate for ORM (evident from the repository and model packages).
– Swagger for API documentation.
– Bcrypt for password encryption.

##### Components
- From the client to UserController, then on to UserService.
- From UserService to the Repository layer.
- From the ProductService making REST calls to the /validate endpoint in the User Microservice.
- Within security components, a flow indicating how requests are processed and intercepted.
– Detailed annotations for OAuth 2.0 participants to illustrate the interplay between user authentication and authorization.

– Start by drawing an overall system architecture with external clients and microservices at the edges.
– Break down the User Microservice into its internal layers (Controller, Service, Repository, Models/DTOs, Security/Configuration).
– Include notes or labels on important configuration beans (like the BCryptPasswordEncoder and OAuth client definitions) as well as integration points (the REST calls to validate tokens).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Clients
    subgraph "External Clients"
        client["User (Client)"]:::external
        product["ProductService (Integration)"]:::external
    end

    %% User Microservice
    subgraph "User Microservice"
        mainApp["Main Application"]:::application
        subgraph "API Layer"
            controller["UserController"]:::controller
        end
        subgraph "Service Layer"
            service["UserService"]:::service
            customUser["CustomUserServiceDetails"]:::service
        end
        subgraph "Data Access Layer"
            userRepo["UserRepository"]:::repository
            tokenRepo["TokenRepo"]:::repository
        end
        subgraph "Domain Models & DTOs"
            models["Models (User,Token,Role,BaseModel)"]:::domain
            dtos["DTOs (SignUpRequestDto,UserResponseDto,LoginRequestDto,LogoutRequestDto)"]:::domain
        end
        subgraph "Security & Configuration"
            secConfig["SecurityConfig"]:::config
            encoder["EncoderConfig"]:::config
            oauthRepo["RegisteredClientRepository"]:::config
        end
    end

    %% Interactions between External Clients and User Microservice
    client -->|"request: signup/login"| controller
    product -->|"REST call: validateToken"| controller

    %% Internal flow within User Microservice
    mainApp --> controller
    mainApp --> service
    mainApp --> secConfig

    controller -->|"invokes business logic"| service
    service -->|"performs DB operations"| userRepo
    service -->|"performs DB operations"| tokenRepo
    service -->|"uses data contracts"| models
    service -->|"uses data contracts"| dtos
    
    secConfig -->|"authenticates via"| customUser
    controller -->|"secured by"| secConfig
    secConfig -->|"configures BCrypt"| encoder

    %% Styles for nodes
    classDef external fill:#FFCCCB,stroke:#8B0000,stroke-width:2px;
    classDef application fill:#D3D3D3,stroke:#696969,stroke-width:2px;
    classDef controller fill:#ADD8E6,stroke:#00008B,stroke-width:2px;
    classDef service fill:#90EE90,stroke:#006400,stroke-width:2px;
    classDef repository fill:#FFA500,stroke:#FF8C00,stroke-width:2px;
    classDef domain fill:#FFFFE0,stroke:#FFD700,stroke-width:2px;
    classDef config fill:#E6E6FA,stroke:#4B0082,stroke-width:2px;

    %% Click Events
    click mainApp "https://github.com/ms-shahid/user-microservice/blob/master/src/main/java/com/userservice/UserserviceApplication.java"
    click controller "https://github.com/ms-shahid/user-microservice/blob/master/src/main/java/com/userservice/controller/UserController.java"
    click service "https://github.com/ms-shahid/user-microservice/blob/master/src/main/java/com/userservice/service/UserService.java"
    click customUser "https://github.com/ms-shahid/user-microservice/blob/master/src/main/java/com/userservice/security/services/CustomUserServiceDetails.java"
    click userRepo "https://github.com/ms-shahid/user-microservice/blob/master/src/main/java/com/userservice/repository/UserRepository.java"
    click tokenRepo "https://github.com/ms-shahid/user-microservice/blob/master/src/main/java/com/userservice/repository/TokenRepo.java"
    click models "https://github.com/ms-shahid/user-microservice/tree/master/src/main/java/com/userservice/models"
    click dtos "https://github.com/ms-shahid/user-microservice/tree/master/src/main/java/com/userservice/dtos"
    click secConfig "https://github.com/ms-shahid/user-microservice/blob/master/src/main/java/com/userservice/configuration/SecurityConfig.java"
    click encoder "https://github.com/ms-shahid/user-microservice/blob/master/src/main/java/com/userservice/configuration/EncoderConfig.java"
    click oauthRepo "https://github.com/ms-shahid/user-microservice/blob/master/src/main/java/com/userservice/security/repository/JpaRegisteredClientRepository.java"
```
