-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Require login #17
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
KevinJBoyer marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from sentence_transformers import SentenceTransformer | ||
|
||
from src.app_config import AppConfig | ||
|
||
_app_config: AppConfig | None = None | ||
|
||
|
||
def get_appconfig() -> AppConfig: | ||
KevinJBoyer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
global _app_config | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To facilitate testing, consider minimizing use of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TIL about the built in cache decorators-- updated the code to use this! |
||
|
||
if not _app_config: | ||
_app_config = AppConfig() | ||
return _app_config | ||
|
||
|
||
_embedding_model: SentenceTransformer | None = None | ||
|
||
|
||
def get_embedding_model() -> SentenceTransformer: | ||
global _embedding_model | ||
|
||
if not _embedding_model: | ||
_embedding_model = SentenceTransformer(get_appconfig().embedding_model) | ||
return _embedding_model |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import chainlit as cl | ||
from src.cache import get_appconfig | ||
|
||
|
||
def login_callback(username: str, password: str) -> cl.User | None: | ||
if password == get_appconfig().global_password: | ||
return cl.User(identifier=username) | ||
else: | ||
return None | ||
|
||
|
||
def require_login() -> None: | ||
if get_appconfig().global_password: | ||
cl.password_auth_callback(login_callback) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import os | ||
|
||
import chainlit.config | ||
import src.cache | ||
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") | ||
src.cache._app_config = None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unclear why this line is needed without examining the code. This is probably an artifact of using global variables. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a comment to the first instance of this in the file! |
||
|
||
require_login() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Arguably, this could be replaced with something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, ideally I'd like to have some kind of integration test of |
||
|
||
assert not chainlit.config.code.password_auth_callback | ||
|
||
|
||
def test_require_login_with_password(monkeypatch): | ||
monkeypatch.setenv("GLOBAL_PASSWORD", "password") | ||
src.cache._app_config = None | ||
|
||
require_login() | ||
|
||
assert chainlit.config.code.password_auth_callback | ||
|
||
|
||
def test_login_callback(monkeypatch): | ||
monkeypatch.setenv("GLOBAL_PASSWORD", "correct pass") | ||
src.cache._app_config = None | ||
|
||
assert login_callback("some user", "wrong pass") is None | ||
assert login_callback("some user", "correct pass").identifier == "some user" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is leftover from the Flask API code we imported, deleted it to avoid any confusion.