Christian Kniep commited on
Commit
a70a19f
·
1 Parent(s): a6e79df

fix OAUTH 'in' HF v3

Browse files
Files changed (2) hide show
  1. src/app.py +12 -1
  2. src/routes/auth.py +16 -4
src/app.py CHANGED
@@ -67,7 +67,8 @@ def create_app():
67
 
68
  # Session cookie configuration
69
  # For HTTPS (HF Spaces): Set SESSION_COOKIE_SECURE=true
70
- app.config["SESSION_COOKIE_SECURE"] = os.getenv("SESSION_COOKIE_SECURE", "False") == "True"
 
71
  app.config["SESSION_COOKIE_HTTPONLY"] = (
72
  os.getenv("SESSION_COOKIE_HTTPONLY", "True") == "True"
73
  )
@@ -76,10 +77,20 @@ def create_app():
76
  app.config["SESSION_COOKIE_SAMESITE"] = None if samesite_value == "None" else samesite_value
77
  app.config["SESSION_COOKIE_NAME"] = "prepmate_session" # Explicit session cookie name
78
  # Don't set SESSION_COOKIE_DOMAIN - let Flask handle it automatically
 
79
  app.config["PERMANENT_SESSION_LIFETIME"] = timedelta(
80
  seconds=int(os.getenv("PERMANENT_SESSION_LIFETIME", "2592000"))
81
  )
82
 
 
 
 
 
 
 
 
 
 
83
  # Authlib configuration for OAuth state management
84
  app.config["AUTHLIB_INSECURE_TRANSPORT"] = os.getenv("FLASK_ENV") == "development"
85
 
 
67
 
68
  # Session cookie configuration
69
  # For HTTPS (HF Spaces): Set SESSION_COOKIE_SECURE=true
70
+ secure_cookie = os.getenv("SESSION_COOKIE_SECURE", "False") == "True"
71
+ app.config["SESSION_COOKIE_SECURE"] = secure_cookie
72
  app.config["SESSION_COOKIE_HTTPONLY"] = (
73
  os.getenv("SESSION_COOKIE_HTTPONLY", "True") == "True"
74
  )
 
77
  app.config["SESSION_COOKIE_SAMESITE"] = None if samesite_value == "None" else samesite_value
78
  app.config["SESSION_COOKIE_NAME"] = "prepmate_session" # Explicit session cookie name
79
  # Don't set SESSION_COOKIE_DOMAIN - let Flask handle it automatically
80
+ app.config["SESSION_COOKIE_PATH"] = "/" # Ensure cookie is valid for all paths
81
  app.config["PERMANENT_SESSION_LIFETIME"] = timedelta(
82
  seconds=int(os.getenv("PERMANENT_SESSION_LIFETIME", "2592000"))
83
  )
84
 
85
+ # Log session configuration for debugging
86
+ print(f"[CONFIG] SESSION_COOKIE_SECURE={secure_cookie}")
87
+ print(f"[CONFIG] SESSION_COOKIE_SAMESITE={app.config['SESSION_COOKIE_SAMESITE']}")
88
+ print(f"[CONFIG] SESSION_COOKIE_NAME={app.config['SESSION_COOKIE_NAME']}")
89
+ print(f"[CONFIG] PERMANENT_SESSION_LIFETIME={app.config['PERMANENT_SESSION_LIFETIME']}")
90
+ print(f"[CONFIG] SESSION_COOKIE_SECURE={secure_cookie}")
91
+ print(f"[CONFIG] SESSION_COOKIE_SAMESITE={app.config['SESSION_COOKIE_SAMESITE']}")
92
+ print(f"[CONFIG] SESSION_COOKIE_NAME={app.config['SESSION_COOKIE_NAME']}")
93
+
94
  # Authlib configuration for OAuth state management
95
  app.config["AUTHLIB_INSECURE_TRANSPORT"] = os.getenv("FLASK_ENV") == "development"
96
 
src/routes/auth.py CHANGED
@@ -30,15 +30,25 @@ def login():
30
  logger.info(f"[OAUTH] Request host: {request.host}")
31
  logger.info(f"[OAUTH] Request scheme: {request.scheme}")
32
 
 
 
 
 
 
 
 
33
  # Mark session as modified to ensure it's saved before redirect
34
  session.modified = True
35
 
36
- # Store redirect_uri in session for callback verification
37
- session["oauth_redirect_uri"] = redirect_uri
38
 
39
  response = auth_service.hf.authorize_redirect(redirect_uri)
40
  logger.info(f"[OAUTH] Redirecting to OAuth provider, response location: {response.location if hasattr(response, 'location') else 'N/A'}")
41
 
 
 
 
42
  return response
43
 
44
 
@@ -57,10 +67,12 @@ def callback():
57
  logger.info(f"[OAUTH] Request args: {dict(request.args)}")
58
 
59
  try:
60
- # Debug session state
 
 
61
  logger.info(f"[OAUTH] Session keys: {list(session.keys())}")
 
62
  logger.info(f"[OAUTH] OAuth state in session: {session.get('_state_huggingface')}")
63
- logger.info(f"[OAUTH] Cookies received: {list(request.cookies.keys())}")
64
 
65
  # Exchange authorization code for access token
66
  # Authlib automatically validates the state and uses the redirect_uri from the session
 
30
  logger.info(f"[OAUTH] Request host: {request.host}")
31
  logger.info(f"[OAUTH] Request scheme: {request.scheme}")
32
 
33
+ # Store redirect_uri in session for callback verification
34
+ session["oauth_redirect_uri"] = redirect_uri
35
+ session["test_marker"] = "login_triggered" # Test value to verify session persistence
36
+
37
+ # Make session permanent to ensure cookie gets set with proper expiry
38
+ session.permanent = True
39
+
40
  # Mark session as modified to ensure it's saved before redirect
41
  session.modified = True
42
 
43
+ logger.info(f"[OAUTH] Session data stored: oauth_redirect_uri={redirect_uri}")
44
+ logger.info(f"[OAUTH] Session keys after storage: {list(session.keys())}")
45
 
46
  response = auth_service.hf.authorize_redirect(redirect_uri)
47
  logger.info(f"[OAUTH] Redirecting to OAuth provider, response location: {response.location if hasattr(response, 'location') else 'N/A'}")
48
 
49
+ # Log set cookies to verify session cookie is being sent
50
+ logger.info(f"[OAUTH] Response cookies: {response.headers.getlist('Set-Cookie')}")
51
+
52
  return response
53
 
54
 
 
67
  logger.info(f"[OAUTH] Request args: {dict(request.args)}")
68
 
69
  try:
70
+ # Debug session state and cookies
71
+ logger.info(f"[OAUTH] Cookies received: {list(request.cookies.keys())}")
72
+ logger.info(f"[OAUTH] Cookie values (redacted): {[(k, v[:20] + '...' if len(v) > 20 else v) for k, v in request.cookies.items()]}")
73
  logger.info(f"[OAUTH] Session keys: {list(session.keys())}")
74
+ logger.info(f"[OAUTH] Test marker from login: {session.get('test_marker')}")
75
  logger.info(f"[OAUTH] OAuth state in session: {session.get('_state_huggingface')}")
 
76
 
77
  # Exchange authorization code for access token
78
  # Authlib automatically validates the state and uses the redirect_uri from the session