vincbeck commented on code in PR #75938:
URL: https://github.com/apache/airflow/pull/75938#discussion_r4601827658


##########
airflow-ctl/src/airflowctl/ctl/commands/auth_command.py:
##########
@@ -53,6 +87,38 @@ def login(args, api_client=NEW_API_CLIENT) -> None:
     password = args.password
     token = args.api_token or os.environ.get("AIRFLOW_CLI_TOKEN")
 
+    if args.sso:
+        if args.api_token or username or password:
+            rich.print(
+                "[red]--sso is not compatible with --api-token, --username or 
--password.", file=sys.stderr
+            )
+            sys.exit(1)
+        try:
+            api_token = _get_token_with_browser(args, api_client, api_url)
+        except Exception as e:
+            rich.print(f"[red]Login failed: {escape(str(e))}", file=sys.stderr)
+            sys.exit(1)
+        not_stored_reason = "The token was not stored." if args.skip_keyring 
else ""
+        try:
+            Credentials(
+                api_url=api_url,
+                api_token=api_token,
+                api_environment=args.env,
+                client_kind=ClientKind.AUTH,
+            ).save(args.skip_keyring)
+        except AirflowCtlKeyringException:

Review Comment:
   The browser login can still be lost here. `Credentials.save` only turns 
`NoKeyringError` and `NotImplementedError` into `AirflowCtlKeyringException`. 
Other keyring errors go through: `KeyringLocked` (locked Secret Service, or 
macOS Keychain access denied), `PasswordSetError`, or the `ValueError` from a 
wrong `EncryptedKeyring` password. The command then ends with a traceback and 
the token is never printed. Fix: also catch `keyring.errors.KeyringError` and 
`ValueError` here, or wrap them in `Credentials.save`.



##########
airflow-ctl/src/airflowctl/ctl/cli_config.py:
##########
@@ -345,6 +357,30 @@ def _load_help_texts_yaml() -> dict[str, dict[str, str]]:
     dest="password",
     help="The password to use for authentication",
 )
+ARG_AUTH_SSO = Arg(
+    flags=("--sso",),
+    dest="sso",
+    default=False,
+    action="store_true",
+    help="Log in through the browser with the identity provider of the API 
server, if it offers it",
+)
+ARG_AUTH_NO_BROWSER = Arg(

Review Comment:
   nit: without `--sso`, `--no-browser` and `--timeout` are silently ignored, 
so `auth login --no-browser` falls back to the password prompt. Should they 
fail like the other incompatible flags?



##########
airflow-ctl/src/airflowctl/ctl/commands/auth_command.py:
##########
@@ -40,7 +42,39 @@
 )
 from airflowctl.api.datamodels.auth_generated import LoginBody
 from airflowctl.ctl.console_formatting import AirflowConsole
-from airflowctl.exceptions import AirflowCtlCredentialInvalidException
+from airflowctl.ctl.utils.browser_login import get_authorization_code, 
resolve_endpoint
+from airflowctl.exceptions import (
+    AirflowCtlBrowserLoginException,
+    AirflowCtlCredentialInvalidException,
+    AirflowCtlKeyringException,
+)
+
+
+def _get_token_with_browser(args, api_client: Client, api_url: str) -> str:
+    """Log in through the browser and return the CLI token, if the API server 
offers it."""
+    api_client.refresh_base_url(base_url=api_url, kind=ClientKind.AUTH)

Review Comment:
   Is `refresh_base_url` needed? Both requests below use absolute URLs.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to