Auth Service Documentation

This document details the Authorization Service, a microservice in the space trader elite-style backend. Its purpose is to handle user authentication, including registration, login, and the management of JWT and refresh tokens.

1. Data Models

The Auth Service relies on two primary data models to manage user data and sessions.

1.1. User Model

Represents a user account in the system.

User.cs
namespace AuthService.Models
{
    public class User
    {
        public int Id { get; set; }
        public string Username { get; set; } = default!;
        public string PasswordHash { get; set; } = default!;
        public string Role { get; set; } = "User";
        public ICollection RefreshTokens { get; set; } = new List();
    }
}

Fields:

1.2. RefreshToken Model

Used to generate new JWTs without requiring the user to re-enter their credentials.

RefreshToken.cs
namespace AuthService.Models
{
    public class RefreshToken
    {
        public int Id { get; set; }
        public string Token { get; set; } = default!;
        public DateTime ExpiresAt { get; set; }
        public int UserId { get; set; }
        public User User { get; set; } = default!;
    }
}

Fields:

2. API Endpoints

All endpoints are defined within the AuthController and are prefixed with /api/Auth.

2.1. Register User

POST /api/Auth/register

Registers a new user with a username, password, and role.

Request Body (JSON)

{
  "username": "newuser",
  "password": "securepassword123",
  "role": "User"
}

Responses

2.2. Login User

POST /api/Auth/login

Authenticates a user and issues a new JWT and a refresh token.

Request Body (JSON)

{
  "username": "existinguser",
  "password": "securepassword123"
}

Responses

2.3. Delete User

POST /api/Auth/delete

Deletes a user account and all associated refresh tokens.

Request Body (JSON)

{
  "username": "userToDelete"
}

Responses

2.4. Refresh Token

POST /api/Auth/refresh

Exchanges an old, unexpired refresh token for a new JWT and a new refresh token.

Request Body (JSON)

{
  "refreshToken": "old_refresh_token_string"
}

Responses

3. Business Logic

The core logic of the service is managed by the AuthServiceLogic class.

4. Configuration and Dependencies

4.1. Configuration

The appsettings.json file contains all the necessary configuration settings.

appsettings.json
{
    "Jwt": {
        "Key": "super-secure-secret-key-change-me-super-secure-secret-key-change-me",
        "Issuer": "AuthService",
        "Audience": "ClientApp",
        "ExpiresInMinutes": 15,
        "RefreshTokenExpiresInDays": 7
    },
    "ConnectionStrings": {
        "DatabaseConnection": "Server=auth_db_server;Username=auth_db_user;Password=auth_db_password;Database=auth_db"
    }
}

The database connection string and JWT parameters are defined here. The ApiGateway and AuthService share the same JWT configuration (Key, Issuer, Audience) to ensure that tokens issued by the auth service can be validated by the gateway.

4.2. Dependencies

Key dependencies are managed in the AuthService.csproj file.

5. Docker and Deployment

The service is containerized using a multi-stage Dockerfile. The final image is based on a lightweight ASP.NET runtime image, which is optimized for deployment.