auth.py 880 B

1234567891011121314151617181920212223242526
  1. import os
  2. from typing import Optional
  3. from dotenv import load_dotenv
  4. from ldap3 import ALL, Connection, Server
  5. load_dotenv()
  6. def ldap_authenticate(username: str, password: str) -> Optional[dict]:
  7. ldap_url = os.environ.get("LDAP_URL")
  8. ldap_user_dn = os.environ.get("LDAP_USER_DN")
  9. ldap_base = os.environ.get("LDAP_BASE_DN")
  10. if not ldap_url:
  11. # Fallback mock for local development: accept any user with password 'password'
  12. if password == "password":
  13. return {"username": username, "groups": ["local"]}
  14. return None
  15. try:
  16. server = Server(ldap_url, get_info=ALL)
  17. conn = Connection(server, user=username, password=password, auto_bind=True)
  18. # Simple success -> return username; group resolution left for future
  19. return {"username": username, "groups": []}
  20. except Exception:
  21. return None