docsservicesAuth ServiceAuth Service

Auth Service

The Auth Service provides secure user authentication with a dual-token JWT system for the Goalixa platform.

Overview

Goalixa Auth is a Flask-based authentication service that handles:

  • User registration and login
  • JWT token management (access + refresh tokens)
  • OAuth integration (Google)
  • Password reset flows
  • Session management

Technology Stack

ComponentTechnology
FrameworkFlask (Python 3.11)
DatabaseSQLite (default), PostgreSQL support
ORMSQLAlchemy
AuthAuthlib (Google OAuth), JWT
MetricsPrometheus

Project Structure

goalixa-auth/
├── app.py                    # Single-file Flask application
├── auth/
│   ├── jwt.py               # JWT token handling
│   ├── models.py            # Database models
│   ├── oauth.py             # Google OAuth
│   ├── email.py             # Email service
│   ├── metrics.py           # Prometheus metrics
│   └── rate_limiter.py      # Rate limiting
├── data.db                  # SQLite database
└── requirements.txt

API Endpoints

Authentication

MethodEndpointDescription
POST/api/loginUser login
POST/api/registerUser registration
POST/api/logoutLogout and revoke token
POST/api/refreshRefresh access token
GET/api/meGet current user

Password Reset

MethodEndpointDescription
POST/api/forgotRequest password reset
POST/api/password-reset/confirmConfirm new password

Email Verification

MethodEndpointDescription
POST/api/verify-emailVerify email with token

OAuth

MethodEndpointDescription
GET/api/oauth/google/startInitiate Google OAuth
GET/api/oauth/google/callbackHandle OAuth callback

Session Management

MethodEndpointDescription
GET/api/sessionsList active sessions
POST/api/sessions/<id>/revokeRevoke session
POST/api/sessions/revoke-allRevoke all sessions

System

MethodEndpointDescription
GET/healthHealth check
GET/metricsPrometheus metrics

Dual-Token System

Access Token

  • TTL: 15 minutes (configurable)
  • Purpose: API authentication
  • Storage: HTTP-only cookie

Refresh Token

  • TTL: 7 days (configurable)
  • Purpose: Obtain new access tokens
  • Storage: HTTP-only cookie + database (device tracking)
  • Rotation: New refresh token on each refresh

Data Models

User

class User:
    id: int
    email: str
    password_hash: str
    active: bool
    email_verified: bool
    created_at: datetime
    updated_at: datetime

RefreshToken

class RefreshToken:
    id: int
    user_id: int
    jti: str              # Token identifier
    token: str            # Hashed token
    device_name: str
    device_type: str
    fingerprint: str
    user_agent: str
    ip_address: str
    expires_at: datetime
    created_at: datetime

PasswordResetToken

class PasswordResetToken:
    id: int
    user_id: int
    token: str
    expires_at: datetime  # 30 minute TTL
    created_at: datetime

Code Examples

User Registration

curl -X POST http://localhost:5001/api/register \
  -H "Content-Type: application/json" \
  -d '{
    "email": "user@example.com",
    "password": "SecurePass123!"
  }'

Response:

{
  "message": "User registered successfully",
  "user": {
    "id": 1,
    "email": "user@example.com",
    "email_verified": false
  }
}

User Login

curl -X POST http://localhost:5001/api/login \
  -H "Content-Type: application/json" \
  -d '{
    "email": "user@example.com",
    "password": "SecurePass123!"
  }'

Response:

{
  "message": "Login successful",
  "user": {
    "id": 1,
    "email": "user@example.com",
    "email_verified": true
  }
}

Cookies are set automatically:

  • access_token - HTTP-only, 15 minute TTL
  • refresh_token - HTTP-only, 7 day TTL

Refreshing Access Token

curl -X POST http://localhost:5001/api/refresh \
  -H "Cookie: refresh_token=<refresh_token>"

Response:

{
  "message": "Token refreshed"
}

Logging Out

curl -X POST http://localhost:5001/api/logout \
  -H "Cookie: access_token=<access_token>"

Google OAuth

# Step 1: Initiate OAuth
curl -X GET "http://localhost:5001/api/oauth/google/start?return_to=http://localhost:3000"
 
# Redirects to Google, then callback sets cookies

Security Features

Rate Limiting

EndpointLimit
Login5 per 5 minutes
Password reset3 per 5 minutes
Register5 per 5 minutes

Password Requirements

  • Minimum 8 characters
  • At least one uppercase letter
  • At least one lowercase letter
  • At least one digit
  • At least one special character
# All auth cookies are:
- HTTPOnly: True      # Prevent JavaScript access
- Secure: True        # HTTPS only in production
- SameSite: "lax"     # CSRF protection
- Domain: Configurable

Session Limits

  • Maximum 5 refresh tokens per user
  • Oldest token revoked when limit exceeded
  • Individual session revocation supported

Configuration

Environment Variables

VariableDescriptionDefault
AUTH_JWT_SECRETJWT signing secretRequired
AUTH_DATABASE_URIDatabase connectionsqlite:///data.db
AUTH_ACCESS_TOKEN_TTL_MINUTESAccess token TTL15
AUTH_REFRESH_TOKEN_TTL_DAYSRefresh token TTL7
AUTH_COOKIE_DOMAINCookie domainNone
AUTH_COOKIE_SECURESecure cookies1
GOOGLE_CLIENT_IDGoogle OAuth client IDNone
GOOGLE_CLIENT_SECRETGoogle OAuth client secretNone
REGISTERABLEEnable registration1

Docker

docker run -p 5001:5001 \
  -e AUTH_JWT_SECRET=your-secret \
  -e GOOGLE_CLIENT_ID=your-client-id \
  -e GOOGLE_CLIENT_SECRET=your-client-secret \
  goalixa-auth:latest

Kubernetes Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: auth-service
spec:
  replicas: 2
  selector:
    matchLabels:
      app: auth-service
  template:
    metadata:
      labels:
        app: auth-service
    spec:
      containers:
      - name: auth-service
        image: goalixa/auth:latest
        ports:
        - containerPort: 5001
        env:
        - name: AUTH_JWT_SECRET
          valueFrom:
            secretKeyRef:
              name: goalixa-secrets
              key: jwt-secret
        - name: AUTH_DATABASE_URI
          value: postgresql://user:pass@postgres:5432/auth
        - name: GOOGLE_CLIENT_ID
          valueFrom:
            secretKeyRef:
              name: goalixa-secrets
              key: google-client-id
        - name: GOOGLE_CLIENT_SECRET
          valueFrom:
            secretKeyRef:
              name: goalixa-secrets
              key: google-client-secret
        resources:
          requests:
            memory: "128Mi"
            cpu: "100m"
          limits:
            memory: "256Mi"
            cpu: "200m"
        livenessProbe:
          httpGet:
            path: /health
            port: 5001
          initialDelaySeconds: 10
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /health
            port: 5001
          initialDelaySeconds: 5
          periodSeconds: 5

Metrics

MetricTypeDescription
auth_login_totalCounterTotal login attempts
auth_login_success_totalCounterSuccessful logins
auth_register_totalCounterTotal registrations
auth_logout_totalCounterTotal logouts
auth_refresh_totalCounterToken refreshes
auth_password_reset_totalCounterPassword resets