Skip to content

Commit 7151121

Browse files
authored
Optionally provide a StringIO for the login flow prompt (#78)
1 parent 2a97220 commit 7151121

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

cirro/api/auth/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from io import StringIO
2+
from typing import Optional
13
from cirro.api.auth.base import AuthInfo
24
from cirro.api.auth.iam import IAMAuth
35
from cirro.api.auth.oauth_client import ClientAuth
@@ -12,12 +14,13 @@
1214
from cirro.api.config import AppConfig
1315

1416

15-
def get_auth_info_from_config(app_config: AppConfig):
17+
def get_auth_info_from_config(app_config: AppConfig, auth_io: Optional[StringIO] = None):
1618
user_config = app_config.user_config
1719
if not user_config or not user_config.auth_method:
1820
return ClientAuth(region=app_config.region,
1921
client_id=app_config.client_id,
20-
auth_endpoint=app_config.auth_endpoint)
22+
auth_endpoint=app_config.auth_endpoint,
23+
auth_io=auth_io)
2124

2225
auth_methods = [
2326
ClientAuth,
@@ -33,7 +36,8 @@ def get_auth_info_from_config(app_config: AppConfig):
3336
return ClientAuth(region=app_config.region,
3437
client_id=app_config.client_id,
3538
auth_endpoint=app_config.auth_endpoint,
36-
enable_cache=auth_config.get('enable_cache') == 'True')
39+
enable_cache=auth_config.get('enable_cache') == 'True',
40+
auth_io=auth_io)
3741

3842
if matched_auth_method == IAMAuth and auth_config.get('load_current'):
3943
return IAMAuth.load_current()

cirro/api/auth/oauth_client.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from io import StringIO
12
import json
23
import logging
34
import sys
@@ -42,12 +43,15 @@ def _build_token_persistence(location, fallback_to_plaintext=False):
4243
return FilePersistence(location)
4344

4445

45-
def _authenticate(client_id: str, auth_endpoint: str):
46+
def _authenticate(client_id: str, auth_endpoint: str, auth_io: Optional[StringIO] = None):
4647
params = {'client_id': client_id}
4748
resp = requests.post(f'{auth_endpoint}/device-code', params=params)
4849
resp.raise_for_status()
4950
flow: DeviceTokenResponse = resp.json()
50-
print(flow['message'])
51+
if auth_io is None:
52+
print(flow['message'])
53+
else:
54+
auth_io.write(flow['message'])
5155
device_expiry = datetime.fromisoformat(flow['expiry'])
5256

5357
params = {
@@ -83,7 +87,14 @@ class ClientAuth(AuthInfo):
8387
Implements the OAuth device code flow
8488
This is the preferred way to authenticate
8589
"""
86-
def __init__(self, client_id: str, region: str, auth_endpoint: str, enable_cache=True):
90+
def __init__(
91+
self,
92+
client_id: str,
93+
region: str,
94+
auth_endpoint: str,
95+
enable_cache=True,
96+
auth_io: Optional[StringIO] = None
97+
):
8798
self.client_id = client_id
8899
self.region = region
89100
self._token_info: Optional[OAuthTokenResponse] = None
@@ -107,7 +118,7 @@ def __init__(self, client_id: str, region: str, auth_endpoint: str, enable_cache
107118
self._clear_token_info()
108119

109120
if not self._token_info:
110-
self._token_info = _authenticate(client_id=client_id, auth_endpoint=auth_endpoint)
121+
self._token_info = _authenticate(client_id=client_id, auth_endpoint=auth_endpoint, auth_io=auth_io)
111122

112123
self._save_token_info()
113124
self._update_token_metadata()

cirro/api/clients/portal.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44
from cirro.api.clients import ApiClient
55
from cirro.api.config import AppConfig
66
from cirro.api.services import DatasetService, ProcessService, ProjectService, FileService, CommonService
7+
from io import StringIO
78

89

910
class DataPortalClient:
1011
"""
1112
A client for interacting with the Cirro platform
1213
"""
13-
def __init__(self, auth_info: Optional[AuthInfo] = None, base_url: str = None):
14+
def __init__(self, auth_info: Optional[AuthInfo] = None, base_url: str = None, auth_io: Optional[StringIO] = None):
1415
self._configuration = AppConfig(base_url=base_url)
1516
if not auth_info:
16-
auth_info = get_auth_info_from_config(self._configuration)
17+
auth_info = get_auth_info_from_config(self._configuration, auth_io=auth_io)
1718

1819
self._api_client = ApiClient(auth_info, data_endpoint=self._configuration.data_endpoint)
1920
self._file_service = FileService(self._api_client, self._configuration)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "cirro"
3-
version = "0.7.2"
3+
version = "0.7.3"
44
description = "CLI tool and SDK for interacting with the Cirro platform"
55
authors = ["Cirro Bio <[email protected]>"]
66
license = "MIT"

0 commit comments

Comments
 (0)