Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,5 @@ jobs:
uses: gitleaks/gitleaks-action@e0c47f4f8be36e29cdc102c57e68cb5cbf0e8d1e # v3.0.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Same version as .pre-commit-config.yaml; the action pins an older one by default.
GITLEAKS_VERSION: 8.30.1
9 changes: 9 additions & 0 deletions .gitleaks.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Extends the built-in rules with the one token shape they do not know.
[extend]
useDefault = true

[[rules]]
id = "yandex-oauth-token"
description = "A bare Yandex OAuth token: y0_ then 40 or more URL-safe characters."
regex = '''y0_[A-Za-z0-9_-]{40,}'''
keywords = ["y0_"]
2 changes: 1 addition & 1 deletion src/ycli/yandex/status/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def _device_flow(oauth_client: OAuthClient, device_name: str | None, console: Co
while True:
result: TokenPollResult = oauth_client.poll_token(device.device_code)
if result.token is not None:
return result.token.access_token
return result.token.access_token.get_secret_value()
if not result.pending:
typer.secho(f"Authorization failed: {result.error}", fg=typer.colors.RED, err=True)
raise typer.Exit(1)
Expand Down
4 changes: 2 additions & 2 deletions src/ycli/yandex/status/oauth_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
these are plain data with no serialization logic (that lives in ``output.py``).
"""

from pydantic import Field
from pydantic import Field, SecretStr

from ycli.yandex.models import APIModel

Expand Down Expand Up @@ -36,7 +36,7 @@ class TokenResponse(APIModel):
token is always present.
"""

access_token: str = Field(description="The issued OAuth access token.")
access_token: SecretStr = Field(description="The issued OAuth access token.")
token_type: str | None = Field(default=None, description="Type of the token, e.g. `bearer`.")
expires_in: int | None = Field(default=None, description="Seconds until the token expires.")
scope: str | None = Field(default=None, description="Scopes the token was granted.")
Expand Down
6 changes: 5 additions & 1 deletion tests/unit/yandex/status/test_login_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from types import SimpleNamespace

import pytest
from pydantic import SecretStr
from rich.console import Console
from rich.live import Live
from typer.testing import CliRunner
Expand Down Expand Up @@ -318,7 +319,10 @@ def test_device_flow_shows_code_in_a_panel_and_returns_token(monkeypatch):
buf = StringIO()
console = Console(file=buf, force_terminal=False, width=80)
client = _FakeOAuth(
[TokenPollResult(pending=True), TokenPollResult(token=TokenResponse(access_token="tok-9"))]
[
TokenPollResult(pending=True),
TokenPollResult(token=TokenResponse(access_token=SecretStr("tok-9"))),
]
)
token = _device_flow(client, None, console) # ty: ignore[invalid-argument-type]
assert token == "tok-9"
Expand Down
11 changes: 10 additions & 1 deletion tests/unit/yandex/status/test_oauth_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import httpx2
import pytest
from pydantic import SecretStr

from ycli.yandex.errors import (
YandexAuthError,
Expand All @@ -10,6 +11,7 @@
YandexServerError,
)
from ycli.yandex.status.client import OAuthClient
from ycli.yandex.status.oauth_models import TokenResponse

DEVICE_CODE_URL = "https://oauth.yandex.ru/device/code"
TOKEN_URL = "https://oauth.yandex.ru/token"
Expand Down Expand Up @@ -61,7 +63,8 @@ def test_poll_token_success(api):
assert result.pending is False
assert result.error == ""
assert result.token is not None
assert result.token.access_token == "tok-abc"
assert result.token.access_token.get_secret_value() == "tok-abc"
assert "tok-abc" not in repr(result.token)


def test_poll_token_pending(api):
Expand Down Expand Up @@ -125,3 +128,9 @@ def test_a_lost_connection_is_a_typed_error(monkeypatch):
)
with pytest.raises(YandexConnectionError, match="ConnectError"):
_client().request_device_code()


def test_a_token_response_does_not_show_the_token_in_its_repr():
response = TokenResponse(access_token=SecretStr("tok-abc"))
assert "tok-abc" not in repr(response)
assert "tok-abc" not in str(response)
Loading