Spaces:
Running
Running
Oviya
commited on
Commit
·
9fe9e53
1
Parent(s):
c7da6fa
Deploy: cookie fixes (Secure + None + Partitioned)
Browse files- verification.py +26 -11
verification.py
CHANGED
|
@@ -5,8 +5,6 @@ from dotenv import load_dotenv
|
|
| 5 |
BASEDIR = os.path.abspath(os.path.dirname(__file__))
|
| 6 |
load_dotenv(os.path.join(BASEDIR, ".env")) # loads DB_USER, DB_PASSWORD, RUN_INIT_DB
|
| 7 |
|
| 8 |
-
|
| 9 |
-
# import os
|
| 10 |
import logging
|
| 11 |
from threading import Lock
|
| 12 |
from functools import wraps
|
|
@@ -18,13 +16,16 @@ from flask import Flask, request, jsonify, make_response
|
|
| 18 |
from flask_cors import CORS
|
| 19 |
|
| 20 |
# ------------------------------------------------------------------------------
|
| 21 |
-
# App
|
| 22 |
# ------------------------------------------------------------------------------
|
| 23 |
app = Flask(__name__)
|
| 24 |
-
CORS(app, supports_credentials=True, origins=["http://localhost:4200"]) # add your prod origins later
|
| 25 |
app.config['SECRET_KEY'] = '96c63da06374c1bde332516f3acbd23c84f35f90d8a6321a25d790a0a451af32'
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
logging.basicConfig(level=logging.INFO)
|
| 29 |
|
| 30 |
# ------------------------------------------------------------------------------
|
|
@@ -34,10 +35,8 @@ DB_SERVER = "pykara-sqlserver.c5aosm6ie5j3.eu-north-1.rds.amazonaws.com,1433"
|
|
| 34 |
DB_DATABASE = "AuthenticationDB1"
|
| 35 |
|
| 36 |
if DB_SERVER.lower().startswith("localhost") or "\\" in DB_SERVER:
|
| 37 |
-
# Local Windows SQL Express with integrated auth
|
| 38 |
CONN_STR = f"DRIVER={{SQL Server}};SERVER={DB_SERVER};DATABASE={DB_DATABASE};Trusted_Connection=yes"
|
| 39 |
else:
|
| 40 |
-
# RDS / SQL login via env secrets
|
| 41 |
CONN_STR = (
|
| 42 |
"DRIVER={ODBC Driver 17 for SQL Server};"
|
| 43 |
f"SERVER={DB_SERVER};DATABASE={DB_DATABASE};"
|
|
@@ -112,7 +111,23 @@ def maybe_init_db():
|
|
| 112 |
_db_init_done = True
|
| 113 |
|
| 114 |
# ------------------------------------------------------------------------------
|
| 115 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
# ------------------------------------------------------------------------------
|
| 117 |
@app.get("/")
|
| 118 |
def health():
|
|
@@ -203,8 +218,8 @@ def login():
|
|
| 203 |
return jsonify({"message": "Database is unavailable"}), 503
|
| 204 |
|
| 205 |
resp = make_response(jsonify({"message": "Login successful"}))
|
| 206 |
-
resp
|
| 207 |
-
resp
|
| 208 |
return resp
|
| 209 |
|
| 210 |
@app.post("/refresh")
|
|
@@ -241,7 +256,7 @@ def refresh():
|
|
| 241 |
)
|
| 242 |
|
| 243 |
resp = make_response(jsonify({'access_token': new_access}))
|
| 244 |
-
resp
|
| 245 |
return resp
|
| 246 |
|
| 247 |
@app.post("/logout")
|
|
|
|
| 5 |
BASEDIR = os.path.abspath(os.path.dirname(__file__))
|
| 6 |
load_dotenv(os.path.join(BASEDIR, ".env")) # loads DB_USER, DB_PASSWORD, RUN_INIT_DB
|
| 7 |
|
|
|
|
|
|
|
| 8 |
import logging
|
| 9 |
from threading import Lock
|
| 10 |
from functools import wraps
|
|
|
|
| 16 |
from flask_cors import CORS
|
| 17 |
|
| 18 |
# ------------------------------------------------------------------------------
|
| 19 |
+
# App, ENV, CORS
|
| 20 |
# ------------------------------------------------------------------------------
|
| 21 |
app = Flask(__name__)
|
|
|
|
| 22 |
app.config['SECRET_KEY'] = '96c63da06374c1bde332516f3acbd23c84f35f90d8a6321a25d790a0a451af32'
|
| 23 |
|
| 24 |
+
IS_PROD = os.getenv("ENV", "dev").lower() == "prod"
|
| 25 |
+
_origins = os.getenv("ALLOWED_ORIGINS", "http://localhost:4200")
|
| 26 |
+
ALLOWED_ORIGINS = [o.strip() for o in _origins.split(",") if o.strip()]
|
| 27 |
+
CORS(app, supports_credentials=True, origins=ALLOWED_ORIGINS)
|
| 28 |
+
|
| 29 |
logging.basicConfig(level=logging.INFO)
|
| 30 |
|
| 31 |
# ------------------------------------------------------------------------------
|
|
|
|
| 35 |
DB_DATABASE = "AuthenticationDB1"
|
| 36 |
|
| 37 |
if DB_SERVER.lower().startswith("localhost") or "\\" in DB_SERVER:
|
|
|
|
| 38 |
CONN_STR = f"DRIVER={{SQL Server}};SERVER={DB_SERVER};DATABASE={DB_DATABASE};Trusted_Connection=yes"
|
| 39 |
else:
|
|
|
|
| 40 |
CONN_STR = (
|
| 41 |
"DRIVER={ODBC Driver 17 for SQL Server};"
|
| 42 |
f"SERVER={DB_SERVER};DATABASE={DB_DATABASE};"
|
|
|
|
| 111 |
_db_init_done = True
|
| 112 |
|
| 113 |
# ------------------------------------------------------------------------------
|
| 114 |
+
# Cookie helpers
|
| 115 |
+
# ------------------------------------------------------------------------------
|
| 116 |
+
def add_cookie(resp, name: str, value: str, max_age: int):
|
| 117 |
+
"""
|
| 118 |
+
In prod: Secure + SameSite=None + Partitioned (works with third-party cookie protections).
|
| 119 |
+
In dev: SameSite=Lax, not Secure.
|
| 120 |
+
"""
|
| 121 |
+
if IS_PROD:
|
| 122 |
+
resp.headers.add(
|
| 123 |
+
"Set-Cookie",
|
| 124 |
+
f"{name}={value}; Path=/; Max-Age={max_age}; Secure; HttpOnly; SameSite=None; Partitioned"
|
| 125 |
+
)
|
| 126 |
+
else:
|
| 127 |
+
resp.set_cookie(name, value, httponly=True, secure=False, samesite="Lax", max_age=max_age, path="/")
|
| 128 |
+
|
| 129 |
+
# ------------------------------------------------------------------------------
|
| 130 |
+
# Health
|
| 131 |
# ------------------------------------------------------------------------------
|
| 132 |
@app.get("/")
|
| 133 |
def health():
|
|
|
|
| 218 |
return jsonify({"message": "Database is unavailable"}), 503
|
| 219 |
|
| 220 |
resp = make_response(jsonify({"message": "Login successful"}))
|
| 221 |
+
add_cookie(resp, 'access_token', access_token, 900) # 15 min
|
| 222 |
+
add_cookie(resp, 'refresh_token', refresh_token, 7*24*60*60) # 7 days
|
| 223 |
return resp
|
| 224 |
|
| 225 |
@app.post("/refresh")
|
|
|
|
| 256 |
)
|
| 257 |
|
| 258 |
resp = make_response(jsonify({'access_token': new_access}))
|
| 259 |
+
add_cookie(resp, 'access_token', new_access, 900)
|
| 260 |
return resp
|
| 261 |
|
| 262 |
@app.post("/logout")
|