Я создал переменную user_details = { вне серверной функции Shiny и назначил ему детали сеанса, и он работал нормально. Проблема в том, что эти данные останутся на сервере, когда каждый пользователь войдет в систему. Когда я и мой друг вошли на веб-страницу после завершения моего единого входа, он увидел мое имя в своем сеансе.
После небольшого исследования я узнал, что мне нужно поместить переменную user_details внутри функции сервера в Shiny, но после этого я не могу присвоить этой переменной данные пользователя сеанса.
Код:
Код: Выделить всё
def server(input, output, session):
user_details = session.user
print(user_details)
shiny_app = App(app_ui, server)
#----------------------------------------------------------------------END OF SHINY-----------------------------------------------------
#---------------------------------------------------------------------START OF SSO-------------------------------------------------------
config = Config('.env')
oauth = OAuth()
CONF_URL = #config url
oauth.register(
name = "app",
server_metadata_url = CONF_URL,
client_id = config.get('client_id'),
client_secret = config.get('client_secret'),
client_kwargs={
'scope': 'openid email'
}
)
random_secret_key = secrets.token_urlsafe(32)
class AuthMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next):
# Check if the user is authenticated
user = request.session.get("user")
# List of paths that require authentication
protected_paths = ["/dash", "/dash/"]
# If the path is protected and the user is not authenticated
if request.url.path in protected_paths and not user:
#Redirect to the login page or SSO login flow
return RedirectResponse(url="/login")
# Proceed with the next middleware or route handler
response = await call_next(request)
return response
async def homepage(request):
global user_details
user = request.session.get('user')
if user:
return RedirectResponse(url='/dash')
#return JSONResponse(user)
return RedirectResponse(url = '/login')
async def login(request):
redirect_uri = request.url_for('auth')
return await oauth.app.authorize_redirect(request, redirect_uri)
async def auth(request):
token = await oauth.app.authorize_access_token(request)
user = token.get('userinfo')
if user:
request.session['user'] = user
return RedirectResponse(url='/')
async def healthcheck(request):
return JSONResponse({"Status" : "running"}, headers = {"Cache-Control" : "max-age=0, no-cache, no-store"})
routes = [
Route('/health/ping', healthcheck),
Route('/', homepage),
Route('/login', login),
Route('/auth', auth),
Mount('/Static', StaticFiles(directory = 'www'), name = 'static'),
Mount('/dash', app = shiny_app)
]
app = Starlette(routes=routes)
app.add_middleware(AuthMiddleware)
app.add_middleware(SessionMiddleware, secret_key = random_secret_key)
Подробнее здесь: https://stackoverflow.com/questions/790 ... for-python
Мобильная версия