From 608585c72cae6ca20d3db45f7af6ed267f64d1a7 Mon Sep 17 00:00:00 2001 From: Urtzi Alfaro Date: Sun, 20 Jul 2025 09:18:08 +0200 Subject: [PATCH] Improve auth process 4 --- scripts/test_unified_auth.sh | 61 ++++++++++--------- services/auth/app/services/auth_service.py | 20 +++++- services/tenant/app/main.py | 4 +- services/tenant/app/models/tenants.py | 2 +- services/tenant/app/services/messaging.py | 13 ++-- .../tenant/app/services/tenant_service.py | 2 +- 6 files changed, 59 insertions(+), 43 deletions(-) diff --git a/scripts/test_unified_auth.sh b/scripts/test_unified_auth.sh index fd92e981..54def8e5 100755 --- a/scripts/test_unified_auth.sh +++ b/scripts/test_unified_auth.sh @@ -119,6 +119,37 @@ fi echo "" + +# ================================================================ +# STEP 5: TENANT REGISTRATION (OPTIONAL) +# ================================================================ + +log_step "Step 5: Registering a bakery/tenant" + +BAKERY_RESPONSE=$(curl -s -X POST "$API_BASE/api/v1/tenants/bakeries" \ + -H "Authorization: Bearer $ACCESS_TOKEN" \ + -H "Content-Type: application/json" \ + -d "{ + \"name\": \"Test Bakery $(date +%H%M)\", + \"business_type\": \"bakery\", + \"address\": \"Calle Test 123\", + \"city\": \"Madrid\", + \"postal_code\": \"28001\", + \"phone\": \"+34600123456\" + }") + +echo "Bakery Registration Response:" +echo "$BAKERY_RESPONSE" | jq '.' + +if echo "$BAKERY_RESPONSE" | jq -e '.id' > /dev/null; then + TENANT_ID=$(echo "$BAKERY_RESPONSE" | jq -r '.id') + log_success "Bakery registration successful! Tenant ID: $TENANT_ID" +else + log_warning "Bakery registration endpoint may not be fully implemented" +fi + +echo "" + # ================================================================ # STEP 2: USER LOGIN # ================================================================ @@ -237,36 +268,6 @@ fi echo "" -# ================================================================ -# STEP 5: TENANT REGISTRATION (OPTIONAL) -# ================================================================ - -log_step "Step 5: Registering a bakery/tenant" - -BAKERY_RESPONSE=$(curl -s -X POST "$API_BASE/api/v1/tenants/bakeries" \ - -H "Authorization: Bearer $ACCESS_TOKEN" \ - -H "Content-Type: application/json" \ - -d "{ - \"name\": \"Test Bakery $(date +%H%M)\", - \"business_type\": \"bakery\", - \"address\": \"Calle Test 123\", - \"city\": \"Madrid\", - \"postal_code\": \"28001\", - \"phone\": \"+34600123456\" - }") - -echo "Bakery Registration Response:" -echo "$BAKERY_RESPONSE" | jq '.' - -if echo "$BAKERY_RESPONSE" | jq -e '.id' > /dev/null; then - TENANT_ID=$(echo "$BAKERY_RESPONSE" | jq -r '.id') - log_success "Bakery registration successful! Tenant ID: $TENANT_ID" -else - log_warning "Bakery registration endpoint may not be fully implemented" -fi - -echo "" - # ================================================================ # STEP 6: TOKEN REFRESH # ================================================================ diff --git a/services/auth/app/services/auth_service.py b/services/auth/app/services/auth_service.py index fe44bcdd..027c085e 100644 --- a/services/auth/app/services/auth_service.py +++ b/services/auth/app/services/auth_service.py @@ -113,9 +113,16 @@ class AuthService: tenant_memberships = await AuthService._get_user_tenants(str(user.id)) # Create tokens - access_token = SecurityManager.create_access_token(user) + access_token = SecurityManager.create_access_token( + user_data={ + "user_id": str(user.id), + "email": user.email, + "full_name": user.full_name, + "tenants": tenant_memberships # Include tenant info in token + } + ) - refresh_token_value = SecurityManager.create_refresh_token(data={"user_id": str(user.id)}) + refresh_token_value = SecurityManager.create_refresh_token(user_data={"user_id": str(user.id)}) # Store refresh token in database refresh_token = RefreshToken( @@ -198,7 +205,14 @@ class AuthService: tenant_memberships = await AuthService._get_user_tenants(str(user.id)) # Create new access token - access_token = SecurityManager.create_access_token(user) + access_token = SecurityManager.create_access_token( + user_data={ + "user_id": str(user.id), + "email": user.email, + "full_name": user.full_name, + "tenants": tenant_memberships + } + ) return { "access_token": access_token, diff --git a/services/tenant/app/main.py b/services/tenant/app/main.py index 5de394f2..7828239a 100644 --- a/services/tenant/app/main.py +++ b/services/tenant/app/main.py @@ -8,7 +8,7 @@ from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from app.core.config import settings -from app.core.database import engine +from app.core.database import database_manager from app.api import tenants from shared.monitoring.logging import setup_logging from shared.monitoring.metrics import MetricsCollector @@ -51,7 +51,7 @@ async def startup_event(): async def shutdown_event(): """Cleanup on shutdown""" logger.info("Shutting down Tenant Service...") - await engine.dispose() + await database_manager.engine.dispose() @app.get("/health") async def health_check(): diff --git a/services/tenant/app/models/tenants.py b/services/tenant/app/models/tenants.py index 3fcba9df..0343d085 100644 --- a/services/tenant/app/models/tenants.py +++ b/services/tenant/app/models/tenants.py @@ -4,7 +4,7 @@ Tenant models for bakery management - FIXED Removed cross-service User relationship to eliminate circular dependencies """ -from sqlalchemy import Column, String, Boolean, DateTime, Float, ForeignKey, Text +from sqlalchemy import Column, String, Boolean, DateTime, Float, ForeignKey, Text, Integer from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.orm import relationship from datetime import datetime, timezone diff --git a/services/tenant/app/services/messaging.py b/services/tenant/app/services/messaging.py index 84b0f49e..c73d7a55 100644 --- a/services/tenant/app/services/messaging.py +++ b/services/tenant/app/services/messaging.py @@ -2,17 +2,19 @@ """ Tenant service messaging for event publishing """ - +from shared.messaging.rabbitmq import RabbitMQClient +from app.core.config import settings import structlog -from shared.messaging.rabbitmq import RabbitMQPublisher logger = structlog.get_logger() +# Single global instance +data_publisher = RabbitMQClient(settings.RABBITMQ_URL, "data-service") + async def publish_tenant_created(tenant_id: str, owner_id: str, tenant_name: str): """Publish tenant created event""" try: - publisher = RabbitMQPublisher() - await publisher.publish_event( + await data_publisher.publish_event( "tenant.created", { "tenant_id": tenant_id, @@ -27,8 +29,7 @@ async def publish_tenant_created(tenant_id: str, owner_id: str, tenant_name: str async def publish_member_added(tenant_id: str, user_id: str, role: str): """Publish member added event""" try: - publisher = RabbitMQPublisher() - await publisher.publish_event( + await data_publisher.publish_event( "tenant.member.added", { "tenant_id": tenant_id, diff --git a/services/tenant/app/services/tenant_service.py b/services/tenant/app/services/tenant_service.py index 60294d01..7b08db39 100644 --- a/services/tenant/app/services/tenant_service.py +++ b/services/tenant/app/services/tenant_service.py @@ -13,7 +13,7 @@ import uuid import json from app.models.tenants import Tenant, TenantMember -from app.schemas.tenants import BakeryRegistration, TenantResponse, TenantAccessResponse, TenantUpdate +from app.schemas.tenants import BakeryRegistration, TenantResponse, TenantAccessResponse, TenantUpdate, TenantMemberResponse from app.services.messaging import publish_tenant_created, publish_member_added logger = structlog.get_logger()