From 4ba865c77c0b82b91c43c2e5476f9d1c134ca279 Mon Sep 17 00:00:00 2001 From: jahwag Date: Sat, 30 Nov 2024 19:40:56 +0100 Subject: [PATCH] feat: add login options --- pyproject.toml | 2 +- src/claudesync/cli/auth.py | 22 +++++++++++++++- src/claudesync/providers/base_claude_ai.py | 30 ++++++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 2cf62fb..8f713d1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "claudesync" -version = "0.6.5" +version = "0.6.6" authors = [ {name = "Jahziah Wagner", email = "540380+jahwag@users.noreply.github.com"}, ] diff --git a/src/claudesync/cli/auth.py b/src/claudesync/cli/auth.py index 18aabf1..f97cd90 100644 --- a/src/claudesync/cli/auth.py +++ b/src/claudesync/cli/auth.py @@ -19,14 +19,34 @@ def auth(): default="claude.ai", help="The provider to use for this project", ) +@click.option( + "--session-key", + help="Directly provide the Claude.ai session key", + envvar="CLAUDE_SESSION_KEY", +) +@click.option( + "--auto-approve", + is_flag=True, + help="Automatically approve the suggested expiry time", +) @click.pass_context @handle_errors -def login(ctx, provider): +def login(ctx, provider, session_key, auto_approve): """Authenticate with an AI provider.""" config = ctx.obj provider_instance = get_provider(config, provider) try: + if session_key: + # If session key is provided, bypass the interactive prompt + if not session_key.startswith("sk-ant"): + raise ProviderError( + "Invalid sessionKey format. Must start with 'sk-ant'" + ) + # Set auto_approve to True when session key is provided + provider_instance._auto_approve_expiry = auto_approve + provider_instance._provided_session_key = session_key + session_key, expiry = provider_instance.login() config.set_session_key(provider, session_key, expiry) click.echo( diff --git a/src/claudesync/providers/base_claude_ai.py b/src/claudesync/providers/base_claude_ai.py index 4981b0a..c5e0eb9 100644 --- a/src/claudesync/providers/base_claude_ai.py +++ b/src/claudesync/providers/base_claude_ai.py @@ -57,6 +57,36 @@ def _configure_logging(self): self.logger.setLevel(getattr(logging, log_level)) def login(self): + """ + Handle login with support for direct session key and auto-approve options. + """ + if hasattr(self, "_provided_session_key"): + # Use the provided session key + session_key = self._provided_session_key + + # Get expiry time + if hasattr(self, "_auto_approve_expiry") and self._auto_approve_expiry: + # Auto-approve with default expiry (30 days) + expires = datetime.datetime.now( + datetime.timezone.utc + ) + datetime.timedelta(days=30) + date_format = "%a, %d %b %Y %H:%M:%S %Z" + expires = expires.strftime(date_format).strip() + expires = datetime.datetime.strptime(expires, date_format) + else: + # Still prompt for expiry even with provided session key + expires = _get_session_key_expiry() + + # Validate the session key by attempting to get organizations + try: + self.config.set_session_key("claude.ai", session_key, expires) + organizations = self.get_organizations() + if organizations: + return session_key, expires + except ProviderError as e: + raise ProviderError(f"Invalid session key: {str(e)}") + + # Default to existing interactive login flow click.echo( "A session key is required to call: " + self.config.get("claude_api_url") )