Buckets:

download
raw
14.1 kB
#!/usr/bin/env python3
"""
Lightweight Test Plan Runner for OIDC Conformance
Interprets test-plans/*.json without needing the full OpenID Foundation suite
"""
import json
import sys
import requests
import jwt
import base64
import hashlib
import secrets
from pathlib import Path
from datetime import datetime
from typing import Dict, Any, Tuple
class TestPlanRunner:
"""Run OIDC conformance tests from JSON test plans"""
def __init__(self, test_plan_path: str, base_url: str = "http://localhost:8000"):
"""Initialize with test plan and target server"""
self.base_url = base_url
self.test_plan = json.load(open(test_plan_path))
self.discovery = None
self.jwks = None
self.client_config = self.test_plan.get("client", {})
self.results = {
"plan_name": self.test_plan["name"],
"timestamp": datetime.now().isoformat(),
"modules": {},
"summary": {"total": 0, "passed": 0, "failed": 0, "skipped": 0},
}
# Test module registry - maps test names to methods
self.test_registry = {
# Discovery tests
"oidcc-server": self.test_server_discovery,
"oidcc-discovery-issuer-not-matching-config": self.test_issuer_match,
# UserInfo tests
"oidcc-userinfo-get": self.test_userinfo_get,
"oidcc-userinfo-post-header": self.test_userinfo_post_header,
# PKCE tests
"oidcc-ensure-pkce-required": self.test_pkce_required,
"oidcc-ensure-pkce-code-verifier-required": self.test_pkce_verifier_required,
"oidcc-ensure-pkce-code-challenge-method-s256": self.test_pkce_s256,
# ID Token tests
"oidcc-id-token-aud-single-value": self.test_id_token_aud,
"oidcc-id-token-iat": self.test_id_token_iat,
"oidcc-id-token-sub": self.test_id_token_sub,
# Scope tests
"oidcc-scope-profile": self.test_scope_profile,
"oidcc-scope-email": self.test_scope_email,
# Redirect URI tests
"oidcc-ensure-registered-redirect-uri": self.test_redirect_uri_exact_match,
}
def run(self) -> dict[str, Any]:
"""Run all test modules in the plan"""
print(f"🧪 Running Test Plan: {self.test_plan['name']}")
print("=" * 60)
# Load discovery document first
self._load_discovery()
# Run each test module
for module in self.test_plan.get("test_modules", []):
module_name = module["name"]
required = module.get("required", False)
if module_name in self.test_registry:
print(f"\n▶️ Testing: {module_name}")
print(f" {module.get('description', 'No description')}")
try:
result = self.test_registry[module_name]()
self.results["modules"][module_name] = {
"status": "PASS" if result else "FAIL",
"required": required,
"description": module.get("description", ""),
}
self.results["summary"]["passed" if result else "failed"] += 1
print(f" {'✅ PASS' if result else '❌ FAIL'}")
except Exception as e:
self.results["modules"][module_name] = {"status": "ERROR", "error": str(e), "required": required}
self.results["summary"]["failed"] += 1
print(f" ❌ ERROR: {e}")
else:
self.results["modules"][module_name] = {"status": "NOT_IMPLEMENTED", "required": required}
self.results["summary"]["skipped"] += 1
print(f"\n⏭️ Skipping: {module_name} (not implemented)")
self.results["summary"]["total"] += 1
return self.results
def _load_discovery(self):
"""Load discovery document and JWKS"""
try:
# Try hyphen version (spec-compliant)
resp = requests.get(f"{self.base_url}/.well-known/openid-configuration")
if resp.status_code == 200:
self.discovery = resp.json()
# Load JWKS
jwks_uri = self.discovery.get("jwks_uri")
if jwks_uri:
jwks_resp = requests.get(jwks_uri)
if jwks_resp.status_code == 200:
self.jwks = jwks_resp.json()
except Exception as e:
print(f"⚠️ Failed to load discovery: {e}")
# Test implementations
def test_server_discovery(self) -> bool:
"""Test that server discovery document is valid"""
if not self.discovery:
return False
required_fields = [
"issuer",
"authorization_endpoint",
"token_endpoint",
"userinfo_endpoint",
"jwks_uri",
"response_types_supported",
"subject_types_supported",
"id_token_signing_alg_values_supported",
]
return all(field in self.discovery for field in required_fields)
def test_issuer_match(self) -> bool:
"""Test that issuer in discovery matches expected"""
if not self.discovery:
return False
expected_issuer = self.test_plan.get("server", {}).get("issuer")
actual_issuer = self.discovery.get("issuer")
return expected_issuer == actual_issuer
def test_userinfo_get(self) -> bool:
"""Test UserInfo endpoint with GET method"""
# Without a real token, we can only test that it requires auth
resp = requests.get(f"{self.base_url}/oidc/userinfo")
# Should return 401 without token
if resp.status_code != 401:
return False
# Should have WWW-Authenticate header
return "WWW-Authenticate" in resp.headers
def test_userinfo_post_header(self) -> bool:
"""Test UserInfo endpoint with POST method and Bearer header"""
# Test that POST is also supported
resp = requests.post(f"{self.base_url}/oidc/userinfo", headers={"Authorization": "Bearer invalid"})
# Should return 401 with invalid token
return resp.status_code == 401
def test_pkce_required(self) -> bool:
"""Test that PKCE is required for authorization"""
if not self.discovery:
return False
# Check discovery document
if self.discovery.get("require_pkce"):
return True
# Try authorization without PKCE
auth_endpoint = self.discovery.get("authorization_endpoint")
if auth_endpoint:
# Remove base URL if it's included
path = auth_endpoint.replace(self.base_url, "") if auth_endpoint.startswith("http") else auth_endpoint
resp = requests.get(
f"{self.base_url}{path}",
params={
"client_id": "test",
"response_type": "code",
"redirect_uri": "http://localhost/callback",
"scope": "openid",
},
allow_redirects=False,
)
# Should fail without PKCE (422 or 400)
return resp.status_code in [400, 422]
return False
def test_pkce_verifier_required(self) -> bool:
"""Test that code_verifier is required at token endpoint"""
# Try token exchange without code_verifier
resp = requests.post(
f"{self.base_url}/api/v1/oauth/token",
data={"grant_type": "authorization_code", "code": "invalid_code", "client_id": "test"},
)
# Should return 400 (not 401 or 500)
return resp.status_code == 400
def test_pkce_s256(self) -> bool:
"""Test S256 code challenge method support"""
if not self.discovery:
return False
methods = self.discovery.get("code_challenge_methods_supported", [])
return "S256" in methods
def test_id_token_aud(self) -> bool:
"""Test ID token aud claim requirements"""
# This would need a real ID token to validate
# For now, check that discovery supports ID tokens
if not self.discovery:
return False
algs = self.discovery.get("id_token_signing_alg_values_supported", [])
return len(algs) > 0
def test_id_token_iat(self) -> bool:
"""Test ID token iat claim requirements"""
# Check that server supports ID tokens
return self.test_id_token_aud()
def test_id_token_sub(self) -> bool:
"""Test ID token sub claim requirements"""
# Check that server supports ID tokens
return self.test_id_token_aud()
def test_scope_profile(self) -> bool:
"""Test profile scope support"""
if not self.discovery:
return False
scopes = self.discovery.get("scopes_supported", [])
return "profile" in scopes
def test_scope_email(self) -> bool:
"""Test email scope support"""
if not self.discovery:
return False
scopes = self.discovery.get("scopes_supported", [])
return "email" in scopes
def test_redirect_uri_exact_match(self) -> bool:
"""Test that redirect_uri must match exactly"""
# Try with mismatched redirect_uri
if not self.discovery:
return False
auth_endpoint = self.discovery.get("authorization_endpoint")
if auth_endpoint:
path = auth_endpoint.replace(self.base_url, "") if auth_endpoint.startswith("http") else auth_endpoint
resp = requests.get(
f"{self.base_url}{path}",
params={
"client_id": "test",
"response_type": "code",
"redirect_uri": "http://invalid.example.com/callback",
"scope": "openid",
"code_challenge": "test",
"code_challenge_method": "S256",
},
allow_redirects=False,
)
# Should reject invalid redirect_uri
return resp.status_code in [400, 422]
return False
def generate_report(self) -> str:
"""Generate a conformance report"""
report = f"""# Test Plan Execution Report
## {self.results["plan_name"]}
**Executed**: {self.results["timestamp"]}
**Server**: {self.base_url}
## Summary
- **Total Tests**: {self.results["summary"]["total"]}
- **Passed**: {self.results["summary"]["passed"]} ✅
- **Failed**: {self.results["summary"]["failed"]} ❌
- **Skipped**: {self.results["summary"]["skipped"]} ⏭️
- **Pass Rate**: {self.results["summary"]["passed"] / max(1, self.results["summary"]["total"]) * 100:.1f}%
## Test Results
| Test Module | Status | Required | Description |
|-------------|--------|----------|-------------|
"""
for name, result in self.results["modules"].items():
status = result["status"]
icon = {"PASS": "✅", "FAIL": "❌", "ERROR": "⚠️", "NOT_IMPLEMENTED": "⏭️"}.get(status, "❓")
required = "Yes" if result.get("required") else "No"
desc = result.get("description", "")[:50]
report += f"| {name} | {icon} {status} | {required} | {desc} |\n"
# Add required failures
required_failures = [
name for name, r in self.results["modules"].items() if r.get("required") and r["status"] != "PASS"
]
if required_failures:
report += f"\n## ⚠️ Required Tests Failed\n"
for name in required_failures:
report += f"- {name}\n"
report += "\n## Certification Readiness\n"
# Calculate required pass rate
required_tests = [r for r in self.results["modules"].values() if r.get("required")]
required_passed = sum(1 for r in required_tests if r["status"] == "PASS")
required_total = len(required_tests)
if required_total > 0:
required_rate = required_passed / required_total * 100
if required_rate == 100:
report += "✅ **READY** - All required tests pass!\n"
elif required_rate >= 80:
report += f"⚠️ **NEARLY READY** - {required_rate:.0f}% of required tests pass\n"
else:
report += f"❌ **NOT READY** - Only {required_rate:.0f}% of required tests pass\n"
return report
def main():
"""Run test plan"""
if len(sys.argv) < 2:
print("Usage: python test_plan_runner.py <test-plan.json>")
print("\nAvailable test plans:")
print(" config/test-plans/basic-certification.json")
print(" config/test-plans/pkce-certification.json")
sys.exit(1)
test_plan_path = sys.argv[1]
if not Path(test_plan_path).exists():
# Try relative to tck directory
alt_path = Path(__file__).parent.parent / test_plan_path
if alt_path.exists():
test_plan_path = alt_path
else:
print(f"Error: Test plan not found: {test_plan_path}")
sys.exit(1)
runner = TestPlanRunner(test_plan_path)
results = runner.run()
# Generate and save report
report = runner.generate_report()
reports_dir = Path(__file__).parent.parent / "reports" / "test-plans"
reports_dir.mkdir(parents=True, exist_ok=True)
plan_name = Path(test_plan_path).stem
report_path = reports_dir / f"{plan_name}_report.md"
with open(report_path, "w") as f:
f.write(report)
print("\n" + "=" * 60)
print(f"📄 Report saved to: {report_path}")
# Print summary
print("\n" + report.split("## Test Results")[0])
# Exit with error if required tests failed
required_failures = [name for name, r in results["modules"].items() if r.get("required") and r["status"] != "PASS"]
if required_failures:
sys.exit(1)
return 0
if __name__ == "__main__":
main()

Xet Storage Details

Size:
14.1 kB
¡
Xet hash:
730c241266c6887dac14f680ad0d4d9910d7d5155b2c4f2e409f184c2c0afe46

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.