sgl-model-gateway/examples/wasm/wasm-guest-auth/README.md
This example demonstrates API key authentication middleware for sgl-model-gateway using the WebAssembly Component Model.
This middleware validates API keys for requests to /api and /v1 paths:
Authorization: Bearer <key> headerAuthorization: ApiKey <key> headerx-api-key header401 Unauthorized for missing or invalid keysDefault API Key: secret-api-key-12345
# Build
cd examples/wasm-guest-auth
./build.sh
# Deploy (replace file_path with actual path)
curl -X POST http://localhost:3000/wasm \
-H "Content-Type: application/json" \
-d '{
"modules": [{
"name": "auth-middleware",
"file_path": "/absolute/path/to/wasm_guest_auth.component.wasm",
"module_type": "Middleware",
"attach_points": [{"Middleware": "OnRequest"}]
}]
}'
Modify EXPECTED_API_KEY in src/lib.rs:
const EXPECTED_API_KEY: &str = "your-secret-key";
# Test unauthorized (returns 401)
curl -v http://localhost:3000/api/test
# Test authorized (passes)
curl -v http://localhost:3000/api/test \
-H "Authorization: Bearer secret-api-key-12345"
EXPECTED_API_KEY in code/api or /v1)OnRequest phase