75 lines
1.7 KiB
Python
75 lines
1.7 KiB
Python
#!/usr/bin/python3
|
|||
|
|
# -*- coding: utf-8 -*-
|
||
|
|
"""
|
||
|
|
Base authentication provider interface
|
||
|
|
"""
|
||
|
|
|
||
|
|
from abc import ABC, abstractmethod
|
||
|
|
from functools import wraps
|
||
|
|
|
||
|
|
|
||
|
|
class AuthProvider(ABC):
|
||
|
|
"""Base class for authentication providers."""
|
||
|
|
|
||
|
|
def __init__(self, config):
|
||
|
|
"""Initialize the auth provider.
|
||
|
|
|
||
|
|
Args:
|
||
|
|
config: Dict with provider-specific configuration
|
||
|
|
"""
|
||
|
|
self.config = config
|
||
|
|
|
||
|
|
def init_app(self, app):
|
||
|
|
"""Initialize the provider with Flask app.
|
||
|
|
|
||
|
|
Args:
|
||
|
|
app: Flask application instance
|
||
|
|
"""
|
||
|
|
pass
|
||
|
|
|
||
|
|
def require_auth(self, f):
|
||
|
|
"""Decorator to require authentication for a route.
|
||
|
|
|
||
|
|
Args:
|
||
|
|
f: Flask route function
|
||
|
|
|
||
|
|
Returns:
|
||
|
|
Decorated function
|
||
|
|
"""
|
||
|
|
@wraps(f)
|
||
|
|
def decorated_function(*args, **kwargs):
|
||
|
|
if not self.is_authenticated():
|
||
|
|
return self.handle_unauthorized()
|
||
|
|
return f(*args, **kwargs)
|
||
|
|
return decorated_function
|
||
|
|
|
||
|
|
def is_authenticated(self):
|
||
|
|
"""Check if current request is authenticated.
|
||
|
|
|
||
|
|
Returns:
|
||
|
|
bool: True if authenticated, False otherwise
|
||
|
|
"""
|
||
|
|
# Default: no authentication required
|
||
|
|
return True
|
||
|
|
|
||
|
|
def handle_unauthorized(self):
|
||
|
|
"""Handle unauthorized access.
|
||
|
|
|
||
|
|
Returns:
|
||
|
|
Flask response for unauthorized access
|
||
|
|
"""
|
||
|
|
from flask import jsonify
|
||
|
|
return jsonify({"error": "Unauthorized"}), 401
|
||
|
|
|
||
|
|
def get_current_user(self):
|
||
|
|
"""Get current authenticated user.
|
||
|
|
|
||
|
|
Returns:
|
||
|
|
User object or dict, or None if not authenticated
|
||
|
|
"""
|
||
|
|
return None
|
||
|
|
|
||
|
|
def logout(self):
|
||
|
|
"""Logout current user."""
|
||
|
|
pass
|