-
Notifications
You must be signed in to change notification settings - Fork 0
feat(api): adds automatic keycloak verification #12
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
Open
Matt-Carre
wants to merge
8
commits into
main
Choose a base branch
from
keycloak_auth
base: main
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f3026fd
fix(pre-commit): reworks compliance check to be more efficient
Matt-Carre 9dfe669
feat(api): adds a test for authentication
Matt-Carre 157d3f8
feat(api): adds error-handling to keycloak_checker
Matt-Carre b7e6880
fix(api): allow manual port-forwarding to 5173
TBThomas56 5da76c5
fix(api): graceful shutdown of local keycloak server
TBThomas56 38514a8
feat(api): adds environmental variables to ensure all code is ran in …
Matt-Carre 81e49e2
feat(api): adds unit tests to open_auth_url
Matt-Carre 5deb6fa
fix(api): changes back to identity-test.diamond.ac.uk
Matt-Carre 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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # refresh_token: dict[str, str] = ( | ||
| # # pyright: ignore[reportUnknownVariableType] | ||
| # keycloak_openid.refresh_token( # pyright: ignore[reportUnknownMemberType] | ||
| # token["refresh_token"] | ||
| # ) | ||
| # ) | ||
| # print(refresh_token) | ||
|
|
||
| # save token and refresh token to file | ||
| # if they're present, check if access token is valid, if no but refresh token is | ||
| # , just refresh token | ||
| # if invalid, run whole thing |
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 was deleted.
Oops, something went wrong.
47 changes: 47 additions & 0 deletions
47
src/python_interface_to_workflows/auth/keycloak_checker.py
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,47 @@ | ||
| import os | ||
|
|
||
| from keycloak import KeycloakOpenID | ||
| from keycloak.pkce_utils import generate_code_challenge, generate_code_verifier | ||
|
|
||
| from python_interface_to_workflows.auth.open_auth_url import open_auth_url | ||
|
|
||
|
|
||
| def return_key(dev: bool) -> str: | ||
| match dev: | ||
| case True: | ||
| keycloak_openid = KeycloakOpenID( | ||
| server_url="https://identity-test.diamond.ac.uk/", | ||
| client_id="workflows-ui-dev", | ||
| realm_name="dls", | ||
| client_secret_key="", | ||
| pool_maxsize=1, | ||
| ) | ||
| case False: | ||
| keycloak_openid = KeycloakOpenID( | ||
| client_id="workflows-dashboard", | ||
| server_url="https://identity-test.diamond.ac.uk/", | ||
| realm_name="dls", | ||
| client_secret_key="", | ||
| pool_maxsize=1, | ||
| ) | ||
| code_verifier = generate_code_verifier() | ||
| code_challenge, code_challenge_method = generate_code_challenge(code_verifier) | ||
| auth_url = keycloak_openid.auth_url( | ||
| redirect_uri="http://localhost:5173/", | ||
| scope="openid posix-uid profile email fedid", | ||
| state="", | ||
| code_challenge=code_challenge, | ||
| code_challenge_method=code_challenge_method, | ||
| ) | ||
| open_auth_url(auth_url) | ||
| token: dict[str, str] = ( # pyright: ignore[reportUnknownVariableType] | ||
| keycloak_openid.token( # pyright: ignore[reportUnknownMemberType] | ||
| grant_type="authorization_code", | ||
| code=os.environ["AUTH"], | ||
| redirect_uri="http://localhost:5173/", | ||
| code_verifier=code_verifier, | ||
| ) | ||
| ) | ||
| os.environ["REFRESH"] = token["refresh_token"] | ||
| os.environ["TOKEN"] = token["access_token"] | ||
| return token["access_token"] # pyright: ignore[reportUnknownArgumentType] |
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,41 @@ | ||
| import os | ||
| import socket | ||
| import urllib.parse | ||
| import webbrowser | ||
| from http.server import BaseHTTPRequestHandler, HTTPServer | ||
| from typing import cast | ||
|
|
||
|
|
||
| class _ReusingHTTPServer(HTTPServer): | ||
| allow_reuse_address = True | ||
| auth_code: str | ||
|
|
||
|
|
||
| class CallbackHandler(BaseHTTPRequestHandler): | ||
| def do_GET(self): | ||
| query = urllib.parse.urlparse(self.path).query | ||
| params = urllib.parse.parse_qs(query) | ||
| if "code" in params: | ||
| cast(_ReusingHTTPServer, self.server).auth_code = params["code"][0] | ||
| self.send_response(200) | ||
| self.end_headers() | ||
| self.wfile.write(b"Authorization successful. You can close this window.") | ||
| else: | ||
| self.send_response(400) | ||
| self.end_headers() | ||
| self.wfile.write(b"Missing authorization code.") | ||
|
|
||
|
|
||
| def open_auth_url(auth_url: str): | ||
| httpd = _ReusingHTTPServer(("localhost", 5173), CallbackHandler) | ||
| webbrowser.open(auth_url) | ||
| try: | ||
| httpd.handle_request() | ||
| os.environ["AUTH"] = httpd.auth_code | ||
| except OSError: | ||
| os.environ["AUTH"] = "" | ||
| print("ERROR: Port in use. Please restart your terminal.") | ||
| exit(1) | ||
| finally: | ||
| httpd.socket.shutdown(socket.SHUT_RDWR) | ||
| httpd.server_close() |
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 |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import os | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| from pytest import mark | ||
|
|
||
| from python_interface_to_workflows.auth.keycloak_checker import return_key | ||
|
|
||
|
|
||
| @mark.parametrize("dev", [True, False]) | ||
| @patch("python_interface_to_workflows.auth.keycloak_checker.KeycloakOpenID") | ||
| @patch("python_interface_to_workflows.auth.keycloak_checker.generate_code_verifier") | ||
| @patch("python_interface_to_workflows.auth.keycloak_checker.generate_code_challenge") | ||
| @patch("python_interface_to_workflows.auth.keycloak_checker.open_auth_url") | ||
| def test_return_key( | ||
| mock_open_auth_url: MagicMock, | ||
| mock_gen_code_challenge: MagicMock, | ||
| mock_gen_code_verifier: MagicMock, | ||
| mock_gen_keycloak_id: MagicMock, | ||
| dev: bool, | ||
| ): | ||
| mock_gen_code_verifier.return_value = "verifier" | ||
| mock_gen_code_challenge.return_value = ("challenge", "S256") | ||
| os.environ["AUTH"] = "auth_url_code" | ||
| keycloak = MagicMock() | ||
| mock_gen_keycloak_id.return_value = keycloak | ||
|
|
||
| keycloak.auth_url.return_value = "https://mock.site" | ||
| keycloak.token.return_value = { | ||
| "access_token": "fake_token", | ||
| "refresh_token": "fake_refresh", | ||
| } | ||
| assert return_key(dev) == "fake_token" | ||
| mock_open_auth_url.assert_called_once_with("https://mock.site") | ||
| keycloak.token.assert_called_once_with( | ||
| grant_type="authorization_code", | ||
| code="auth_url_code", | ||
| redirect_uri="http://localhost:5173/", | ||
| code_verifier="verifier", | ||
| ) |
Oops, something went wrong.
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.