-
Notifications
You must be signed in to change notification settings - Fork 77
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
Fix/keycloak timeout #445
Open
MichalJura
wants to merge
12
commits into
master
Choose a base branch
from
fix/keycloak-timeout
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Fix/keycloak timeout #445
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
81bf3ef
If token expires user has anonymous access
9bd2256
added current path just in case of relogging in
b5da8fa
added refresh token feature
30293f2
cosmetics
1dbd7af
Moved keycloak token handling to backend
18a104f
moved token handling to backend
28b296c
moved refresh_token to http only cookies
2fb6e08
cosmetics
f2e1dd9
flake8 adjustments
6588c63
added more typing
0982f89
removed unneccessary logging
69992d4
changed refresh_token max_age
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 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,6 +1,8 @@ | ||
from contextlib import asynccontextmanager | ||
import os | ||
|
||
import requests # type: ignore | ||
|
||
import uvicorn # type: ignore | ||
from pathlib import Path | ||
from fastapi import ( | ||
|
@@ -44,6 +46,8 @@ | |
BackendStatusDatasetsSchema, | ||
AgentSchema, | ||
ServerSchema, | ||
LoginSchema, | ||
TokenSchema, | ||
) | ||
|
||
|
||
|
@@ -70,6 +74,24 @@ def with_plugins() -> Iterable[PluginManager]: | |
plugins.cleanup() | ||
|
||
|
||
def get_new_tokens(refresh_token): | ||
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. Please add typing (I guess |
||
data = { | ||
"grant_type": "refresh_token", | ||
"refresh_token": refresh_token, | ||
"client_id": db.config.openid_client_id, | ||
"client_secret": db.config.openid_secret, | ||
} | ||
url = "http://mquery-keycloak-1:8080/auth/realms/myrealm/protocol/openid-connect/token" | ||
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. Shouldn't be hardcoded - use url from config instead |
||
try: | ||
response: requests.Response = requests.post(url=url, data=data) | ||
token_data = response.json() | ||
new_refresh_token = token_data["refresh_token"] | ||
new_token = token_data["access_token"] | ||
return new_token, new_refresh_token | ||
except requests.exceptions.RequestException: | ||
return None, None | ||
|
||
|
||
class User: | ||
def __init__(self, token: Optional[Dict]) -> None: | ||
self.__token = token | ||
|
@@ -124,8 +146,11 @@ async def current_user(authorization: Optional[str] = Header(None)) -> User: | |
token_json = jwt.decode( | ||
token, public_key, algorithms=["RS256"], audience="account" # type: ignore | ||
) | ||
except jwt.ExpiredSignatureError: | ||
# token expired so user is anonymous | ||
return User(None) | ||
except jwt.InvalidTokenError: | ||
# Invalid token means invalid signature, issuer, or just expired. | ||
# Invalid token means invalid signature, issuer. | ||
raise unauthorized | ||
|
||
return User(token_json) | ||
|
@@ -584,6 +609,35 @@ def server() -> ServerSchema: | |
) | ||
|
||
|
||
@app.post("/api/login", response_model=LoginSchema, tags=["stable"]) | ||
async def login(request: Request, response: Response) -> LoginSchema: | ||
token = await request.json() | ||
if token["refresh_token"]: | ||
response.set_cookie( | ||
key="refresh_token", | ||
value=token["refresh_token"], | ||
httponly=True, | ||
max_age=1800, | ||
) | ||
return LoginSchema(status="OK") | ||
return LoginSchema(status="Bad Token") | ||
|
||
|
||
@app.post("/api/token/refresh", response_model=TokenSchema) | ||
def refresh_token(request: Request, response: Response) -> TokenSchema: | ||
refresh_token_value = request.cookies.get("refresh_token") | ||
if refresh_token_value: | ||
new_token, new_refresh_token = get_new_tokens(refresh_token_value) | ||
response.set_cookie( | ||
key="refresh_token", | ||
value=new_refresh_token, | ||
httponly=True, | ||
max_age=1800, | ||
) | ||
return TokenSchema(token=new_token) | ||
return TokenSchema(token=None) | ||
|
||
|
||
@app.get("/query/{path}", include_in_schema=False) | ||
def serve_index(path: str) -> FileResponse: | ||
return FileResponse(Path(__file__).parent / "mqueryfront/dist/index.html") | ||
|
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
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
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.
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.