67 lines
2.2 KiB
Python
67 lines
2.2 KiB
Python
from __future__ import annotations
|
|
|
|
from importlib.util import find_spec
|
|
from typing import Any, Protocol
|
|
|
|
from streamlit_oauth import OAuth2Component
|
|
|
|
from app.config import get_location, get_setting
|
|
|
|
from .mock_provider import get_jwt_token as get_jwt_token_local
|
|
from .mock_provider import protect_application as protect_application_local
|
|
|
|
|
|
class AuthProvider(Protocol):
|
|
def get_token_data(self) -> dict[str, Any] | None: ...
|
|
|
|
|
|
class LocalMockProvider:
|
|
def get_token_data(self) -> dict[str, Any] | None:
|
|
protect_application_local(render_exit=False)
|
|
return get_jwt_token_local()
|
|
|
|
|
|
class RaisaProvider:
|
|
def get_token_data(self) -> dict[str, Any] | None:
|
|
from raisa_streamlit_oauth import ( # type: ignore[reportMissingImports]
|
|
get_jwt_token as get_jwt_token_raisa,
|
|
)
|
|
from raisa_streamlit_oauth import ( # type: ignore[reportMissingImports]
|
|
protect_application as protect_application_raisa,
|
|
)
|
|
|
|
protect_application_raisa(render_exit=False)
|
|
return get_jwt_token_raisa()
|
|
|
|
|
|
class OAuthFallbackProvider:
|
|
def get_token_data(self) -> dict[str, Any] | None:
|
|
oauth2 = OAuth2Component(
|
|
client_id=get_setting("KEYCLOAK_CLIENT_ID"),
|
|
client_secret=get_setting("KEYCLOAK_CLIENT_SECRET"),
|
|
authorize_endpoint=get_setting("KEYCLOAK_AUTHORIZE_URL"),
|
|
token_endpoint=get_setting("KEYCLOAK_TOKEN_URL"),
|
|
refresh_token_endpoint=get_setting("KEYCLOAK_TOKEN_URL"),
|
|
revoke_token_endpoint=get_setting("KEYCLOAK_LOGOUT_URL"),
|
|
)
|
|
result = oauth2.authorize_button(
|
|
name="Войти через Keycloak",
|
|
redirect_uri=get_setting("KEYCLOAK_REDIRECT_URI"),
|
|
scope=get_setting("KEYCLOAK_SCOPE"),
|
|
key="keycloak-login",
|
|
use_container_width=True,
|
|
pkce="S256",
|
|
)
|
|
if result and "token" in result:
|
|
return result["token"]
|
|
return None
|
|
|
|
|
|
def build_auth_provider() -> AuthProvider:
|
|
if get_location() == "local":
|
|
return LocalMockProvider()
|
|
if find_spec("raisa_streamlit_oauth") is not None:
|
|
return RaisaProvider()
|
|
return OAuthFallbackProvider()
|
|
|