Skip to content

Commit

Permalink
feat: add login options
Browse files Browse the repository at this point in the history
  • Loading branch information
jahwag committed Nov 30, 2024
1 parent a7fbe00 commit 4ba865c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "claudesync"
version = "0.6.5"
version = "0.6.6"
authors = [
{name = "Jahziah Wagner", email = "[email protected]"},
]
Expand Down
22 changes: 21 additions & 1 deletion src/claudesync/cli/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
30 changes: 30 additions & 0 deletions src/claudesync/providers/base_claude_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
Expand Down

0 comments on commit 4ba865c

Please sign in to comment.