An open API service indexing awesome lists of open source software.

https://github.com/gregorykogan/jwt-microservice

A high-performance JWT authentication microservice written in Go, featuring Redis caching and Docker support.
https://github.com/gregorykogan/jwt-microservice

authentication auto-logout caching docker docker-compose go high-performance jwt load-balancing microservice nginx redis security structured-logging testing token-refresh uuid

Last synced: 3 months ago
JSON representation

A high-performance JWT authentication microservice written in Go, featuring Redis caching and Docker support.

Awesome Lists containing this project

README

          

# JWT Microservice ย  ![License MIT](https://img.shields.io/badge/license-MIT-green) ![Docker Ready](https://img.shields.io/badge/docker-ready-blue)

A high-performance **JWT authentication microservice** written in Go, featuring Redis caching and Docker support.

## ๐Ÿš€ Features

- ๐Ÿ” **JWT-based authentication**
- โšก **High-performance Redis caching**
- โ™ป๏ธ **Token refresh mechanism**
- ๐Ÿ”’ **Auto-logout functionality**
- ๐Ÿ“ **Structured logging**
- ๐Ÿณ **Docker support**
- ๐ŸŒ **Fast and lightweight**
- ๐Ÿงช **Comprehensive test coverage**

## ๐Ÿ› ๏ธ Architecture

```mermaid
graph LR
Client --> NGINX[๐Ÿ”€ NGINX]
subgraph Docker Network
NGINX --> DNS[๐Ÿ“ก Docker DNS]
DNS --> JWT1[๐ŸŸข JWT Service 1]
DNS --> JWT2[๐ŸŸข JWT Service 2]
DNS --> JWTn[๐ŸŸข JWT Service n]
end
subgraph Shared State
direction LR
JWT1 --> Redis[(๐Ÿ’พ Redis)]
JWT2 --> Redis
JWTn --> Redis
end
```

## ๐Ÿ“š API Endpoints

| Endpoint | Method | Description | Auth Required |
| --------------- | ------ | ------------------------ | ------------- |
| `/ping` | GET | Health check endpoint | โŒ No |
| `/login` | POST | Login and get token pair | โŒ No |
| `/refresh` | POST | Refresh token pair | โœ… Yes |
| `/logout` | POST | Invalidate token pair | โœ… Yes |
| `/authenticate` | GET | Validate access token | โœ… Yes |

## ๐Ÿš€ Quick Start

### ๐Ÿ“‹ Prerequisites

- ๐Ÿณ **Docker**
- ๐Ÿ› ๏ธ **Docker Compose**

### โš–๏ธ Scaling

The service supports horizontal scaling through Docker's built-in DNS-based load balancing:

```bash
docker compose up --build --scale jwt=5
```

This command will:

- ๐Ÿƒโ€โ™‚๏ธ Start 5 instances of the JWT service
- ๐ŸŒ Register them with Docker's DNS service
- ๐Ÿ”„ Enable automatic load balancing through Docker's embedded DNS server
- ๐Ÿ’พ Maintain shared state through Redis

NGINX acts as a reverse proxy, forwarding requests to the Docker DNS service, which routes them to the appropriate JWT service instance.

### ๐ŸŽฏ Running Locally

1. **Clone the repository**

```bash
git clone https://github.com/GregoryKogan/jwt-microservice.git
cd jwt-microservice
```

2. **Choose a startup option:**

#### ๐Ÿš€ Single Instance

```bash
docker compose up --build
```

#### โš–๏ธ Multiple Instances (Scaled)

```bash
# Start 5 instances with load balancing
docker compose up --build --scale jwt=5
```

#### ๐Ÿ› ๏ธ Development Mode

```bash
# Live-reloading for single instance
docker compose up --build --watch

# Live-reloading with multiple instances
docker compose up --build --watch --scale jwt=3
```

The service will be available at **`http://localhost:4000`**.

### โš™๏ธ Configuration

The service is configured via `config.yml`:

```yaml
server:
port: 8080
max_processors: 2 # sets GOMAXPROCS

logging:
mode: text # text or json
level: debug

cache:
host: cache
port: 6379

auth:
issuer: jwt-microservice
access_lifetime: 15m
refresh_lifetime: 720h
auto_logout: 24h
```

Also, take a look at the `docker-compose.yml` file for more configuration options such as CPU resource limits and port mappings.

### โœ… Testing

Run all tests with cache mocking:

```bash
go test ./... -v
```

## ๐Ÿ“Š Load Testing

Load testing is performed using **Grafana K6**. To execute the load tests with the `load-test` profile, run:

```bash
docker compose --profile load-test up --build --scale jwt=5
```

The xk6 dashboard will be available at **`http://localhost:5665`** during load testing.

### ๐Ÿ“ˆ Load Testing Results

| Instances | CPU Limit per Instance | Max Processors | Avg Response Time | RPS | Error Rate |
| --------- | ---------------------- | -------------- | ----------------- | ---- | ---------- |
| 1 | 0.1 | 1 | 895.21ms | 974 | 72.51% |
| 2 | 0.1 | 1 | 274.59ms | 1097 | 53.54% |
| 4 | 0.2 | 2 | 8.07ms | 1163 | 0.39% |
| 8 | 0.25 | 2 | 6.7ms | 1154 | 0.21% |
| 16 | 0.5 | 4 | 15.84ms | 978 | 0.59% |
| 32 | - | - | 91.54ms | 1031 | 1.81% |

> **Note:** Charts and graphs are generated on the fly using **xk6**. Here's an example of the performance chart:


xk6-chart

## ๐Ÿ›ก๏ธ Security Features

- ๐Ÿ”‘ **UUID-based token tracking**
- ๐Ÿ”„ **Token Rotation Mechanism**
- โŒ **Automatic token invalidation**
- โฐ **Configurable token lifetimes**
- ๐Ÿ”„ **Secure token refresh mechanism**
- ๐Ÿ•’ **Auto-logout for inactive users**

### ๐Ÿ”„ Token Rotation Mechanism

The service implements a secure **token rotation mechanism** to enhance security:

- **Single-Use Refresh Tokens:** Each refresh token is valid for only one use. Upon using it to obtain a new token pair, the old refresh token is invalidated.
- **Prevents Replay Attacks:** This mechanism mitigates the risk of replay attacks by ensuring that stolen or leaked refresh tokens cannot be reused.
- **Seamless User Experience:** Token rotation happens transparently, providing continuous access without requiring the user to re-authenticate.

## ๐Ÿงฉ API Usage Examples

### ๐Ÿ”‘ Login

```bash
curl -X POST http://localhost:8080/login \
-H "Content-Type: application/json" \
-d '{"user_id": 1}'
```

### โ™ป๏ธ Refresh Token

```bash
curl -X POST http://localhost:8080/refresh \
-H "Content-Type: application/json" \
-d '{"refresh": "your-refresh-token"}'
```

### โœ… Authenticate

```bash
curl -X GET http://localhost:8080/authenticate \
-H "Authorization: Bearer your-access-token"
```

### ๐Ÿšช Logout

```bash
curl -X POST http://localhost:8080/logout \
-H "Authorization: Bearer your-access-token"
```

## ๐Ÿค Contributing

1. **Fork the repository**
2. **Create your feature branch** (`git checkout -b feature/amazing-feature`)
3. **Commit your changes** (`git commit -m 'Add amazing feature'`)
4. **Push to the branch** (`git push origin feature/amazing-feature`)
5. **Open a Pull Request**

## ๐Ÿ“„ License

This project is licensed under the **MIT License** - see the [LICENSE](LICENSE) file for details.