The before code shows security only at the API Gateway, trusting internal services blindly. The after code adds authentication and authorization checks inside Service A, ensuring security spans all services.
### Before: Security only at API Gateway
class APIGateway:
def handle_request(self, request):
if not self.authenticate(request):
return "Unauthorized"
return self.forward_to_service(request)
class ServiceA:
def process(self, request):
# No security checks here
return "Processed by Service A"
### After: Security in every service
class APIGateway:
def handle_request(self, request):
if not self.authenticate(request):
return "Unauthorized"
return self.forward_to_service(request)
class ServiceA:
def process(self, request):
if not self.authenticate(request):
return "Unauthorized"
if not self.authorize(request):
return "Forbidden"
return "Processed by Service A"
def authenticate(self, request):
# Verify token or credentials
return True
def authorize(self, request):
# Check permissions
return True
def forward_to_service(self, request):
# Placeholder for forwarding logic
pass
def authenticate(self, request):
# Placeholder for authentication logic
return True