From 4824e62f9a680fbecf0eef2d576093a1e0205891 Mon Sep 17 00:00:00 2001 From: Vladimir Lipkin Date: Tue, 10 Dec 2024 17:33:23 +0300 Subject: [PATCH] add oauth string auth helper --- src/yandex_cloud_ml_sdk/_auth.py | 3 +++ tests/auth/test_get_auth_provider.py | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/src/yandex_cloud_ml_sdk/_auth.py b/src/yandex_cloud_ml_sdk/_auth.py index 64d7fc3..ccd5b49 100644 --- a/src/yandex_cloud_ml_sdk/_auth.py +++ b/src/yandex_cloud_ml_sdk/_auth.py @@ -335,6 +335,7 @@ async def get_auth_provider( ) -> BaseAuth: simple_iam_regexp = re.compile(r'^t\d\.') iam_regexp = re.compile(r't1\.[A-Z0-9a-z_-]+[=]{0,2}\.[A-Z0-9a-z_-]{86}[=]{0,2}') + simple_oauth_regexp = re.compile(r'y[0123]_[-\w]') result: BaseAuth | None = None if isinstance(auth, str): @@ -347,6 +348,8 @@ async def get_auth_provider( UserWarning, stacklevel=2, ) + elif simple_oauth_regexp.match(auth): + result = OAuthTokenAuth(auth) else: result = APIKeyAuth(auth) elif isinstance(auth, BaseAuth): diff --git a/tests/auth/test_get_auth_provider.py b/tests/auth/test_get_auth_provider.py index 44ef9ae..29c37c8 100644 --- a/tests/auth/test_get_auth_provider.py +++ b/tests/auth/test_get_auth_provider.py @@ -46,6 +46,14 @@ async def test_input_data(monkeypatch): auth = await get_auth_provider(auth=NoAuth(), endpoint=None, yc_profile=None) assert isinstance(auth, NoAuth) + with pytest.warns(UserWarning, match=r"Sharing your personal OAuth token is not safe"): + auth = await get_auth_provider( + auth="y3_ABC-_-abc123", + endpoint=None, + yc_profile=None + ) + assert isinstance(auth, OAuthTokenAuth) + @pytest.mark.filterwarnings(r"ignore:.*OAuth:UserWarning") async def test_order(monkeypatch, mock_client, process_maker):