File size: 2,846 Bytes
6aecb2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 保持此模块轻量,避免在最小化或测试环境中导入 request_context 等子模块时
# 提前加载可选依赖,例如 SQLAlchemy。

from __future__ import annotations

from typing import Any

__all__ = [
    "AuthService",
    "get_auth_service",
    "init_default_admin",
    "AuthMiddleware",
    "create_auth_middleware",
    "get_current_user",
    "require_auth",
    "require_admin",
    "get_current_user_optional",
    "get_current_user_required",
    "get_current_admin_user",
    "is_authenticated",
    "is_admin",
    "get_user_info",
    "auth_router",
]


def __getattr__(name: str) -> Any:  # pragma: no cover
    if name in {"AuthService", "get_auth_service", "init_default_admin"}:
        from .auth_service import AuthService, get_auth_service, init_default_admin

        value = {"AuthService": AuthService, "get_auth_service": get_auth_service, "init_default_admin": init_default_admin}[name]
        globals()[name] = value
        return value

    middleware_exports = {
        "AuthMiddleware",
        "create_auth_middleware",
        "get_current_user",
        "require_auth",
        "require_admin",
        "get_current_user_optional",
        "get_current_user_required",
        "get_current_admin_user",
        "is_authenticated",
        "is_admin",
        "get_user_info",
    }
    if name in middleware_exports:
        from .middleware import (
            AuthMiddleware,
            create_auth_middleware,
            get_current_user,
            require_auth,
            require_admin,
            get_current_user_optional,
            get_current_user_required,
            get_current_admin_user,
            is_authenticated,
            is_admin,
            get_user_info,
        )

        value = locals()[name]
        globals()[name] = value
        return value

    if name == "auth_router":
        from fastapi import APIRouter

        from .routes import router as _base_auth_router

        try:
            from .github_oauth_routes import router as _github_oauth_router
        except Exception:
            _github_oauth_router = None

        try:
            from .linuxdo_oauth_routes import router as _linuxdo_oauth_router
        except Exception:
            _linuxdo_oauth_router = None

        auth_router = APIRouter()
        auth_router.include_router(_base_auth_router)
        if _github_oauth_router is not None:
            auth_router.include_router(_github_oauth_router)
        if _linuxdo_oauth_router is not None:
            auth_router.include_router(_linuxdo_oauth_router)

        globals()["auth_router"] = auth_router
        return auth_router

    raise AttributeError(f"module {__name__!r} has no attribute {name!r}")


def __dir__() -> list[str]:  # pragma: no cover
    return sorted(set(globals().keys()) | set(__all__))