Skip to content

Commit 3cb04d4

Browse files
committed
CM-60929-fix-review
1 parent 4da4bc8 commit 3cb04d4

File tree

5 files changed

+32
-7
lines changed

5 files changed

+32
-7
lines changed

cycode/cli/apps/ai_guardrails/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import typer
22

3+
from cycode.cli.apps.ai_guardrails.ensure_auth_command import ensure_auth_command
34
from cycode.cli.apps.ai_guardrails.install_command import install_command
45
from cycode.cli.apps.ai_guardrails.scan.scan_command import scan_command
56
from cycode.cli.apps.ai_guardrails.status_command import status_command
@@ -17,3 +18,6 @@
1718
name='scan',
1819
short_help='Scan content from AI IDE hooks for secrets (reads JSON from stdin).',
1920
)(scan_command)
21+
app.command(hidden=True, name='ensure-auth', short_help='Ensure authentication, triggering auth if needed.')(
22+
ensure_auth_command
23+
)

cycode/cli/apps/ai_guardrails/consts.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,15 @@ def _get_claude_code_hooks_dir() -> Path:
8383

8484
# Command used in hooks
8585
CYCODE_SCAN_PROMPT_COMMAND = 'cycode ai-guardrails scan'
86-
CYCODE_AUTH_CHECK_COMMAND = "if cycode status 2>&1 | grep -q 'Is authenticated: False'; then cycode auth 2>&1; fi"
86+
CYCODE_ENSURE_AUTH_COMMAND = 'cycode ai-guardrails ensure-auth'
8787

8888

8989
def _get_cursor_hooks_config(async_mode: bool = False) -> dict:
9090
"""Get Cursor-specific hooks configuration."""
9191
config = IDE_CONFIGS[AIIDEType.CURSOR]
9292
command = f'{CYCODE_SCAN_PROMPT_COMMAND} &' if async_mode else CYCODE_SCAN_PROMPT_COMMAND
9393
hooks = {event: [{'command': command}] for event in config.hook_events}
94-
hooks['sessionStart'] = [{'command': CYCODE_AUTH_CHECK_COMMAND}]
94+
hooks['sessionStart'] = [{'command': CYCODE_ENSURE_AUTH_COMMAND}]
9595

9696
return {
9797
'version': 1,
@@ -118,7 +118,7 @@ def _get_claude_code_hooks_config(async_mode: bool = False) -> dict:
118118
'SessionStart': [
119119
{
120120
'matcher': '',
121-
'hooks': [{'type': 'command', 'command': CYCODE_AUTH_CHECK_COMMAND}],
121+
'hooks': [{'type': 'command', 'command': CYCODE_ENSURE_AUTH_COMMAND}],
122122
}
123123
],
124124
'UserPromptSubmit': [
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import typer
2+
3+
from cycode.cli.apps.auth.auth_common import get_authorization_info
4+
from cycode.cli.apps.auth.auth_manager import AuthManager
5+
from cycode.cli.exceptions.handle_auth_errors import handle_auth_exception
6+
from cycode.cli.logger import logger
7+
8+
9+
def ensure_auth_command(ctx: typer.Context) -> None:
10+
"""Ensure the user is authenticated, triggering authentication if needed."""
11+
auth_info = get_authorization_info(ctx)
12+
if auth_info is not None:
13+
logger.debug('Already authenticated')
14+
return
15+
16+
logger.debug('Not authenticated, starting authentication')
17+
try:
18+
auth_manager = AuthManager()
19+
auth_manager.authenticate()
20+
except Exception as err:
21+
handle_auth_exception(ctx, err)

cycode/cli/apps/ai_guardrails/hooks_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def save_hooks_file(hooks_path: Path, hooks_config: dict) -> bool:
6262
return False
6363

6464

65-
_CYCODE_COMMAND_MARKERS = ('cycode ai-guardrails', 'cycode auth')
65+
_CYCODE_COMMAND_MARKERS = ('cycode ai-guardrails',)
6666

6767

6868
def _is_cycode_command(command: str) -> bool:

tests/cli/commands/ai_guardrails/test_hooks_manager.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from pyfakefs.fake_filesystem import FakeFilesystem
77

88
from cycode.cli.apps.ai_guardrails.consts import (
9-
CYCODE_AUTH_CHECK_COMMAND,
9+
CYCODE_ENSURE_AUTH_COMMAND,
1010
CYCODE_SCAN_PROMPT_COMMAND,
1111
AIIDEType,
1212
PolicyMode,
@@ -93,7 +93,7 @@ def test_get_hooks_config_cursor_session_start() -> None:
9393
assert 'sessionStart' in config['hooks']
9494
entries = config['hooks']['sessionStart']
9595
assert len(entries) == 1
96-
assert entries[0]['command'] == CYCODE_AUTH_CHECK_COMMAND
96+
assert entries[0]['command'] == CYCODE_ENSURE_AUTH_COMMAND
9797

9898

9999
def test_get_hooks_config_claude_code_sync() -> None:
@@ -123,7 +123,7 @@ def test_get_hooks_config_claude_code_session_start() -> None:
123123
assert 'SessionStart' in config['hooks']
124124
entries = config['hooks']['SessionStart']
125125
assert len(entries) == 1
126-
assert entries[0]['hooks'][0]['command'] == CYCODE_AUTH_CHECK_COMMAND
126+
assert entries[0]['hooks'][0]['command'] == CYCODE_ENSURE_AUTH_COMMAND
127127

128128

129129
def test_create_policy_file_warn(fs: FakeFilesystem) -> None:

0 commit comments

Comments
 (0)