Ask for a shared token before the UI shows. If FABRIC_UI_TOKEN is set, the user must type the same token before the app shows. If it is not set, there is no token check, and only the loopback bind in .streamlit/config.toml limits access. The compare time does not change with the tok
()
| 1375 | |
| 1376 | |
| 1377 | def _require_access_token(): |
| 1378 | """Ask for a shared token before the UI shows. |
| 1379 | |
| 1380 | If FABRIC_UI_TOKEN is set, the user must type the same token before the |
| 1381 | app shows. If it is not set, there is no token check, and only the |
| 1382 | loopback bind in .streamlit/config.toml limits access. The compare |
| 1383 | time does not change with the token value. |
| 1384 | """ |
| 1385 | token = os.environ.get("FABRIC_UI_TOKEN", "") |
| 1386 | if not token: |
| 1387 | return |
| 1388 | if st.session_state.get("_authed"): |
| 1389 | return |
| 1390 | if len(token) < 16: |
| 1391 | logger.warning("FABRIC_UI_TOKEN is short: use a random token of 16 or more characters") |
| 1392 | st.title("🔒 Fabric Pattern Studio") |
| 1393 | entered = st.text_input("Access token", type="password") |
| 1394 | # Compare bytes. compare_digest refuses a str with non-ASCII characters. |
| 1395 | if entered and secrets.compare_digest(entered.encode(), token.encode()): |
| 1396 | st.session_state["_authed"] = True |
| 1397 | st.rerun() |
| 1398 | if entered: |
| 1399 | st.error("Invalid access token") |
| 1400 | st.stop() |
| 1401 | |
| 1402 | |
| 1403 | def main(): |