/jwt-token-refresh-mechanism

A secure, scalable JWT Token Refresh Mechanism built with .NET 9, Domain-Driven Design (DDD), and Clean Architecture. Implements refresh tokens, secure authentication, and token revocation to enhance API security and user management.

Primary LanguageC#

๐Ÿ” JWT Token Refresh Mechanism โ€“ Secure API Authentication ๐Ÿš€

.NET 9 JWT Clean Architecture Domain-Driven Design Security

A secure and scalable JWT Token Refresh Mechanism built with .NET 9, Domain-Driven Design (DDD), and Clean Architecture. Implements refresh tokens, token revocation, and multi-layered authentication to enhance security and user management.


๐Ÿ”ฅ Features

โœ… User Registration & Login โ€“ Secure user authentication with password hashing.
โœ… JWT Access Tokens โ€“ Short-lived access tokens for secure API access.
โœ… Refresh Tokens โ€“ Long-lived refresh tokens for session continuation.
โœ… Token Revocation โ€“ Prevent unauthorized access by revoking refresh tokens.
โœ… DDD & Clean Architecture โ€“ Ensures maintainability and scalability.
โœ… Unit Testing โ€“ Comprehensive tests for security and authentication workflows.


๐Ÿ›  Technologies Used

๐ŸŸฃ .NET 9 โ€“ Backend framework for secure API development.
๐Ÿ”ด JWT Authentication โ€“ Secure user authentication and authorization.
๐ŸŸข Entity Framework Core โ€“ Database interactions and migrations.
๐ŸŸก FluentValidation โ€“ Request validation to enforce strong security rules.
๐Ÿ”ต MediatR โ€“ Implements CQRS for command-query separation.
๐ŸŸ  xUnit & Moq โ€“ Unit testing and mocking dependencies for reliability.


๐Ÿ›๏ธ Architecture

This project follows Clean Architecture principles and DDD:

1๏ธโƒฃ Domain Layer
๐Ÿ“Œ Business logic, entities, and domain events (User, RefreshToken).

2๏ธโƒฃ Application Layer
๐Ÿ“Œ Use cases, commands, queries (LoginCommand, RefreshTokenCommand).

3๏ธโƒฃ Infrastructure Layer
๐Ÿ“Œ Database, repositories, JWT provider, external services (EF Core, TokenService).

4๏ธโƒฃ Presentation Layer
๐Ÿ“Œ API controllers, request/response models (UsersController, LoginRequest).


๐Ÿ”„ How It Works

1๏ธโƒฃ User Registration

๐Ÿ”น Users register with email and password.
๐Ÿ”น Passwords are hashed before being stored securely.

2๏ธโƒฃ Login & Token Issuance

๐Ÿ”น Users log in with credentials.
๐Ÿ”น A JWT access token (short-lived) and a refresh token (long-lived) are issued.
๐Ÿ”น The refresh token is stored securely in the database.

3๏ธโƒฃ Token Refresh Flow

๐Ÿ”น When the access token expires, the client sends a refresh token to /refresh-token.
๐Ÿ”น A new JWT access token is issued.
๐Ÿ”น The old refresh token is revoked, ensuring security.

4๏ธโƒฃ Token Revocation

๐Ÿ”น Users can revoke refresh tokens, logging out from all devices.


๐Ÿš€ Getting Started

Prerequisites

๐Ÿ“Œ .NET 9 SDK
๐Ÿ“Œ Visual Studio 2022 or VS Code
๐Ÿ“Œ Postman for API testing

Step 1: Clone the Repository

git clone https://github.com/MrEshboboyev/jwt-token-refresh-mechanism.git
cd jwt-token-refresh-mechanism

Step 2: Install Dependencies

dotnet restore

Step 3: Run the Application

dotnet run --project src/Presentation

๐Ÿ”— API Endpoints

Method Endpoint Description
POST /api/users/register Registers a new user
POST /api/users/login Logs in and returns access & refresh tokens
POST /api/users/refresh-token Refreshes JWT access token
POST /api/users/revoke-token Revokes a refresh token

๐Ÿงช Testing

Unit Tests

Run all unit tests:

dotnet test

Manual API Testing

1๏ธโƒฃ Register a new user
2๏ธโƒฃ Log in and get access & refresh tokens
3๏ธโƒฃ Use refresh token to obtain a new access token
4๏ธโƒฃ Revoke refresh token to log out

โœ… Test using Postman, Swagger, or any REST client.


๐ŸŽฏ Why Use This Project?

โœ… High Security โ€“ Implements refresh token rotation & revocation.
โœ… Scalable & Maintainable โ€“ Follows DDD & Clean Architecture.
โœ… Ready for Production โ€“ Follows industry best practices.
โœ… Cloud-Ready โ€“ Can be deployed on AWS, Azure, Kubernetes.


๐Ÿ“„ License

This project is licensed under the MIT License. See the LICENSE file for details.


๐Ÿ“ž Contact

For feedback, questions, or contributions:
๐Ÿ“ง Email: mreshboboyev@gmail.com
๐Ÿ’ป GitHub: MrEshboboyev


๐Ÿš€ Secure your API with a scalable JWT Token Refresh Mechanism! Clone the repo and start coding today!