thomasht86 commited on
Commit
097ca00
·
verified ·
1 Parent(s): 39de730

deploy at 2024-08-25 08:30:53.354677

Browse files
Files changed (1) hide show
  1. main.py +10 -33
main.py CHANGED
@@ -121,13 +121,7 @@ login_redir = RedirectResponse("/login", status_code=303)
121
 
122
 
123
  def user_auth_before(req, sess):
124
- # The `auth` key in the request scope is automatically provided
125
- # to any handler which requests it, and can not be injected
126
- # by the user using query params, cookies, etc, so it should
127
- # be secure to use.
128
- print(f"Session Data before route: {sess}")
129
- auth = req.scope["auth"] = sess.get("auth", None)
130
-
131
 
132
 
133
  spinner_css = Style("""
@@ -184,24 +178,15 @@ class DebugSessionMiddleware(SessionMiddleware):
184
  # Generate a secure secret key
185
  SECRET_KEY = secrets.token_urlsafe(32)
186
 
187
- # Custom authentication backend
188
- class SimpleAuthBackend(AuthenticationBackend):
189
- async def authenticate(self, request):
190
- if "auth" not in request.session:
191
- return None
192
- return AuthCredentials(["authenticated"]), SimpleUser("admin")
193
-
194
-
195
  # Modify the middleware setup
196
  middlewares = [
197
  Middleware(
198
  SessionMiddleware,
199
  secret_key=SECRET_KEY,
200
  max_age=3600,
201
- same_site='Lax', # Try 'Lax' if 'None' doesn't work
202
- https_only=False, # Set to True if your site uses HTTPS
203
  ),
204
- Middleware(AuthenticationMiddleware, backend=SimpleAuthBackend()),
205
  ]
206
 
207
  bware = Beforeware(
@@ -211,24 +196,15 @@ bware = Beforeware(
211
  r"/static/.*",
212
  r".*\.css",
213
  r".*\.js",
214
- "/",
215
- "/login",
216
- "/search",
217
- "/document/.*",
218
- "/expand/.*",
219
- "/source",
220
- "/about",
221
- "/admin",
222
  ],
223
  )
224
 
225
  app, rt = fast_app(
226
- #before=bware,
227
  live=DEV_MODE,
228
  hdrs=headers,
229
  middleware=middlewares,
230
  key_fname=sess_key_path,
231
- same_site="None",
232
  )
233
 
234
  # Add this function for debugging
@@ -472,10 +448,10 @@ async def login(request: Request):
472
  # Set "set-cookie" in header
473
  response.set_cookie(
474
  "session",
475
- request.session["auth"],
476
  max_age=3600,
477
  httponly=False,
478
- samesite='none', # Try 'Lax' if 'None' doesn't work
479
  secure=True, # Set to True if your site uses HTTPS
480
  )
481
  return response
@@ -685,8 +661,9 @@ def download_csv(request: Request):
685
 
686
  @app.route("/admin")
687
  async def admin(request: Request):
688
- auth = request.cookies.get("session", None)
689
- if auth is None:
 
690
  return RedirectResponse("/login", status_code=303)
691
 
692
  page = int(request.query_params.get("page", 1))
@@ -765,7 +742,7 @@ async def admin(request: Request):
765
 
766
  return (
767
  Title("Admin"),
768
- get_navbar(request.user.is_authenticated),
769
  Main(
770
  Div(
771
  A(
 
121
 
122
 
123
  def user_auth_before(req, sess):
124
+ auth = req.scope["auth"] = sess.get("auth", False)
 
 
 
 
 
 
125
 
126
 
127
  spinner_css = Style("""
 
178
  # Generate a secure secret key
179
  SECRET_KEY = secrets.token_urlsafe(32)
180
 
 
 
 
 
 
 
 
 
181
  # Modify the middleware setup
182
  middlewares = [
183
  Middleware(
184
  SessionMiddleware,
185
  secret_key=SECRET_KEY,
186
  max_age=3600,
187
+ same_site='lax', # Try 'Lax' if 'None' doesn't work
188
+ https_only=True, # Set to True if your site uses HTTPS
189
  ),
 
190
  ]
191
 
192
  bware = Beforeware(
 
196
  r"/static/.*",
197
  r".*\.css",
198
  r".*\.js",
 
 
 
 
 
 
 
 
199
  ],
200
  )
201
 
202
  app, rt = fast_app(
203
+ before=bware,
204
  live=DEV_MODE,
205
  hdrs=headers,
206
  middleware=middlewares,
207
  key_fname=sess_key_path,
 
208
  )
209
 
210
  # Add this function for debugging
 
448
  # Set "set-cookie" in header
449
  response.set_cookie(
450
  "session",
451
+ request.session,
452
  max_age=3600,
453
  httponly=False,
454
+ samesite='none', # Try 'lax' if 'none' doesn't work
455
  secure=True, # Set to True if your site uses HTTPS
456
  )
457
  return response
 
661
 
662
  @app.route("/admin")
663
  async def admin(request: Request):
664
+ auth = request.session.get("auth", False)
665
+ if not auth:
666
+ print(f"Not authenticated: {auth}")
667
  return RedirectResponse("/login", status_code=303)
668
 
669
  page = int(request.query_params.get("page", 1))
 
742
 
743
  return (
744
  Title("Admin"),
745
+ get_navbar(auth),
746
  Main(
747
  Div(
748
  A(