Skip to content

6ermvH/auth-service

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Auth Service

A simple and lightweight microservice for handling JWT-based authentication. This service provides endpoints for generating and refreshing tokens, using a PostgreSQL database to securely store refresh tokens.

🚀 Getting Started

The recommended way to run this service is with Docker Compose.

Prerequisites

  • Docker and Docker Compose
  • A .env file in the project root (you can copy .env.example if it exists, or create one from scratch).

1. Configure Your Environment

Create a .env file in the project root with the following variables:

# For JWT signing
JWT_SECRET_KEY=your_super_secret_key

# PostgreSQL connection details
DB_USER=your_db_user
DB_PASSWORD=your_db_password
DB_NAME=your_db_name

You can generate a secure JWT_SECRET_KEY with the following command:

openssl rand -base64 64

2. Run the Service

With your .env file configured, start the service using Docker Compose:

docker-compose up --build -d

The service will be available at http://localhost:8080.

⚙️ API Endpoints

Generate Tokens

  • Request:

    curl -X POST "http://localhost:8080/token?user_id=<your_user_uuid>"
  • Success Response (200 OK):

    {
      "access_token": "your_jwt_access_token",
      "refresh_token": "your_base64_refresh_token"
    }

Refresh Tokens

  • Request:

    curl -X POST http://localhost:8080/refresh \
         -H "Content-Type: application/json" \
         -d '{"access_token": "your_jwt_access_token", "refresh_token": "your_base64_refresh_token"}'
  • Success Response (200 OK):

    {
      "access_token": "new_jwt_access_token",
      "refresh_token": "new_base64_refresh_token"
    }

🗄️ Database Schema

The service uses a single table to store refresh token data. The docker-compose.yml is configured to automatically run any .sql scripts placed in the /migrations directory upon database creation.

CREATE TABLE IF NOT EXISTS refresh_tokens (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    user_id UUID NOT NULL,
    access_token_sha256 TEXT NOT NULL,
    refresh_token_hash TEXT NOT NULL,
    client_ip TEXT NOT NULL,
    expired_at TIMESTAMP DEFAULT now() + interval \'7 days\',
    is_used BOOLEAN DEFAULT false
);

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published