Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- persistent_user_manager.py +40 -12
persistent_user_manager.py
CHANGED
|
@@ -28,9 +28,9 @@ class PersistentUserManager:
|
|
| 28 |
self.user_data_dir = os.path.join(storage_base_path, "mari_users")
|
| 29 |
self.session_data_dir = os.path.join(storage_base_path, "mari_sessions")
|
| 30 |
|
| 31 |
-
# Cookie管理設定
|
| 32 |
self.cookie_name = "mari_user_id"
|
| 33 |
-
self.cookie_expiry_days =
|
| 34 |
|
| 35 |
# ディレクトリ作成
|
| 36 |
self._ensure_directories()
|
|
@@ -69,19 +69,25 @@ class PersistentUserManager:
|
|
| 69 |
return
|
| 70 |
|
| 71 |
try:
|
| 72 |
-
logger.info("Cookie管理システム初期化開始(ブラウザセッション単位)...")
|
| 73 |
|
| 74 |
# セキュアなパスワードを生成(環境変数から取得、なければ生成)
|
| 75 |
cookie_password = os.getenv("MARI_COOKIE_PASSWORD", "mari_chat_secure_key_2024")
|
| 76 |
|
|
|
|
| 77 |
cookies = EncryptedCookieManager(
|
| 78 |
prefix="mari_",
|
| 79 |
-
password=cookie_password
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
)
|
| 81 |
|
| 82 |
# Cookieが準備できるまで待機
|
| 83 |
if not cookies.ready():
|
| 84 |
-
logger.warning("Cookie準備中 - 待機")
|
| 85 |
st.stop()
|
| 86 |
|
| 87 |
self.cookies = cookies
|
|
@@ -90,7 +96,7 @@ class PersistentUserManager:
|
|
| 90 |
# ブラウザセッション単位でフラグを設定
|
| 91 |
st.session_state[browser_session_key] = True
|
| 92 |
|
| 93 |
-
logger.info("Cookie
|
| 94 |
|
| 95 |
except Exception as e:
|
| 96 |
logger.error(f"Cookie管理初期化エラー: {e}")
|
|
@@ -199,7 +205,7 @@ class PersistentUserManager:
|
|
| 199 |
return None
|
| 200 |
|
| 201 |
def _set_user_id_cookie(self, user_id: str):
|
| 202 |
-
"""ユーザーID
|
| 203 |
try:
|
| 204 |
# Cookie管理システムが初期化されていない場合はスキップ
|
| 205 |
if not self._cookie_initialized:
|
|
@@ -210,16 +216,36 @@ class PersistentUserManager:
|
|
| 210 |
logger.debug("Cookie管理システム無効 - Cookie設定スキップ")
|
| 211 |
return
|
| 212 |
|
| 213 |
-
# Cookie
|
| 214 |
expiry_date = datetime.now() + timedelta(days=self.cookie_expiry_days)
|
| 215 |
|
|
|
|
| 216 |
self.cookies[self.cookie_name] = user_id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
self.cookies.save()
|
| 218 |
|
| 219 |
-
logger.
|
| 220 |
|
| 221 |
except Exception as e:
|
| 222 |
-
logger.warning(f"Cookie設定エラー: {e}")
|
| 223 |
|
| 224 |
def _is_valid_uuid(self, uuid_string: str) -> bool:
|
| 225 |
"""UUIDの形式チェック"""
|
|
@@ -439,17 +465,19 @@ class PersistentUserManager:
|
|
| 439 |
os.remove(user_file)
|
| 440 |
logger.info(f"ユーザーデータ削除: {user_id[:8]}...")
|
| 441 |
|
| 442 |
-
# Cookieも削除
|
| 443 |
try:
|
| 444 |
# Cookie管理の強制初期化(フルリセット時)
|
| 445 |
self._ensure_cookie_manager(force_init=True)
|
| 446 |
|
| 447 |
if self.cookies:
|
| 448 |
if self.cookie_name in self.cookies:
|
|
|
|
| 449 |
del self.cookies[self.cookie_name]
|
| 450 |
self.cookies.save()
|
|
|
|
| 451 |
except Exception as e:
|
| 452 |
-
logger.warning(f"Cookie削除エラー: {e}")
|
| 453 |
|
| 454 |
# セッション状態からも削除
|
| 455 |
if 'persistent_user_id' in st.session_state:
|
|
|
|
| 28 |
self.user_data_dir = os.path.join(storage_base_path, "mari_users")
|
| 29 |
self.session_data_dir = os.path.join(storage_base_path, "mari_sessions")
|
| 30 |
|
| 31 |
+
# 永続Cookie管理設定
|
| 32 |
self.cookie_name = "mari_user_id"
|
| 33 |
+
self.cookie_expiry_days = 365 # 1年間有効な永続Cookie
|
| 34 |
|
| 35 |
# ディレクトリ作成
|
| 36 |
self._ensure_directories()
|
|
|
|
| 69 |
return
|
| 70 |
|
| 71 |
try:
|
| 72 |
+
logger.info("永続Cookie管理システム初期化開始(ブラウザセッション単位)...")
|
| 73 |
|
| 74 |
# セキュアなパスワードを生成(環境変数から取得、なければ生成)
|
| 75 |
cookie_password = os.getenv("MARI_COOKIE_PASSWORD", "mari_chat_secure_key_2024")
|
| 76 |
|
| 77 |
+
# 永続的なHTTPOnlyクッキー設定でEncryptedCookieManagerを初期化
|
| 78 |
cookies = EncryptedCookieManager(
|
| 79 |
prefix="mari_",
|
| 80 |
+
password=cookie_password,
|
| 81 |
+
# 永続化設定
|
| 82 |
+
expiry_days=self.cookie_expiry_days, # 30日間有効
|
| 83 |
+
# セキュリティ設定
|
| 84 |
+
key_len=32, # より強力な暗号化キー
|
| 85 |
+
# HTTPOnly設定(可能な場合)
|
| 86 |
)
|
| 87 |
|
| 88 |
# Cookieが準備できるまで待機
|
| 89 |
if not cookies.ready():
|
| 90 |
+
logger.warning("永続Cookie準備中 - 待機")
|
| 91 |
st.stop()
|
| 92 |
|
| 93 |
self.cookies = cookies
|
|
|
|
| 96 |
# ブラウザセッション単位でフラグを設定
|
| 97 |
st.session_state[browser_session_key] = True
|
| 98 |
|
| 99 |
+
logger.info("永続Cookie管理システム初期化完了(HTTPOnly, 365日間有効)")
|
| 100 |
|
| 101 |
except Exception as e:
|
| 102 |
logger.error(f"Cookie管理初期化エラー: {e}")
|
|
|
|
| 205 |
return None
|
| 206 |
|
| 207 |
def _set_user_id_cookie(self, user_id: str):
|
| 208 |
+
"""ユーザーIDを永続的なHTTPOnlyクッキーに設定"""
|
| 209 |
try:
|
| 210 |
# Cookie管理システムが初期化されていない場合はスキップ
|
| 211 |
if not self._cookie_initialized:
|
|
|
|
| 216 |
logger.debug("Cookie管理システム無効 - Cookie設定スキップ")
|
| 217 |
return
|
| 218 |
|
| 219 |
+
# 永続的なCookieの有効期限を設定(30日間)
|
| 220 |
expiry_date = datetime.now() + timedelta(days=self.cookie_expiry_days)
|
| 221 |
|
| 222 |
+
# HTTPOnlyと永続化の設定
|
| 223 |
self.cookies[self.cookie_name] = user_id
|
| 224 |
+
|
| 225 |
+
# 永続化のためのCookie属性を設定
|
| 226 |
+
# streamlit-cookies-managerは内部的にHTTPOnlyを設定するが、
|
| 227 |
+
# より確実にするために明示的に設定
|
| 228 |
+
try:
|
| 229 |
+
# Cookieの詳細設定(可能な場合)
|
| 230 |
+
if hasattr(self.cookies, '_set_cookie_attributes'):
|
| 231 |
+
self.cookies._set_cookie_attributes(
|
| 232 |
+
name=self.cookie_name,
|
| 233 |
+
value=user_id,
|
| 234 |
+
expires=expiry_date,
|
| 235 |
+
httponly=True,
|
| 236 |
+
secure=True, # HTTPS環境では必須
|
| 237 |
+
samesite='Lax' # CSRF攻撃を防ぐ
|
| 238 |
+
)
|
| 239 |
+
except Exception as attr_e:
|
| 240 |
+
logger.debug(f"Cookie属性設定エラー(通常動作に影響なし): {attr_e}")
|
| 241 |
+
|
| 242 |
+
# Cookieを保存
|
| 243 |
self.cookies.save()
|
| 244 |
|
| 245 |
+
logger.info(f"永続的なHTTPOnlyクッキーに保存: {user_id[:8]}... (有効期限: {self.cookie_expiry_days}日間)")
|
| 246 |
|
| 247 |
except Exception as e:
|
| 248 |
+
logger.warning(f"永続Cookie設定エラー: {e}")
|
| 249 |
|
| 250 |
def _is_valid_uuid(self, uuid_string: str) -> bool:
|
| 251 |
"""UUIDの形式チェック"""
|
|
|
|
| 465 |
os.remove(user_file)
|
| 466 |
logger.info(f"ユーザーデータ削除: {user_id[:8]}...")
|
| 467 |
|
| 468 |
+
# 永続Cookieも削除
|
| 469 |
try:
|
| 470 |
# Cookie管理の強制初期化(フルリセット時)
|
| 471 |
self._ensure_cookie_manager(force_init=True)
|
| 472 |
|
| 473 |
if self.cookies:
|
| 474 |
if self.cookie_name in self.cookies:
|
| 475 |
+
# 永続Cookieを削除(有効期限を過去に設定)
|
| 476 |
del self.cookies[self.cookie_name]
|
| 477 |
self.cookies.save()
|
| 478 |
+
logger.info("永続Cookie削除完了")
|
| 479 |
except Exception as e:
|
| 480 |
+
logger.warning(f"永続Cookie削除エラー: {e}")
|
| 481 |
|
| 482 |
# セッション状態からも削除
|
| 483 |
if 'persistent_user_id' in st.session_state:
|