-
Notifications
You must be signed in to change notification settings - Fork 0
Add CLI for generating access tokens #42
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
+1,117
−10
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,72 @@ | ||
| import argparse | ||
| import sys | ||
|
|
||
| from kognic.auth import DEFAULT_HOST | ||
|
|
||
|
|
||
| def create_parser(): | ||
| parser = argparse.ArgumentParser( | ||
| prog="kognic-auth", | ||
| description="Kognic authentication CLI", | ||
| ) | ||
| subparsers = parser.add_subparsers(dest="command", help="Available commands") | ||
|
|
||
| # get-access-token subcommand | ||
| token_parser = subparsers.add_parser( | ||
| "get-access-token", | ||
| help="Generate an access token for Kognic API authentication", | ||
| ) | ||
| token_parser.add_argument( | ||
| "--server", | ||
| default=DEFAULT_HOST, | ||
| help=f"Authentication server URL (default: {DEFAULT_HOST})", | ||
| ) | ||
| token_parser.add_argument( | ||
| "--credentials", | ||
| metavar="FILE", | ||
| help="Path to JSON credentials file. If not provided, credentials are read from environment variables.", | ||
| ) | ||
|
|
||
| return parser | ||
|
|
||
|
|
||
| def get_access_token(parsed): | ||
| try: | ||
| from kognic.auth.requests.auth_session import RequestsAuthSession | ||
| except ImportError: | ||
| print("Error: requests library is required. Install with: pip install kognic-auth[requests]", file=sys.stderr) | ||
| return 1 | ||
|
|
||
| try: | ||
| session = RequestsAuthSession( | ||
| auth=parsed.credentials, | ||
| host=parsed.server, | ||
| ) | ||
| # Access .session to trigger token fetch | ||
| _ = session.session | ||
| print(session.access_token) | ||
| return 0 | ||
| except FileNotFoundError as e: | ||
| print(f"Error: {e}", file=sys.stderr) | ||
| return 1 | ||
| except ValueError as e: | ||
| print(f"Error: {e}", file=sys.stderr) | ||
| return 1 | ||
| except Exception as e: | ||
| print(f"Error fetching token: {e}", file=sys.stderr) | ||
| return 1 | ||
|
|
||
|
|
||
| def main(args=None): | ||
| parser = create_parser() | ||
| parsed = parser.parse_args(args) | ||
|
|
||
| if parsed.command == "get-access-token": | ||
| return get_access_token(parsed) | ||
| else: | ||
| parser.print_help() | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main()) |
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,126 @@ | ||
| import unittest | ||
| from unittest import mock | ||
|
|
||
| from kognic.auth import DEFAULT_HOST | ||
| from kognic.auth.cli import create_parser, main | ||
|
|
||
|
|
||
| class CliParserTest(unittest.TestCase): | ||
| def test_default_server(self): | ||
| parser = create_parser() | ||
| args = parser.parse_args(["get-access-token"]) | ||
| self.assertEqual(args.server, DEFAULT_HOST) | ||
| self.assertIsNone(args.credentials) | ||
|
|
||
| def test_custom_server(self): | ||
| parser = create_parser() | ||
| args = parser.parse_args(["get-access-token", "--server", "https://custom.auth.server"]) | ||
| self.assertEqual(args.server, "https://custom.auth.server") | ||
|
|
||
| def test_credentials_file(self): | ||
| parser = create_parser() | ||
| args = parser.parse_args(["get-access-token", "--credentials", "/path/to/creds.json"]) | ||
| self.assertEqual(args.credentials, "/path/to/creds.json") | ||
|
|
||
| def test_all_options(self): | ||
| parser = create_parser() | ||
| args = parser.parse_args( | ||
| ["get-access-token", "--server", "https://my.server", "--credentials", "my_creds.json"] | ||
| ) | ||
| self.assertEqual(args.server, "https://my.server") | ||
| self.assertEqual(args.credentials, "my_creds.json") | ||
|
|
||
| def test_no_command_shows_help(self): | ||
| with mock.patch("builtins.print"): | ||
| result = main([]) | ||
| self.assertEqual(result, 0) | ||
|
|
||
|
|
||
| class CliMainTest(unittest.TestCase): | ||
| @mock.patch("kognic.auth.requests.auth_session.RequestsAuthSession") | ||
| def test_main_prints_token(self, mock_session_class): | ||
| mock_session = mock.MagicMock() | ||
| mock_session.access_token = "test-access-token-123" | ||
| mock_session_class.return_value = mock_session | ||
|
|
||
| with mock.patch("builtins.print") as mock_print: | ||
| result = main(["get-access-token"]) | ||
|
|
||
| self.assertEqual(result, 0) | ||
| mock_print.assert_called_once_with("test-access-token-123") | ||
| mock_session_class.assert_called_once_with(auth=None, host=DEFAULT_HOST) | ||
|
|
||
| @mock.patch("kognic.auth.requests.auth_session.RequestsAuthSession") | ||
| def test_main_with_credentials_file(self, mock_session_class): | ||
| mock_session = mock.MagicMock() | ||
| mock_session.access_token = "token-from-file" | ||
| mock_session_class.return_value = mock_session | ||
|
|
||
| with mock.patch("builtins.print") as mock_print: | ||
| result = main(["get-access-token", "--credentials", "/path/to/creds.json"]) | ||
|
|
||
| self.assertEqual(result, 0) | ||
| mock_print.assert_called_once_with("token-from-file") | ||
| mock_session_class.assert_called_once_with(auth="/path/to/creds.json", host=DEFAULT_HOST) | ||
|
|
||
| @mock.patch("kognic.auth.requests.auth_session.RequestsAuthSession") | ||
| def test_main_with_custom_server(self, mock_session_class): | ||
| mock_session = mock.MagicMock() | ||
| mock_session.access_token = "custom-server-token" | ||
| mock_session_class.return_value = mock_session | ||
|
|
||
| with mock.patch("builtins.print") as mock_print: | ||
| result = main(["get-access-token", "--server", "https://custom.server"]) | ||
|
|
||
| self.assertEqual(result, 0) | ||
| mock_print.assert_called_once_with("custom-server-token") | ||
| mock_session_class.assert_called_once_with(auth=None, host="https://custom.server") | ||
|
|
||
| @mock.patch("kognic.auth.requests.auth_session.RequestsAuthSession") | ||
| def test_main_with_all_options(self, mock_session_class): | ||
| mock_session = mock.MagicMock() | ||
| mock_session.access_token = "full-options-token" | ||
| mock_session_class.return_value = mock_session | ||
|
|
||
| with mock.patch("builtins.print"): | ||
| result = main(["get-access-token", "--server", "https://my.server", "--credentials", "creds.json"]) | ||
|
|
||
| self.assertEqual(result, 0) | ||
| mock_session_class.assert_called_once_with(auth="creds.json", host="https://my.server") | ||
|
|
||
| @mock.patch("kognic.auth.requests.auth_session.RequestsAuthSession") | ||
| def test_main_file_not_found(self, mock_session_class): | ||
| mock_session_class.side_effect = FileNotFoundError("Could not find Api Credentials file at /bad/path.json") | ||
|
|
||
| with mock.patch("builtins.print") as mock_print: | ||
| result = main(["get-access-token", "--credentials", "/bad/path.json"]) | ||
|
|
||
| self.assertEqual(result, 1) | ||
| mock_print.assert_called_once() | ||
| self.assertIn("Error:", mock_print.call_args[0][0]) | ||
|
|
||
| @mock.patch("kognic.auth.requests.auth_session.RequestsAuthSession") | ||
| def test_main_value_error(self, mock_session_class): | ||
| mock_session_class.side_effect = ValueError("Bad auth credentials") | ||
|
|
||
| with mock.patch("builtins.print") as mock_print: | ||
| result = main(["get-access-token"]) | ||
|
|
||
| self.assertEqual(result, 1) | ||
| mock_print.assert_called_once() | ||
| self.assertIn("Error:", mock_print.call_args[0][0]) | ||
|
|
||
| @mock.patch("kognic.auth.requests.auth_session.RequestsAuthSession") | ||
| def test_main_generic_exception(self, mock_session_class): | ||
| mock_session_class.side_effect = Exception("Network error") | ||
|
|
||
| with mock.patch("builtins.print") as mock_print: | ||
| result = main(["get-access-token"]) | ||
|
|
||
| self.assertEqual(result, 1) | ||
| mock_print.assert_called_once() | ||
| self.assertIn("Error fetching token:", mock_print.call_args[0][0]) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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.