Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions v2/examples/rest-api-simple/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,44 @@
const express = require('express');
const rateLimit = require('express-rate-limit');
const app = express();
const port = process.env.PORT || 3000;

// Middleware
app.use(express.json());

// Security: Rate limiting middleware
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
message: 'Too many requests from this IP, please try again later.'
});
app.use(limiter);

// Security: Simple authentication middleware
const authenticateToken = (req, res, next) => {
const authHeader = req.headers['authorization'];
const token = authHeader && authHeader.split(' ')[1];

if (!token) {
return res.status(401).json({ error: 'Authentication required' });
}

// Simple token validation - in production use JWT
if (token !== process.env.API_TOKEN && token !== 'demo-token') {
return res.status(403).json({ error: 'Invalid token' });
}

next();
};

// Security: Logging middleware
const logRequest = (req, res, next) => {
const timestamp = new Date().toISOString();
console.log(`[${timestamp}] ${req.method} ${req.path} - IP: ${req.ip}`);
next();
};
app.use(logRequest);

// In-memory data store
let items = [
{ id: 1, name: 'Item 1', description: 'This is the first item' },
Expand Down
43 changes: 43 additions & 0 deletions v2/src/swarm/sparc-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -960,15 +960,36 @@ class Product(db.Model):
}
`,
routes: `from flask import Blueprint, request, jsonify
from functools import wraps
from models import db, User, Product
from services import UserService, ProductService

api_bp = Blueprint('api', __name__)
user_service = UserService()
product_service = ProductService()

# Authentication decorator
def require_auth(f):
@wraps(f)
def decorated_function(*args, **kwargs):
# Extract user info from request headers or session
# This assumes JWT token or session-based auth is implemented
auth_header = request.headers.get('Authorization')
if not auth_header:
return jsonify({'error': 'Authentication required'}), 401
return f(*args, **kwargs)
return decorated_function

def get_current_user():
# Extract current user from request context
# This would typically come from JWT token or session
# For now, returning mock - should be implemented based on auth strategy
from flask import g
return getattr(g, 'current_user', None)

# User routes
@api_bp.route('/users', methods=['GET'])
@require_auth
def get_users():
page = request.args.get('page', 1, type=int)
per_page = request.args.get('per_page', 10, type=int)
Expand All @@ -982,6 +1003,7 @@ def get_users():
})

@api_bp.route('/users/<int:user_id>', methods=['GET'])
@require_auth
def get_user(user_id):
user = User.query.get_or_404(user_id)
return jsonify(user.to_dict())
Expand All @@ -1002,15 +1024,29 @@ def create_user():
return jsonify(user.to_dict()), 201

@api_bp.route('/users/<int:user_id>', methods=['PUT'])
@require_auth
def update_user(user_id):
current_user = get_current_user()

# Authorization check: users can only update their own profile, admin can update any
if current_user and current_user.id != user_id and not getattr(current_user, 'is_admin', False):
return jsonify({'error': 'Unauthorized: Cannot update other users'}), 403

user = User.query.get_or_404(user_id)
data = request.get_json()

user = user_service.update_user(user, data)
return jsonify(user.to_dict())

@api_bp.route('/users/<int:user_id>', methods=['DELETE'])
@require_auth
def delete_user(user_id):
current_user = get_current_user()

# Authorization check: only admin users can delete users
if not current_user or not getattr(current_user, 'is_admin', False):
return jsonify({'error': 'Unauthorized: Only administrators can delete users'}), 403

user = User.query.get_or_404(user_id)
user_service.delete_user(user)
return '', 204
Expand All @@ -1022,7 +1058,14 @@ def get_products():
return jsonify([p.to_dict() for p in products])

@api_bp.route('/products', methods=['POST'])
@require_auth
def create_product():
current_user = get_current_user()

# Authorization check: only admin users can create products
if not current_user or not getattr(current_user, 'is_admin', False):
return jsonify({'error': 'Unauthorized: Only administrators can create products'}), 403

data = request.get_json()
product = product_service.create_product(data)
return jsonify(product.to_dict()), 201
Expand Down
Loading