refactor(auth): improve code structure and add user registration endpoint
refactor(main): update index route response and remove greeting service refactor(decorators): streamline authentication decorators and remove unused ones test(routes): update tests to reflect changes in main routes and error messages
This commit is contained in:
@@ -2,64 +2,53 @@
|
||||
|
||||
from flask import Blueprint
|
||||
|
||||
from app.services.decorators import get_current_user, require_auth, require_admin, require_auth_or_api_token, get_user_from_api_token
|
||||
from app.services.greeting_service import GreetingService
|
||||
from app.services.decorators import get_current_user, require_auth, require_role
|
||||
|
||||
bp = Blueprint("main", __name__)
|
||||
|
||||
|
||||
@bp.route("/")
|
||||
def index() -> dict[str, str]:
|
||||
"""Root endpoint that returns a greeting."""
|
||||
return GreetingService.get_greeting()
|
||||
|
||||
|
||||
@bp.route("/hello")
|
||||
@bp.route("/hello/<name>")
|
||||
def hello(name: str | None = None) -> dict[str, str]:
|
||||
"""Hello endpoint with optional name parameter."""
|
||||
return GreetingService.get_greeting(name)
|
||||
"""Root endpoint that returns API status."""
|
||||
return {"message": "API is running", "status": "ok"}
|
||||
|
||||
|
||||
@bp.route("/protected")
|
||||
@require_auth
|
||||
def protected() -> dict[str, str]:
|
||||
"""Protected endpoint that requires JWT authentication."""
|
||||
"""Protected endpoint that requires authentication."""
|
||||
user = get_current_user()
|
||||
return {
|
||||
"message": f"Hello {user['name']}, this is a protected endpoint!",
|
||||
"user": user
|
||||
"user": user,
|
||||
}
|
||||
|
||||
|
||||
@bp.route("/api-protected")
|
||||
@require_auth_or_api_token
|
||||
@require_auth
|
||||
def api_protected() -> dict[str, str]:
|
||||
"""Protected endpoint that accepts JWT or API token authentication."""
|
||||
# Try to get user from JWT first, then API token
|
||||
user = get_current_user()
|
||||
if not user:
|
||||
user = get_user_from_api_token()
|
||||
|
||||
return {
|
||||
"message": f"Hello {user['name']}, you accessed this via {user['provider']}!",
|
||||
"user": user
|
||||
"user": user,
|
||||
}
|
||||
|
||||
|
||||
@bp.route("/admin")
|
||||
@require_admin
|
||||
@require_auth
|
||||
@require_role("admin")
|
||||
def admin_only() -> dict[str, str]:
|
||||
"""Admin-only endpoint to demonstrate role-based access."""
|
||||
user = get_current_user()
|
||||
return {
|
||||
"message": f"Hello admin {user['name']}, you have admin access!",
|
||||
"user": user,
|
||||
"admin_info": "This endpoint is only accessible to admin users"
|
||||
"admin_info": "This endpoint is only accessible to admin users",
|
||||
}
|
||||
|
||||
|
||||
@bp.route("/health")
|
||||
def health() -> dict[str, str]:
|
||||
"""Health check endpoint."""
|
||||
return {"status": "ok"}
|
||||
return {"status": "ok"}
|
||||
|
||||
Reference in New Issue
Block a user