-
Notifications
You must be signed in to change notification settings - Fork 425
backend (auth): Remove Session usage, add JWT bearer auth logic, dynamically set router dependencies #163
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
Merged
Merged
backend (auth): Remove Session usage, add JWT bearer auth logic, dynamically set router dependencies #163
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
1dc63ab
wip
tianjing-li 1066962
wip google oauth
tianjing-li 45bbb90
Add auth code
tianjing-li ff9a877
Add Google redirect logic
tianjing-li 06ca28e
Change class name
tianjing-li 6185e6e
Merge branch 'main' into add-google-oauth
tianjing-li 6e249fe
pr comments
tianjing-li 112ef51
fix tests
tianjing-li a48333d
Merge branch 'main' into add-google-oauth
tianjing-li 91c5d9c
Add JWT logic for basic auth + oauth, move router dependencies to cre…
tianjing-li eaa710f
Revert makefile
tianjing-li 7c9c88a
revert compose
tianjing-li 0474788
Merge conflict fixes
tianjing-li a2a8f04
disable auth by default
tianjing-li 3aeb1f3
Merge branch 'main' into add-jwt
tianjing-li f9d20f3
Add header logic
tianjing-li f7e028b
Add header logic everywhere
tianjing-li 576299a
Merge branch 'add-jwt' of https://github.com/cohere-ai/cohere-toolkit…
tianjing-li 305db8a
remove basic auth
tianjing-li 5c5229e
add sentencepiece
tianjing-li 082c358
update deps
tianjing-li a0f79c0
fix freezegun test issue
tianjing-li 21e08f2
Merge branch 'main' into add-jwt
tianjing-li 7a9c36f
Small fixes to OAuth
tianjing-li cf007de
Merge branch 'add-jwt' of https://github.com/cohere-ai/cohere-toolkit…
tianjing-li bbe6d84
pr comments
tianjing-li ad1051e
format
tianjing-li 3b0e845
Merge branch 'main' into add-jwt
tianjing-li abca2bc
merge main
tianjing-li c26af9a
Merge branch 'add-jwt' of https://github.com/cohere-ai/cohere-toolkit…
tianjing-li 01e4f73
small fix
tianjing-li ae4847b
revert makefile
tianjing-li 8dfc91a
Merge branch 'main' into add-jwt
tianjing-li e43c744
revert community deps
tianjing-li bd37798
Merge branch 'add-jwt' of https://github.com/cohere-ai/cohere-toolkit…
tianjing-li File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -35,4 +35,4 @@ first-run: | |
| win-first-run: | ||
| make win-setup | ||
| make migrate | ||
| make dev | ||
| make dev | ||
This file contains hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,9 +1,23 @@ | ||
| from backend.services.auth import BasicAuthentication, GoogleOAuth | ||
|
|
||
| # Modify this to enable auth strategies. | ||
| # Add Auth strategy classes here to enable them | ||
| # Ex: [BasicAuthentication] | ||
| ENABLED_AUTH_STRATEGIES = [] | ||
|
|
||
| # Define the mapping from Auth strategy name to class obj - does not need to be manually modified. | ||
| # During runtime, this will create an instance of each enabled strategy class. | ||
| # Ex: {"Basic": BasicAuthentication()} | ||
| ENABLED_AUTH_STRATEGY_MAPPING = {cls.NAME: cls() for cls in ENABLED_AUTH_STRATEGIES} | ||
|
|
||
|
|
||
| def is_authentication_enabled() -> bool: | ||
| """ | ||
| Check whether any form of authentication was enabled. | ||
|
|
||
| Returns: | ||
| bool: Whether authentication is enabled. | ||
| """ | ||
| if ENABLED_AUTH_STRATEGIES: | ||
| return True | ||
|
|
||
| return False |
This file contains hidden or 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,90 @@ | ||
| from enum import StrEnum | ||
|
|
||
| from fastapi import Depends | ||
|
|
||
| from backend.database_models import get_session | ||
| from backend.services.auth.request_validators import validate_authorization | ||
| from backend.services.request_validators import ( | ||
| validate_chat_request, | ||
| validate_user_header, | ||
| ) | ||
|
|
||
|
|
||
| # Important! Any new routers must have a corresponding RouterName entry and Router dependencies | ||
| # mapping below. Make sure they use the correct ones depending on whether authentication is enabled or not. | ||
| class RouterName(StrEnum): | ||
| AUTH = "auth" | ||
| CHAT = "chat" | ||
| CONVERSATION = "conversation" | ||
| DEPLOYMENT = "deployment" | ||
| EXPERIMENTAL_FEATURES = "experimental_features" | ||
| TOOL = "tool" | ||
| USER = "user" | ||
|
|
||
|
|
||
| # Router dependency mappings | ||
| ROUTER_DEPENDENCIES = { | ||
tianjing-li marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| RouterName.AUTH: { | ||
| "default": [ | ||
| Depends(get_session), | ||
| ], | ||
| "auth": [ | ||
| Depends(get_session), | ||
| ], | ||
| }, | ||
| RouterName.CHAT: { | ||
| "default": [ | ||
| Depends(get_session), | ||
| Depends(validate_chat_request), | ||
| Depends(validate_user_header), | ||
| ], | ||
| "auth": [ | ||
| Depends(get_session), | ||
| Depends(validate_chat_request), | ||
| Depends(validate_authorization), | ||
| ], | ||
| }, | ||
| RouterName.CONVERSATION: { | ||
| "default": [ | ||
| Depends(get_session), | ||
| Depends(validate_user_header), | ||
| ], | ||
| "auth": [ | ||
| Depends(get_session), | ||
| Depends(validate_authorization), | ||
| ], | ||
| }, | ||
| RouterName.DEPLOYMENT: { | ||
| "default": [ | ||
| Depends(get_session), | ||
| ], | ||
| "auth": [ | ||
| Depends(get_session), | ||
| Depends(validate_authorization), | ||
| ], | ||
| }, | ||
| RouterName.EXPERIMENTAL_FEATURES: { | ||
| "default": [ | ||
| Depends(get_session), | ||
| ], | ||
| "auth": [ | ||
| Depends(get_session), | ||
| Depends(validate_authorization), | ||
| ], | ||
| }, | ||
| RouterName.TOOL: { | ||
| "default": [], | ||
| "auth": [ | ||
| Depends(validate_authorization), | ||
| ], | ||
| }, | ||
| RouterName.USER: { | ||
| "default": [ | ||
| Depends(get_session), | ||
| ], | ||
| "auth": [ | ||
| Depends(get_session), | ||
| Depends(validate_authorization), | ||
| ], | ||
| }, | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.