auth.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. from datetime import timedelta
  2. from typing import Annotated
  3. from fastapi import APIRouter, Depends, Header, HTTPException, status
  4. from fastapi.security import HTTPAuthorizationCredentials
  5. from sqlalchemy import select
  6. from sqlalchemy.ext.asyncio import AsyncSession
  7. from sqlalchemy.orm import selectinload
  8. from backend.app.api.routes.settings import get_external_login_url
  9. from backend.app.core.auth import (
  10. ACCESS_TOKEN_EXPIRE_MINUTES,
  11. ALGORITHM,
  12. SECRET_KEY,
  13. Permission,
  14. RequirePermissionIfAuthEnabled,
  15. _validate_api_key,
  16. authenticate_user,
  17. authenticate_user_by_email,
  18. create_access_token,
  19. get_current_active_user,
  20. get_password_hash,
  21. get_user_by_email,
  22. get_user_by_username,
  23. security,
  24. )
  25. from backend.app.core.database import get_db
  26. from backend.app.core.permissions import ALL_PERMISSIONS
  27. from backend.app.models.group import Group
  28. from backend.app.models.settings import Settings
  29. from backend.app.models.user import User
  30. from backend.app.schemas.auth import (
  31. ForgotPasswordRequest,
  32. ForgotPasswordResponse,
  33. GroupBrief,
  34. LoginRequest,
  35. LoginResponse,
  36. ResetPasswordRequest,
  37. ResetPasswordResponse,
  38. SetupRequest,
  39. SetupResponse,
  40. SMTPSettings,
  41. TestSMTPRequest,
  42. TestSMTPResponse,
  43. UserResponse,
  44. )
  45. from backend.app.services.email_service import (
  46. create_password_reset_email_from_template,
  47. generate_secure_password,
  48. get_smtp_settings,
  49. save_smtp_settings,
  50. send_email,
  51. )
  52. def _user_to_response(user: User) -> UserResponse:
  53. """Convert a User model to UserResponse schema."""
  54. return UserResponse(
  55. id=user.id,
  56. username=user.username,
  57. email=user.email,
  58. role=user.role,
  59. is_active=user.is_active,
  60. is_admin=user.is_admin,
  61. auth_source=getattr(user, "auth_source", "local"),
  62. groups=[GroupBrief(id=g.id, name=g.name) for g in user.groups],
  63. permissions=sorted(user.get_permissions()),
  64. created_at=user.created_at.isoformat(),
  65. )
  66. def _api_key_to_user_response(api_key) -> UserResponse:
  67. """Create a synthetic admin UserResponse for a valid API key."""
  68. return UserResponse(
  69. id=0,
  70. username=f"api-key:{api_key.key_prefix}",
  71. email=None,
  72. role="admin",
  73. is_active=True,
  74. is_admin=True,
  75. groups=[],
  76. permissions=sorted(ALL_PERMISSIONS),
  77. created_at=api_key.created_at.isoformat(),
  78. )
  79. router = APIRouter(prefix="/auth", tags=["authentication"])
  80. async def is_auth_enabled(db: AsyncSession) -> bool:
  81. """Check if authentication is enabled."""
  82. result = await db.execute(select(Settings).where(Settings.key == "auth_enabled"))
  83. setting = result.scalar_one_or_none()
  84. if setting is None:
  85. return False
  86. return setting.value.lower() == "true"
  87. async def is_advanced_auth_enabled(db: AsyncSession) -> bool:
  88. """Check if advanced authentication is enabled."""
  89. result = await db.execute(select(Settings).where(Settings.key == "advanced_auth_enabled"))
  90. setting = result.scalar_one_or_none()
  91. if setting is None:
  92. return False
  93. return setting.value.lower() == "true"
  94. async def set_advanced_auth_enabled(db: AsyncSession, enabled: bool) -> None:
  95. """Set advanced authentication enabled status."""
  96. from backend.app.core.db_dialect import upsert_setting
  97. await upsert_setting(db, Settings, "advanced_auth_enabled", "true" if enabled else "false")
  98. async def set_auth_enabled(db: AsyncSession, enabled: bool) -> None:
  99. """Set authentication enabled status."""
  100. from backend.app.core.db_dialect import upsert_setting
  101. await upsert_setting(db, Settings, "auth_enabled", "true" if enabled else "false")
  102. # Note: Don't commit here - let get_db handle it or commit explicitly in the route
  103. async def is_setup_completed(db: AsyncSession) -> bool:
  104. """Check if setup has been completed."""
  105. result = await db.execute(select(Settings).where(Settings.key == "setup_completed"))
  106. setting = result.scalar_one_or_none()
  107. return setting and setting.value.lower() == "true"
  108. async def set_setup_completed(db: AsyncSession, completed: bool) -> None:
  109. """Set setup completed status."""
  110. from backend.app.core.db_dialect import upsert_setting
  111. await upsert_setting(db, Settings, "setup_completed", "true" if completed else "false")
  112. # Note: Don't commit here - let get_db handle it or commit explicitly in the route
  113. @router.post("/setup", response_model=SetupResponse)
  114. async def setup_auth(request: SetupRequest, db: AsyncSession = Depends(get_db)):
  115. """First-time setup: enable/disable authentication and create admin user."""
  116. import logging
  117. logger = logging.getLogger(__name__)
  118. try:
  119. # If auth is currently enabled, block unauthenticated setup changes.
  120. # Use the admin panel (/disable endpoint) to modify auth when it's already on.
  121. if await is_auth_enabled(db):
  122. raise HTTPException(
  123. status_code=status.HTTP_403_FORBIDDEN,
  124. detail="Authentication is already configured. Use the admin panel to modify auth settings.",
  125. )
  126. admin_created = False
  127. if request.auth_enabled:
  128. # Check if admin users already exist
  129. admin_users_result = await db.execute(select(User).where(User.role == "admin"))
  130. existing_admin_users = list(admin_users_result.scalars().all())
  131. has_admin_users = len(existing_admin_users) > 0
  132. if has_admin_users:
  133. # Admin users already exist, just enable auth (don't create new admin)
  134. logger.info(
  135. f"Admin users already exist ({len(existing_admin_users)} found), enabling authentication without creating new admin"
  136. )
  137. admin_created = False
  138. else:
  139. # No admin users exist, require admin credentials to create first admin
  140. if not request.admin_username or not request.admin_password:
  141. raise HTTPException(
  142. status_code=status.HTTP_400_BAD_REQUEST,
  143. detail="Admin username and password are required when enabling authentication (no admin users exist)",
  144. )
  145. # Check if username already exists (shouldn't happen if no admin users exist, but check anyway)
  146. existing_user = await get_user_by_username(db, request.admin_username)
  147. if existing_user:
  148. raise HTTPException(
  149. status_code=status.HTTP_400_BAD_REQUEST,
  150. detail="User with this username already exists",
  151. )
  152. # Create admin user FIRST (before enabling auth)
  153. try:
  154. logger.info("Creating admin user: %s", request.admin_username)
  155. admin_user = User(
  156. username=request.admin_username,
  157. password_hash=get_password_hash(request.admin_password),
  158. role="admin",
  159. is_active=True,
  160. )
  161. # Try to add user to Administrators group if it exists
  162. admin_group_result = await db.execute(select(Group).where(Group.name == "Administrators"))
  163. admin_group = admin_group_result.scalar_one_or_none()
  164. if admin_group:
  165. admin_user.groups.append(admin_group)
  166. logger.info("Added new admin user to Administrators group")
  167. db.add(admin_user)
  168. logger.info("Admin user added to session: %s", request.admin_username)
  169. admin_created = True
  170. except Exception as e:
  171. await db.rollback()
  172. logger.error("Failed to create admin user: %s", e, exc_info=True)
  173. raise HTTPException(
  174. status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
  175. detail=f"Failed to create admin user: {str(e)}",
  176. )
  177. # Set auth enabled and mark setup as completed
  178. await set_auth_enabled(db, request.auth_enabled)
  179. await set_setup_completed(db, True)
  180. await db.commit()
  181. if admin_created:
  182. await db.refresh(admin_user)
  183. logger.info("Admin user created successfully: %s", admin_user.id)
  184. logger.info("Setup completed: auth_enabled=%s, admin_created=%s", request.auth_enabled, admin_created)
  185. return SetupResponse(auth_enabled=request.auth_enabled, admin_created=admin_created)
  186. except HTTPException:
  187. raise
  188. except Exception as e:
  189. logger.error("Setup error: %s", e, exc_info=True)
  190. await db.rollback()
  191. raise HTTPException(
  192. status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
  193. detail=f"Setup failed: {str(e)}",
  194. )
  195. @router.get("/status")
  196. async def get_auth_status(db: AsyncSession = Depends(get_db)):
  197. """Get authentication status (public endpoint)."""
  198. auth_enabled = await is_auth_enabled(db)
  199. setup_completed = await is_setup_completed(db)
  200. # Only require setup if it hasn't been completed yet
  201. requires_setup = not setup_completed
  202. return {"auth_enabled": auth_enabled, "requires_setup": requires_setup}
  203. @router.post("/disable", response_model=dict)
  204. async def disable_auth(
  205. current_user: User = Depends(get_current_active_user),
  206. db: AsyncSession = Depends(get_db),
  207. ):
  208. """Disable authentication (admin only)."""
  209. import logging
  210. logger = logging.getLogger(__name__)
  211. # Reload user with groups for proper is_admin check
  212. result = await db.execute(select(User).where(User.id == current_user.id).options(selectinload(User.groups)))
  213. user = result.scalar_one()
  214. # Only admins can disable authentication
  215. if not user.is_admin:
  216. raise HTTPException(
  217. status_code=status.HTTP_403_FORBIDDEN,
  218. detail="Only admins can disable authentication",
  219. )
  220. try:
  221. await set_auth_enabled(db, False)
  222. await db.commit()
  223. logger.info("Authentication disabled by admin user: %s", user.username)
  224. return {"message": "Authentication disabled successfully", "auth_enabled": False}
  225. except Exception as e:
  226. await db.rollback()
  227. logger.error("Failed to disable authentication: %s", e, exc_info=True)
  228. raise HTTPException(
  229. status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
  230. detail=f"Failed to disable authentication: {str(e)}",
  231. )
  232. @router.post("/login", response_model=LoginResponse)
  233. async def login(request: LoginRequest, db: AsyncSession = Depends(get_db)):
  234. """Login and get access token.
  235. Supports username or email-based login. Username lookup is case-insensitive.
  236. """
  237. # Check if auth is enabled
  238. auth_enabled = await is_auth_enabled(db)
  239. if not auth_enabled:
  240. raise HTTPException(
  241. status_code=status.HTTP_400_BAD_REQUEST,
  242. detail="Authentication is not enabled",
  243. )
  244. # Check if LDAP is enabled
  245. ldap_user = None
  246. ldap_settings = await _get_ldap_settings(db)
  247. if ldap_settings:
  248. try:
  249. from backend.app.services.ldap_service import (
  250. authenticate_ldap_user,
  251. parse_ldap_config,
  252. )
  253. ldap_config = parse_ldap_config(ldap_settings)
  254. if ldap_config:
  255. ldap_user = authenticate_ldap_user(ldap_config, request.username, request.password)
  256. if ldap_user:
  257. # LDAP auth succeeded — find or create local user
  258. user = await get_user_by_username(db, ldap_user.username)
  259. if user and user.auth_source != "ldap":
  260. # Username exists as local user — don't override
  261. user = None
  262. ldap_user = None
  263. elif not user:
  264. if not ldap_config.auto_provision:
  265. # User doesn't exist and auto-provision is off
  266. ldap_user = None
  267. else:
  268. # Auto-provision LDAP user
  269. user = await _provision_ldap_user(db, ldap_user, ldap_config)
  270. if user and ldap_user:
  271. # Update email and group mappings on each login
  272. await _sync_ldap_user(db, user, ldap_user, ldap_config)
  273. except Exception as e:
  274. import logging
  275. logging.getLogger(__name__).warning("LDAP authentication error, falling back to local: %s", e)
  276. ldap_user = None
  277. # Try username-based authentication (skip if already authenticated via LDAP)
  278. if not ldap_user:
  279. user = await authenticate_user(db, request.username, request.password)
  280. # If username auth failed and advanced auth is enabled, try email-based authentication
  281. if not user and not ldap_user:
  282. advanced_auth = await is_advanced_auth_enabled(db)
  283. if advanced_auth:
  284. user = await authenticate_user_by_email(db, request.username, request.password)
  285. if not user:
  286. raise HTTPException(
  287. status_code=status.HTTP_401_UNAUTHORIZED,
  288. detail="Incorrect username or password",
  289. headers={"WWW-Authenticate": "Bearer"},
  290. )
  291. # Reload user with groups for proper permission calculation
  292. result = await db.execute(select(User).where(User.id == user.id).options(selectinload(User.groups)))
  293. user = result.scalar_one()
  294. access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
  295. access_token = create_access_token(data={"sub": user.username}, expires_delta=access_token_expires)
  296. return LoginResponse(
  297. access_token=access_token,
  298. token_type="bearer",
  299. user=_user_to_response(user),
  300. )
  301. @router.get("/me", response_model=UserResponse)
  302. async def get_current_user_info(
  303. credentials: Annotated[HTTPAuthorizationCredentials | None, Depends(security)] = None,
  304. x_api_key: Annotated[str | None, Header(alias="X-API-Key")] = None,
  305. db: AsyncSession = Depends(get_db),
  306. ):
  307. """Get current user information.
  308. Accepts JWT tokens (via Authorization: Bearer header) and API keys
  309. (via X-API-Key header or Authorization: Bearer bb_xxx).
  310. API keys return a synthetic admin user with all permissions.
  311. """
  312. import jwt
  313. from jwt.exceptions import PyJWTError as JWTError
  314. # Check for API key via X-API-Key header
  315. if x_api_key:
  316. api_key = await _validate_api_key(db, x_api_key)
  317. if api_key:
  318. return _api_key_to_user_response(api_key)
  319. # Check for Bearer token (could be JWT or API key)
  320. if credentials is not None:
  321. token = credentials.credentials
  322. # Check if it's an API key (starts with bb_)
  323. if token.startswith("bb_"):
  324. api_key = await _validate_api_key(db, token)
  325. if api_key:
  326. return _api_key_to_user_response(api_key)
  327. raise HTTPException(
  328. status_code=status.HTTP_401_UNAUTHORIZED,
  329. detail="Invalid API key",
  330. headers={"WWW-Authenticate": "Bearer"},
  331. )
  332. # Otherwise treat as JWT
  333. try:
  334. payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
  335. username: str = payload.get("sub")
  336. if username is None:
  337. raise HTTPException(
  338. status_code=status.HTTP_401_UNAUTHORIZED,
  339. detail="Could not validate credentials",
  340. headers={"WWW-Authenticate": "Bearer"},
  341. )
  342. except JWTError:
  343. raise HTTPException(
  344. status_code=status.HTTP_401_UNAUTHORIZED,
  345. detail="Could not validate credentials",
  346. headers={"WWW-Authenticate": "Bearer"},
  347. )
  348. user = await get_user_by_username(db, username)
  349. if user is None or not user.is_active:
  350. raise HTTPException(
  351. status_code=status.HTTP_401_UNAUTHORIZED,
  352. detail="Could not validate credentials",
  353. headers={"WWW-Authenticate": "Bearer"},
  354. )
  355. # Reload with groups for proper permission calculation
  356. result = await db.execute(select(User).where(User.id == user.id).options(selectinload(User.groups)))
  357. user = result.scalar_one()
  358. return _user_to_response(user)
  359. # No credentials provided
  360. raise HTTPException(
  361. status_code=status.HTTP_401_UNAUTHORIZED,
  362. detail="Authentication required",
  363. headers={"WWW-Authenticate": "Bearer"},
  364. )
  365. @router.post("/logout")
  366. async def logout():
  367. """Logout (client should discard token)."""
  368. return {"message": "Logged out successfully"}
  369. # Advanced Authentication Endpoints
  370. @router.post("/smtp/test", response_model=TestSMTPResponse)
  371. async def test_smtp_connection(
  372. test_request: TestSMTPRequest,
  373. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
  374. db: AsyncSession = Depends(get_db),
  375. ):
  376. """Test SMTP connection using saved settings (admin only when auth enabled)."""
  377. import logging
  378. logger = logging.getLogger(__name__)
  379. try:
  380. smtp_settings = await get_smtp_settings(db)
  381. if not smtp_settings:
  382. return TestSMTPResponse(success=False, message="SMTP settings not configured. Save SMTP settings first.")
  383. # Send test email
  384. send_email(
  385. smtp_settings=smtp_settings,
  386. to_email=test_request.test_recipient,
  387. subject="BamBuddy SMTP Test",
  388. body_text="This is a test email from BamBuddy. If you received this, your SMTP settings are working correctly!",
  389. body_html="<p>This is a test email from <strong>BamBuddy</strong>.</p><p>If you received this, your SMTP settings are working correctly!</p>",
  390. )
  391. logger.info(f"Test email sent successfully to {test_request.test_recipient}")
  392. return TestSMTPResponse(success=True, message="Test email sent successfully")
  393. except Exception as e:
  394. logger.error(f"Failed to send test email: {e}")
  395. return TestSMTPResponse(success=False, message=f"Failed to send test email: {str(e)}")
  396. @router.get("/smtp", response_model=SMTPSettings | None)
  397. async def get_smtp_config(
  398. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_READ),
  399. db: AsyncSession = Depends(get_db),
  400. ):
  401. """Get SMTP settings (admin only when auth enabled). Password is not returned."""
  402. smtp_settings = await get_smtp_settings(db)
  403. if smtp_settings:
  404. # Don't return password in response
  405. smtp_settings.smtp_password = None
  406. return smtp_settings
  407. @router.post("/smtp", response_model=dict)
  408. async def save_smtp_config(
  409. smtp_settings: SMTPSettings,
  410. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
  411. db: AsyncSession = Depends(get_db),
  412. ):
  413. """Save SMTP settings (admin only when auth enabled)."""
  414. import logging
  415. logger = logging.getLogger(__name__)
  416. try:
  417. await save_smtp_settings(db, smtp_settings)
  418. await db.commit()
  419. logger.info(f"SMTP settings updated by admin user: {current_user.username if current_user else 'anonymous'}")
  420. return {"message": "SMTP settings saved successfully"}
  421. except Exception as e:
  422. await db.rollback()
  423. logger.error(f"Failed to save SMTP settings: {e}")
  424. raise HTTPException(
  425. status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
  426. detail=f"Failed to save SMTP settings: {str(e)}",
  427. )
  428. @router.post("/advanced-auth/enable", response_model=dict)
  429. async def enable_advanced_auth(
  430. current_user: User = Depends(get_current_active_user),
  431. db: AsyncSession = Depends(get_db),
  432. ):
  433. """Enable advanced authentication (admin only).
  434. Requires SMTP settings to be configured and tested first.
  435. """
  436. import logging
  437. logger = logging.getLogger(__name__)
  438. # Reload user with groups for proper is_admin check
  439. result = await db.execute(select(User).where(User.id == current_user.id).options(selectinload(User.groups)))
  440. user = result.scalar_one()
  441. if not user.is_admin:
  442. raise HTTPException(
  443. status_code=status.HTTP_403_FORBIDDEN,
  444. detail="Only admins can enable advanced authentication",
  445. )
  446. # Verify SMTP settings are configured
  447. smtp_settings = await get_smtp_settings(db)
  448. if not smtp_settings:
  449. raise HTTPException(
  450. status_code=status.HTTP_400_BAD_REQUEST,
  451. detail="SMTP settings must be configured before enabling advanced authentication",
  452. )
  453. try:
  454. await set_advanced_auth_enabled(db, True)
  455. await db.commit()
  456. logger.info(f"Advanced authentication enabled by admin user: {user.username}")
  457. return {"message": "Advanced authentication enabled successfully", "advanced_auth_enabled": True}
  458. except Exception as e:
  459. await db.rollback()
  460. logger.error(f"Failed to enable advanced authentication: {e}")
  461. raise HTTPException(
  462. status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
  463. detail=f"Failed to enable advanced authentication: {str(e)}",
  464. )
  465. @router.post("/advanced-auth/disable", response_model=dict)
  466. async def disable_advanced_auth(
  467. current_user: User = Depends(get_current_active_user),
  468. db: AsyncSession = Depends(get_db),
  469. ):
  470. """Disable advanced authentication (admin only)."""
  471. import logging
  472. logger = logging.getLogger(__name__)
  473. # Reload user with groups for proper is_admin check
  474. result = await db.execute(select(User).where(User.id == current_user.id).options(selectinload(User.groups)))
  475. user = result.scalar_one()
  476. if not user.is_admin:
  477. raise HTTPException(
  478. status_code=status.HTTP_403_FORBIDDEN,
  479. detail="Only admins can disable advanced authentication",
  480. )
  481. try:
  482. await set_advanced_auth_enabled(db, False)
  483. await db.commit()
  484. logger.info(f"Advanced authentication disabled by admin user: {user.username}")
  485. return {"message": "Advanced authentication disabled successfully", "advanced_auth_enabled": False}
  486. except Exception as e:
  487. await db.rollback()
  488. logger.error(f"Failed to disable advanced authentication: {e}")
  489. raise HTTPException(
  490. status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
  491. detail=f"Failed to disable advanced authentication: {str(e)}",
  492. )
  493. @router.get("/advanced-auth/status")
  494. async def get_advanced_auth_status(db: AsyncSession = Depends(get_db)):
  495. """Get advanced authentication status."""
  496. advanced_auth_enabled = await is_advanced_auth_enabled(db)
  497. smtp_configured = await get_smtp_settings(db) is not None
  498. return {
  499. "advanced_auth_enabled": advanced_auth_enabled,
  500. "smtp_configured": smtp_configured,
  501. }
  502. @router.post("/forgot-password", response_model=ForgotPasswordResponse)
  503. async def forgot_password(request: ForgotPasswordRequest, db: AsyncSession = Depends(get_db)):
  504. """Request password reset via email (advanced auth only)."""
  505. import logging
  506. logger = logging.getLogger(__name__)
  507. # Check if advanced auth is enabled
  508. advanced_auth = await is_advanced_auth_enabled(db)
  509. if not advanced_auth:
  510. raise HTTPException(
  511. status_code=status.HTTP_400_BAD_REQUEST,
  512. detail="Advanced authentication is not enabled",
  513. )
  514. # Get SMTP settings
  515. smtp_settings = await get_smtp_settings(db)
  516. if not smtp_settings:
  517. raise HTTPException(
  518. status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
  519. detail="Email service is not configured",
  520. )
  521. # Find user by email
  522. user = await get_user_by_email(db, request.email)
  523. # Always return success message to prevent email enumeration
  524. # but only send email if user exists and is not an LDAP user
  525. if user and user.is_active and user.auth_source != "ldap":
  526. try:
  527. # Generate new password
  528. new_password = generate_secure_password()
  529. user.password_hash = get_password_hash(new_password)
  530. await db.commit()
  531. login_url = await get_external_login_url(db)
  532. # Send password reset email
  533. subject, text_body, html_body = await create_password_reset_email_from_template(
  534. db, user.username, new_password, login_url
  535. )
  536. send_email(smtp_settings, user.email, subject, text_body, html_body)
  537. logger.info(f"Password reset email sent to {user.email}")
  538. except Exception as e:
  539. logger.error(f"Failed to send password reset email: {e}")
  540. # Don't reveal error to user for security
  541. return ForgotPasswordResponse(
  542. message="If the email address is associated with an account, a password reset email has been sent."
  543. )
  544. @router.post("/reset-password", response_model=ResetPasswordResponse)
  545. async def reset_user_password(
  546. request: ResetPasswordRequest,
  547. current_user: User = Depends(get_current_active_user),
  548. db: AsyncSession = Depends(get_db),
  549. ):
  550. """Reset a user's password and send them an email (admin only, advanced auth only)."""
  551. import logging
  552. logger = logging.getLogger(__name__)
  553. # Reload user with groups for proper is_admin check
  554. result = await db.execute(select(User).where(User.id == current_user.id).options(selectinload(User.groups)))
  555. admin_user = result.scalar_one()
  556. if not admin_user.is_admin:
  557. raise HTTPException(
  558. status_code=status.HTTP_403_FORBIDDEN,
  559. detail="Only admins can reset user passwords",
  560. )
  561. # Check if advanced auth is enabled
  562. advanced_auth = await is_advanced_auth_enabled(db)
  563. if not advanced_auth:
  564. raise HTTPException(
  565. status_code=status.HTTP_400_BAD_REQUEST,
  566. detail="Advanced authentication is not enabled",
  567. )
  568. # Get SMTP settings
  569. smtp_settings = await get_smtp_settings(db)
  570. if not smtp_settings:
  571. raise HTTPException(
  572. status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
  573. detail="Email service is not configured",
  574. )
  575. # Find user to reset
  576. result = await db.execute(select(User).where(User.id == request.user_id))
  577. user = result.scalar_one_or_none()
  578. if not user:
  579. raise HTTPException(
  580. status_code=status.HTTP_404_NOT_FOUND,
  581. detail="User not found",
  582. )
  583. if user.auth_source == "ldap":
  584. raise HTTPException(
  585. status_code=status.HTTP_400_BAD_REQUEST,
  586. detail="Cannot reset password for LDAP users — passwords are managed by the LDAP server",
  587. )
  588. if not user.email:
  589. raise HTTPException(
  590. status_code=status.HTTP_400_BAD_REQUEST,
  591. detail="User does not have an email address configured",
  592. )
  593. try:
  594. # Generate new password
  595. new_password = generate_secure_password()
  596. user.password_hash = get_password_hash(new_password)
  597. await db.commit()
  598. login_url = await get_external_login_url(db)
  599. # Send password reset email
  600. subject, text_body, html_body = await create_password_reset_email_from_template(
  601. db, user.username, new_password, login_url
  602. )
  603. send_email(smtp_settings, user.email, subject, text_body, html_body)
  604. logger.info(f"Password reset by admin {admin_user.username} for user {user.username}")
  605. return ResetPasswordResponse(message=f"Password reset email sent to {user.email}")
  606. except Exception as e:
  607. await db.rollback()
  608. logger.error(f"Failed to reset password for user {user.username}: {e}")
  609. raise HTTPException(
  610. status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
  611. detail=f"Failed to reset password: {str(e)}",
  612. )
  613. # LDAP Authentication Helpers
  614. async def _get_ldap_settings(db: AsyncSession) -> dict[str, str] | None:
  615. """Get LDAP settings from the database. Returns None if LDAP is not enabled."""
  616. ldap_keys = [
  617. "ldap_enabled",
  618. "ldap_server_url",
  619. "ldap_bind_dn",
  620. "ldap_bind_password",
  621. "ldap_search_base",
  622. "ldap_user_filter",
  623. "ldap_security",
  624. "ldap_group_mapping",
  625. "ldap_auto_provision",
  626. "ldap_ca_cert_path",
  627. ]
  628. result = await db.execute(select(Settings).where(Settings.key.in_(ldap_keys)))
  629. settings = {s.key: s.value for s in result.scalars().all()}
  630. if settings.get("ldap_enabled", "false").lower() != "true":
  631. return None
  632. return settings
  633. async def _provision_ldap_user(db: AsyncSession, ldap_user, ldap_config) -> User:
  634. """Create a new local user from LDAP authentication."""
  635. import logging
  636. from backend.app.services.ldap_service import resolve_group_mapping
  637. logger = logging.getLogger(__name__)
  638. new_user = User(
  639. username=ldap_user.username,
  640. email=ldap_user.email,
  641. password_hash=None,
  642. role="user",
  643. auth_source="ldap",
  644. is_active=True,
  645. )
  646. # Map LDAP groups to BamBuddy groups
  647. mapped_group_names = resolve_group_mapping(ldap_user.groups, ldap_config.group_mapping)
  648. if mapped_group_names:
  649. groups_result = await db.execute(select(Group).where(Group.name.in_(mapped_group_names)))
  650. new_user.groups = list(groups_result.scalars().all())
  651. db.add(new_user)
  652. await db.commit()
  653. await db.refresh(new_user)
  654. logger.info("Auto-provisioned LDAP user: %s (groups: %s)", new_user.username, mapped_group_names)
  655. return new_user
  656. async def _sync_ldap_user(db: AsyncSession, user: User, ldap_user, ldap_config) -> None:
  657. """Sync LDAP user attributes (email, groups) on each login."""
  658. import logging
  659. from backend.app.services.ldap_service import resolve_group_mapping
  660. logger = logging.getLogger(__name__)
  661. changed = False
  662. # Update email if changed
  663. if ldap_user.email and ldap_user.email != user.email:
  664. user.email = ldap_user.email
  665. changed = True
  666. # Sync group mappings — always update to match LDAP state (including revocation)
  667. mapped_group_names = resolve_group_mapping(ldap_user.groups, ldap_config.group_mapping)
  668. if mapped_group_names:
  669. groups_result = await db.execute(select(Group).where(Group.name.in_(mapped_group_names)))
  670. new_groups = list(groups_result.scalars().all())
  671. else:
  672. new_groups = []
  673. current_group_ids = {g.id for g in user.groups}
  674. new_group_ids = {g.id for g in new_groups}
  675. if current_group_ids != new_group_ids:
  676. user.groups = new_groups
  677. changed = True
  678. if changed:
  679. await db.commit()
  680. logger.info("Synced LDAP user attributes: %s", user.username)
  681. @router.post("/ldap/test")
  682. async def test_ldap(
  683. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
  684. db: AsyncSession = Depends(get_db),
  685. ):
  686. """Test LDAP connection using saved settings (admin only when auth enabled)."""
  687. import logging
  688. from backend.app.services.ldap_service import parse_ldap_config, test_ldap_connection
  689. logger = logging.getLogger(__name__)
  690. ldap_settings = await _get_ldap_settings(db)
  691. if not ldap_settings:
  692. # LDAP might not be enabled yet but settings might still exist — read all keys
  693. ldap_keys = [
  694. "ldap_enabled",
  695. "ldap_server_url",
  696. "ldap_bind_dn",
  697. "ldap_bind_password",
  698. "ldap_search_base",
  699. "ldap_user_filter",
  700. "ldap_security",
  701. "ldap_group_mapping",
  702. "ldap_auto_provision",
  703. ]
  704. result = await db.execute(select(Settings).where(Settings.key.in_(ldap_keys)))
  705. ldap_settings = {s.key: s.value for s in result.scalars().all()}
  706. # Force enabled for test
  707. ldap_settings["ldap_enabled"] = "true"
  708. config = parse_ldap_config(ldap_settings)
  709. if not config:
  710. return {"success": False, "message": "LDAP server URL is not configured"}
  711. success, message = test_ldap_connection(config)
  712. if success:
  713. logger.info("LDAP connection test successful")
  714. else:
  715. logger.warning("LDAP connection test failed: %s", message)
  716. return {"success": success, "message": message}
  717. @router.get("/ldap/status")
  718. async def get_ldap_status(db: AsyncSession = Depends(get_db)):
  719. """Get LDAP authentication status."""
  720. # Only fetch the minimum keys needed — never load secrets
  721. ldap_keys = ["ldap_enabled", "ldap_server_url"]
  722. result = await db.execute(select(Settings).where(Settings.key.in_(ldap_keys)))
  723. settings = {s.key: s.value for s in result.scalars().all()}
  724. return {
  725. "ldap_enabled": settings.get("ldap_enabled", "false").lower() == "true",
  726. "ldap_configured": bool(settings.get("ldap_server_url")),
  727. }