()
| 1940 | |
| 1941 | |
| 1942 | def _bootstrap_tokens(): |
| 1943 | env_path = Path(__file__).resolve().parent / ".env" |
| 1944 | env_values = _read_env_file_values(env_path) |
| 1945 | |
| 1946 | admin_token = (os.environ.get("INTELOSINT_ADMIN_TOKEN") or env_values.get("INTELOSINT_ADMIN_TOKEN") or "").strip() |
| 1947 | if not admin_token: |
| 1948 | admin_token = os.urandom(32).hex() |
| 1949 | elif len(admin_token) < 24: |
| 1950 | admin_token = admin_token + os.urandom(16).hex() |
| 1951 | |
| 1952 | cred_key = (os.environ.get("INTELOSINT_CRED_KEY") or env_values.get("INTELOSINT_CRED_KEY") or "").strip() |
| 1953 | if not _is_valid_fernet_key(cred_key): |
| 1954 | if Fernet is None: |
| 1955 | raise RuntimeError("Dipendenza cryptography non disponibile: impossibile gestire INTELOSINT_CRED_KEY.") |
| 1956 | cred_key = Fernet.generate_key().decode("utf-8") |
| 1957 | |
| 1958 | os.environ["INTELOSINT_ADMIN_TOKEN"] = admin_token |
| 1959 | os.environ["INTELOSINT_CRED_KEY"] = cred_key |
| 1960 | env_values["INTELOSINT_ADMIN_TOKEN"] = admin_token |
| 1961 | env_values["INTELOSINT_CRED_KEY"] = cred_key |
| 1962 | |
| 1963 | try: |
| 1964 | env_lines = [f"{k}={v}" for k, v in env_values.items()] |
| 1965 | env_path.write_text("\n".join(env_lines) + "\n", encoding="utf-8") |
| 1966 | except Exception: |
| 1967 | print(f"[!] Impossibile scrivere o aggiornare il file {env_path}.") |
| 1968 | |
| 1969 | |
| 1970 | _load_local_env_file() |
no test coverage detected