|
1 | | -# authentication |
| 1 | +# Authentication Microservice |
| 2 | + |
| 3 | +## Overview |
| 4 | +The Authentication Microservice is a core component of the Podzilla system, designed to handle user authentication and authorization securely. Built as a standalone microservice, it provides robust mechanisms for user management, secure access control, and session handling, ensuring seamless integration with other services in the Podzilla ecosystem. |
| 5 | + |
| 6 | +## Badges |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +## Features |
| 19 | +- **Login**: Secure user authentication using email/username and password with JWT-based access tokens. |
| 20 | +- **Logout**: Invalidate user sessions and refresh tokens. |
| 21 | +- **Register**: Create new user accounts with email verification. |
| 22 | +- **Update User Data**: Allow users to update their profile information securely. |
| 23 | +- **Activate/Deactivate Users**: Admin functionality to manage user account statuses. |
| 24 | +- **Refresh Access Token**: Generate new access tokens using refresh tokens for seamless user experience. |
| 25 | +- **Role-Based Access Control**: Support for different user roles (e.g., admin, user) with fine-grained permissions. |
| 26 | + |
| 27 | +## Code Style |
| 28 | +The project adheres to Java conventions and best practices for maintainability and readability: |
| 29 | +- **Naming Conventions**: |
| 30 | + - Classes: PascalCase (e.g., `UserService`, `AuthController`) |
| 31 | + - Methods and Variables: camelCase (e.g., `authenticateUser`, `userId`) |
| 32 | + - Constants: UPPER_SNAKE_CASE (e.g., `MAX_LOGIN_ATTEMPTS`) |
| 33 | + - Packages: Lowercase with meaningful hierarchy (e.g., `com.podzilla.auth.service`) |
| 34 | +- **Best Practices**: |
| 35 | + - Use meaningful names that reflect purpose (e.g., `generateJwtToken` instead of `genToken`). |
| 36 | + - Follow single-responsibility principle for methods and classes. |
| 37 | + - Include JavaDoc for public methods and classes. |
| 38 | + - Enforce code quality with Checkstyle to ensure consistent formatting and adherence to standards. |
| 39 | + - Avoid magic numbers/strings; use constants instead. |
| 40 | + - Handle exceptions gracefully with proper logging. |
| 41 | + |
| 42 | +## Architecture |
| 43 | +The Authentication Microservice follows a **microservice architecture** with **Domain-Driven Design (DDD)** and **Clean Architecture** principles. The codebase is organized into distinct layers to ensure separation of concerns: |
| 44 | +- **Models**: Represent domain entities (e.g., `User`, `Role`). |
| 45 | +- **Repositories**: Handle data persistence using JDBC for MySQL and Redis for caching. |
| 46 | +- **Services**: Contain business logic (e.g., `AuthService`, `UserService`). |
| 47 | +- **Controllers**: Expose RESTful endpoints for client interaction (e.g., `/api/auth/login`). |
| 48 | +- **DTOs (Data Transfer Objects)**: Used for request/response payloads to decouple API contracts from domain models. |
| 49 | + |
| 50 | +The microservice communicates with other Podzilla services via REST APIs and uses Redis for fast, in-memory token storage. Spring Security is leveraged for authentication and authorization, ensuring secure access control. |
| 51 | + |
| 52 | +## Installation and Running |
| 53 | + |
| 54 | +### Prerequisites |
| 55 | +- Java 17 |
| 56 | +- Maven 3.8.6+ |
| 57 | +- Docker 24.0+ |
| 58 | +- MySQL 8.0+ |
| 59 | +- Redis 7.2+ |
| 60 | +- Git |
| 61 | + |
| 62 | +### Setup |
| 63 | +1. **Clone the Repository**: |
| 64 | + ```bash |
| 65 | + git clone https://github.com/Podzilla/authentication.git |
| 66 | + cd authentication |
| 67 | + ``` |
| 68 | + |
| 69 | +2. **Configure Environment**: |
| 70 | + - Create a `.env` file based on `.env.example` and update with your MySQL and Redis credentials: |
| 71 | + ```env |
| 72 | + SPRING_DATASOURCE_URL=jdbc:mysql://localhost:3306/auth_db |
| 73 | + SPRING_DATASOURCE_USERNAME=root |
| 74 | + SPRING_DATASOURCE_PASSWORD=your_password |
| 75 | + SPRING_REDIS_HOST=localhost |
| 76 | + SPRING_REDIS_PORT=6379 |
| 77 | + ``` |
| 78 | +
|
| 79 | +3. **Build the Project**: |
| 80 | + ```bash |
| 81 | + mvn clean install |
| 82 | + ``` |
| 83 | + |
| 84 | +4. **Run the Application**: |
| 85 | + ```bash |
| 86 | + mvn spring-boot:run |
| 87 | + ``` |
| 88 | + The service will be available at `http://localhost:8080`. |
| 89 | + |
| 90 | +5. **Run with Docker**: |
| 91 | + ```bash |
| 92 | + docker build -t podzilla-authentication . |
| 93 | + docker run -p 8080:8080 --env-file .env podzilla-authentication |
| 94 | + ``` |
| 95 | + |
| 96 | +6. **Access Swagger UI**: |
| 97 | + - Navigate to `http://localhost:8080/swagger-ui.html` to explore and test API endpoints. |
| 98 | + |
| 99 | +7. **Run Tests**: |
| 100 | + ```bash |
| 101 | + mvn test |
| 102 | + ``` |
| 103 | + - Unit and integration tests are written using JUnit 5. |
| 104 | + - Code coverage reports are generated and available in `target/site/jacoco`. |
| 105 | + |
| 106 | +## How to Contribute |
| 107 | +We welcome contributions to enhance the Authentication Microservice! To contribute: |
| 108 | +1. Fork the repository. |
| 109 | +2. Create a new branch (`git checkout -b feature/your-feature`). |
| 110 | +3. Make your changes and ensure tests pass (`mvn test`). |
| 111 | +4. Run Checkstyle to verify code quality: |
| 112 | + ```bash |
| 113 | + mvn checkstyle:check |
| 114 | + ``` |
| 115 | +5. Commit your changes with a clear message (`git commit -m "Add your feature"`). |
| 116 | +6. Push to your branch (`git push origin feature/your-feature`). |
| 117 | +7. Open a pull request with a detailed description of your changes. |
| 118 | +8. Ensure your PR passes CI/CD checks. |
| 119 | + |
| 120 | +Please follow the code style guidelines and include tests for new features or bugfixes. |
0 commit comments