-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial implementation * Refine implementation * PR Feedback * Add comment to require_login
- Loading branch information
1 parent
718763c
commit cae05e6
Showing
8 changed files
with
102 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import chainlit as cl | ||
from src.shared import get_app_config | ||
|
||
|
||
def login_callback(username: str, password: str) -> cl.User | None: | ||
if password == get_app_config().global_password: | ||
return cl.User(identifier=username) | ||
else: | ||
return None | ||
|
||
|
||
def require_login() -> None: | ||
# In addition to setting GLOBAL_PASSWORD, Chainlit also | ||
# requires CHAINLIT_AUTH_SECRET to be set (to sign the | ||
# authorization tokens.) | ||
|
||
if get_app_config().global_password: | ||
cl.password_auth_callback(login_callback) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from functools import cache | ||
|
||
from sentence_transformers import SentenceTransformer | ||
|
||
from src.app_config import AppConfig | ||
|
||
|
||
@cache | ||
def get_app_config() -> AppConfig: | ||
return AppConfig() | ||
|
||
|
||
@cache | ||
def get_embedding_model() -> SentenceTransformer: | ||
return SentenceTransformer(get_app_config().embedding_model) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import os | ||
|
||
import chainlit.config | ||
import src.shared | ||
from src.login import login_callback, require_login | ||
|
||
|
||
def test_require_login_no_password(monkeypatch): | ||
if "GLOBAL_PASSWORD" in os.environ: | ||
monkeypatch.delenv("GLOBAL_PASSWORD") | ||
|
||
# Rebuild AppConfig with new environment variables | ||
src.shared.get_app_config.cache_clear() | ||
|
||
require_login() | ||
|
||
assert not chainlit.config.code.password_auth_callback | ||
|
||
|
||
def test_require_login_with_password(monkeypatch): | ||
monkeypatch.setenv("GLOBAL_PASSWORD", "password") | ||
src.shared.get_app_config.cache_clear() | ||
|
||
require_login() | ||
|
||
assert chainlit.config.code.password_auth_callback | ||
|
||
|
||
def test_login_callback(monkeypatch): | ||
monkeypatch.setenv("GLOBAL_PASSWORD", "correct pass") | ||
src.shared.get_app_config.cache_clear() | ||
|
||
assert login_callback("some user", "wrong pass") is None | ||
assert login_callback("some user", "correct pass").identifier == "some user" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters