toucan-router-tools 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- toucan_router_tools-0.1.0/.gitignore +13 -0
- toucan_router_tools-0.1.0/LICENSE +21 -0
- toucan_router_tools-0.1.0/PKG-INFO +18 -0
- toucan_router_tools-0.1.0/pyproject.toml +33 -0
- toucan_router_tools-0.1.0/src/toucan_router_tools/__init__.py +0 -0
- toucan_router_tools-0.1.0/src/toucan_router_tools/sheets.py +349 -0
- toucan_router_tools-0.1.0/src/toucan_router_tools/upload_loom.py +412 -0
- toucan_router_tools-0.1.0/tests/test_sheets.py +363 -0
- toucan_router_tools-0.1.0/tests/test_upload_loom.py +86 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Toucan Router contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: toucan-router-tools
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Optional Loom and Google Sheets tools for Toucan Router.
|
|
5
|
+
Project-URL: Homepage, https://github.com/salalaslam/toucan-router
|
|
6
|
+
Project-URL: Repository, https://github.com/salalaslam/toucan-router
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Keywords: loom,sheets,toucan
|
|
10
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Requires-Python: >=3.11
|
|
14
|
+
Provides-Extra: test
|
|
15
|
+
Requires-Dist: pytest>=8; extra == 'test'
|
|
16
|
+
Description-Content-Type: text/plain
|
|
17
|
+
|
|
18
|
+
Optional tools for Toucan Router. See the project homepage for setup instructions.
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.24"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "toucan-router-tools"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Optional Loom and Google Sheets tools for Toucan Router."
|
|
9
|
+
readme = {text = "Optional tools for Toucan Router. See the project homepage for setup instructions.", content-type = "text/plain"}
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
keywords = ["toucan", "loom", "sheets"]
|
|
13
|
+
classifiers = ["Programming Language :: Python :: 3", "Programming Language :: Python :: 3.11", "Operating System :: POSIX :: Linux"]
|
|
14
|
+
requires-python = ">=3.11"
|
|
15
|
+
dependencies = []
|
|
16
|
+
|
|
17
|
+
[project.urls]
|
|
18
|
+
Homepage = "https://github.com/salalaslam/toucan-router"
|
|
19
|
+
Repository = "https://github.com/salalaslam/toucan-router"
|
|
20
|
+
|
|
21
|
+
[project.optional-dependencies]
|
|
22
|
+
test = ["pytest>=8"]
|
|
23
|
+
|
|
24
|
+
[project.scripts]
|
|
25
|
+
toucan-upload-loom = "toucan_router_tools.upload_loom:main"
|
|
26
|
+
toucan-sheets = "toucan_router_tools.sheets:main"
|
|
27
|
+
|
|
28
|
+
[tool.hatch.build.targets.wheel]
|
|
29
|
+
packages = ["src/toucan_router_tools"]
|
|
30
|
+
|
|
31
|
+
[tool.pytest.ini_options]
|
|
32
|
+
testpaths = ["tests"]
|
|
33
|
+
pythonpath = ["src"]
|
|
File without changes
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
"""Optional Google Sheets companion using a user's desktop OAuth client."""
|
|
2
|
+
import argparse
|
|
3
|
+
import base64
|
|
4
|
+
from contextlib import contextmanager
|
|
5
|
+
from datetime import datetime, timezone
|
|
6
|
+
import fcntl
|
|
7
|
+
import hashlib
|
|
8
|
+
import hmac
|
|
9
|
+
from http.server import BaseHTTPRequestHandler, HTTPServer
|
|
10
|
+
import json
|
|
11
|
+
import math
|
|
12
|
+
import os
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
import re
|
|
15
|
+
import secrets
|
|
16
|
+
import sys
|
|
17
|
+
import tempfile
|
|
18
|
+
import time
|
|
19
|
+
import urllib.error
|
|
20
|
+
import urllib.parse
|
|
21
|
+
import urllib.request
|
|
22
|
+
|
|
23
|
+
SCOPES = ['https://www.googleapis.com/auth/drive.file']
|
|
24
|
+
AUTHORIZE = 'https://accounts.google.com/o/oauth2/v2/auth'
|
|
25
|
+
TOKEN = 'https://oauth2.googleapis.com/token'
|
|
26
|
+
SHEETS = 'https://sheets.googleapis.com/v4/spreadsheets'
|
|
27
|
+
IDENTITY = 'https://www.googleapis.com/drive/v3/about?fields=user'
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class SheetsError(Exception):
|
|
31
|
+
def __init__(self, message, code=1):
|
|
32
|
+
super().__init__(message)
|
|
33
|
+
self.code = code
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def directory():
|
|
37
|
+
return Path(os.environ.get('TOUCAN_SHEETS_DIR') or '~/.config/toucan/sheets').expanduser()
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@contextmanager
|
|
41
|
+
def lock():
|
|
42
|
+
path = directory()
|
|
43
|
+
path.mkdir(parents=True, exist_ok=True, mode=0o700)
|
|
44
|
+
path.chmod(0o700)
|
|
45
|
+
fd = os.open(path / 'token.lock', os.O_CREAT | os.O_RDWR, 0o600)
|
|
46
|
+
with os.fdopen(fd, 'w') as output:
|
|
47
|
+
os.fchmod(output.fileno(), 0o600)
|
|
48
|
+
fcntl.flock(output.fileno(), fcntl.LOCK_EX)
|
|
49
|
+
yield
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def load_private(name):
|
|
53
|
+
path = directory() / name
|
|
54
|
+
if not path.is_file():
|
|
55
|
+
message = 'No desktop OAuth client. Add client.json before running toucan-sheets auth.' if name == 'client.json' else 'Not authenticated. Run toucan-sheets auth.'
|
|
56
|
+
raise SheetsError(message)
|
|
57
|
+
path.chmod(0o600)
|
|
58
|
+
try:
|
|
59
|
+
data = json.loads(path.read_text())
|
|
60
|
+
except (ValueError, UnicodeError):
|
|
61
|
+
raise SheetsError(f'Invalid {name}; expected a JSON object.', 2) from None
|
|
62
|
+
if not isinstance(data, dict):
|
|
63
|
+
raise SheetsError(f'Invalid {name}; expected a JSON object.', 2)
|
|
64
|
+
return data
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def client():
|
|
68
|
+
data = load_private('client.json').get('installed')
|
|
69
|
+
if not isinstance(data, dict) or not all(isinstance(data.get(key), str) and data[key] for key in ('client_id', 'client_secret')):
|
|
70
|
+
raise SheetsError('client.json must contain an installed desktop OAuth client.', 2)
|
|
71
|
+
return data
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def save(record):
|
|
75
|
+
# Callers hold token.lock across the read, refresh/authorization and replace.
|
|
76
|
+
fd, temporary = tempfile.mkstemp(prefix='.token-', dir=directory())
|
|
77
|
+
try:
|
|
78
|
+
with os.fdopen(fd, 'w') as output:
|
|
79
|
+
os.fchmod(output.fileno(), 0o600)
|
|
80
|
+
json.dump(record, output)
|
|
81
|
+
output.flush()
|
|
82
|
+
os.fsync(output.fileno())
|
|
83
|
+
os.replace(temporary, directory() / 'token.json')
|
|
84
|
+
finally:
|
|
85
|
+
if os.path.exists(temporary):
|
|
86
|
+
os.unlink(temporary)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
class NoRedirect(urllib.request.HTTPRedirectHandler):
|
|
90
|
+
def redirect_request(self, req, fp, code, msg, headers, newurl):
|
|
91
|
+
return None
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
HTTP = urllib.request.build_opener(NoRedirect)
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def request(url, method='GET', record=None, body=None, fields=None, sensitive=()):
|
|
98
|
+
headers = {'Accept': 'application/json'}
|
|
99
|
+
hidden = list(sensitive)
|
|
100
|
+
data = None
|
|
101
|
+
if record:
|
|
102
|
+
headers['Authorization'] = 'Bearer ' + record['access_token']
|
|
103
|
+
hidden.extend(record.get(key) for key in ('access_token', 'refresh_token'))
|
|
104
|
+
if fields is not None:
|
|
105
|
+
data = urllib.parse.urlencode(fields).encode()
|
|
106
|
+
headers['Content-Type'] = 'application/x-www-form-urlencoded'
|
|
107
|
+
hidden.extend(fields.get(key) for key in ('client_secret', 'refresh_token', 'code', 'code_verifier'))
|
|
108
|
+
elif body is not None:
|
|
109
|
+
data = json.dumps(body).encode()
|
|
110
|
+
headers['Content-Type'] = 'application/json'
|
|
111
|
+
req = urllib.request.Request(url, data=data, headers=headers, method=method)
|
|
112
|
+
try:
|
|
113
|
+
with HTTP.open(req, timeout=60) as response:
|
|
114
|
+
raw = response.read()
|
|
115
|
+
result = json.loads(raw) if raw else {}
|
|
116
|
+
except urllib.error.HTTPError as error:
|
|
117
|
+
message = 'Google API request failed'
|
|
118
|
+
try:
|
|
119
|
+
result = json.loads(error.read())
|
|
120
|
+
if isinstance(result, dict):
|
|
121
|
+
hidden.extend(result.get(key) for key in ('access_token', 'refresh_token', 'client_secret'))
|
|
122
|
+
detail = result.get('error', {})
|
|
123
|
+
if isinstance(detail, dict):
|
|
124
|
+
message = detail.get('message', message)
|
|
125
|
+
elif isinstance(detail, str):
|
|
126
|
+
message = result.get('error_description') or detail
|
|
127
|
+
except (ValueError, OSError):
|
|
128
|
+
pass
|
|
129
|
+
if not isinstance(message, str):
|
|
130
|
+
message = 'Google API request failed'
|
|
131
|
+
for value in sorted((v for v in hidden if isinstance(v, str) and v), key=len, reverse=True):
|
|
132
|
+
for variant in (value, urllib.parse.quote(value, safe=''), urllib.parse.quote_plus(value)):
|
|
133
|
+
message = message.replace(variant, '[redacted]')
|
|
134
|
+
message = re.sub(r'(?i)\b(?:authorization\s*[:=]\s*|bearer\s+)\S+', '[redacted]', message)
|
|
135
|
+
raise SheetsError(f'Google API HTTP {error.code}: {message}') from None
|
|
136
|
+
except (urllib.error.URLError, TimeoutError, OSError):
|
|
137
|
+
raise SheetsError('Google request failed due to a network error.') from None
|
|
138
|
+
except (ValueError, UnicodeError):
|
|
139
|
+
raise SheetsError('Google returned invalid JSON.') from None
|
|
140
|
+
if not isinstance(result, dict):
|
|
141
|
+
raise SheetsError('Google returned an invalid response.')
|
|
142
|
+
return result
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def token_record(response, previous=None):
|
|
146
|
+
access = response.get('access_token')
|
|
147
|
+
refresh = response.get('refresh_token') or (previous or {}).get('refresh_token')
|
|
148
|
+
lifetime = response.get('expires_in')
|
|
149
|
+
scopes = response.get('scope', ' '.join(SCOPES))
|
|
150
|
+
if not isinstance(scopes, str) or set(scopes.split()) != set(SCOPES):
|
|
151
|
+
raise SheetsError('Unexpected OAuth scopes; authenticate again with drive.file only.')
|
|
152
|
+
if not isinstance(access, str) or not access or isinstance(lifetime, bool) or not isinstance(lifetime, (int, float)) or not math.isfinite(lifetime) or lifetime <= 0:
|
|
153
|
+
raise SheetsError('Google returned incomplete credentials.')
|
|
154
|
+
if str(response.get('token_type', '')).lower() != 'bearer':
|
|
155
|
+
raise SheetsError('Google returned an unsupported token type.')
|
|
156
|
+
if not isinstance(refresh, str) or not refresh:
|
|
157
|
+
raise SheetsError('Google did not return an offline refresh token. Run toucan-sheets auth again.')
|
|
158
|
+
return dict(access_token=access, refresh_token=refresh, expires_at=time.time() + lifetime, scopes=SCOPES)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def credentials():
|
|
162
|
+
with lock():
|
|
163
|
+
app = client()
|
|
164
|
+
record = load_private('token.json')
|
|
165
|
+
if record.get('scopes') != SCOPES:
|
|
166
|
+
raise SheetsError('Unexpected OAuth scopes; authenticate again with drive.file only.')
|
|
167
|
+
expiry = record.get('expires_at')
|
|
168
|
+
if not isinstance(record.get('access_token'), str) or not record['access_token'] or isinstance(expiry, bool) or not isinstance(expiry, (int, float)) or not math.isfinite(expiry):
|
|
169
|
+
raise SheetsError('Invalid token.json. Run toucan-sheets auth --force.')
|
|
170
|
+
if expiry <= time.time() + 120:
|
|
171
|
+
if not isinstance(record.get('refresh_token'), str) or not record['refresh_token']:
|
|
172
|
+
raise SheetsError('No refresh token. Run toucan-sheets auth --force.')
|
|
173
|
+
response = request(TOKEN, 'POST', fields={
|
|
174
|
+
'grant_type': 'refresh_token', 'client_id': app['client_id'],
|
|
175
|
+
'client_secret': app['client_secret'], 'refresh_token': record['refresh_token'],
|
|
176
|
+
}, sensitive=[record['access_token']])
|
|
177
|
+
record = token_record(response, record)
|
|
178
|
+
save(record)
|
|
179
|
+
return record
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def identity(record, sensitive=()):
|
|
183
|
+
data = request(IDENTITY, record=record, sensitive=sensitive)
|
|
184
|
+
user = data.get('user')
|
|
185
|
+
if not isinstance(user, dict) or not isinstance(user.get('emailAddress'), str) or not user['emailAddress']:
|
|
186
|
+
raise SheetsError('Google did not return an account email.')
|
|
187
|
+
return user['emailAddress']
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
class CallbackHandler(BaseHTTPRequestHandler):
|
|
191
|
+
def log_message(self, format, *args):
|
|
192
|
+
# The request URL contains the authorization code and must never be logged.
|
|
193
|
+
pass
|
|
194
|
+
|
|
195
|
+
def do_GET(self):
|
|
196
|
+
parsed = urllib.parse.urlsplit(self.path)
|
|
197
|
+
query = urllib.parse.parse_qs(parsed.query, keep_blank_values=True)
|
|
198
|
+
states = query.get('state', [])
|
|
199
|
+
valid = parsed.path == '/' and len(states) == 1 and hmac.compare_digest(states[0].encode(), self.server.oauth_state.encode())
|
|
200
|
+
codes = query.get('code', [])
|
|
201
|
+
errors = query.get('error', [])
|
|
202
|
+
if not valid or not ((len(codes) == 1 and codes[0] and not errors) or (len(errors) == 1 and errors[0] and not codes)):
|
|
203
|
+
self.send_response(400)
|
|
204
|
+
message = b'Invalid OAuth callback.'
|
|
205
|
+
else:
|
|
206
|
+
self.server.code = codes[0] if codes else None
|
|
207
|
+
self.server.denied = bool(errors)
|
|
208
|
+
self.send_response(200)
|
|
209
|
+
message = b'Toucan authorization received. You may close this tab.'
|
|
210
|
+
self.send_header('Content-Type', 'text/plain; charset=utf-8')
|
|
211
|
+
self.send_header('Content-Length', str(len(message)))
|
|
212
|
+
self.end_headers()
|
|
213
|
+
self.wfile.write(message)
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
class CallbackServer(HTTPServer):
|
|
217
|
+
def get_request(self):
|
|
218
|
+
connection, address = super().get_request()
|
|
219
|
+
connection.settimeout(1)
|
|
220
|
+
return connection, address
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def auth(port=8765, force=False, account=None):
|
|
224
|
+
if not 1 <= port <= 65535:
|
|
225
|
+
raise SheetsError('Port must be between 1 and 65535.', 2)
|
|
226
|
+
with lock():
|
|
227
|
+
if (directory() / 'token.json').exists() and not force:
|
|
228
|
+
raise SheetsError('A token already exists. Check toucan-sheets status or use auth --force.', 3)
|
|
229
|
+
app = client()
|
|
230
|
+
verifier = secrets.token_urlsafe(64)
|
|
231
|
+
challenge = base64.urlsafe_b64encode(hashlib.sha256(verifier.encode()).digest()).decode().rstrip('=')
|
|
232
|
+
state = secrets.token_urlsafe(32)
|
|
233
|
+
redirect = f'http://127.0.0.1:{port}/'
|
|
234
|
+
fields = dict(client_id=app['client_id'], redirect_uri=redirect, response_type='code',
|
|
235
|
+
scope=' '.join(SCOPES), state=state, code_challenge=challenge,
|
|
236
|
+
code_challenge_method='S256', access_type='offline', prompt='consent')
|
|
237
|
+
if account:
|
|
238
|
+
fields['login_hint'] = account
|
|
239
|
+
with CallbackServer(('127.0.0.1', port), CallbackHandler) as server:
|
|
240
|
+
server.oauth_state, server.code, server.denied = state, None, False
|
|
241
|
+
print(json.dumps({'authorization_url': AUTHORIZE + '?' + urllib.parse.urlencode(fields)}), file=sys.stderr, flush=True)
|
|
242
|
+
deadline = time.monotonic() + 900
|
|
243
|
+
while not server.code and not server.denied:
|
|
244
|
+
remaining = deadline - time.monotonic()
|
|
245
|
+
if remaining <= 0:
|
|
246
|
+
raise SheetsError('Authorization timed out after 15 minutes. Run toucan-sheets auth again.')
|
|
247
|
+
server.timeout = min(1, remaining)
|
|
248
|
+
server.handle_request()
|
|
249
|
+
if server.denied:
|
|
250
|
+
raise SheetsError('Google authorization was denied.')
|
|
251
|
+
code = server.code
|
|
252
|
+
response = request(TOKEN, 'POST', fields=dict(
|
|
253
|
+
grant_type='authorization_code', client_id=app['client_id'], client_secret=app['client_secret'],
|
|
254
|
+
code=code, code_verifier=verifier, redirect_uri=redirect))
|
|
255
|
+
record = token_record(response)
|
|
256
|
+
email = identity(record, sensitive=[app['client_secret'], code, verifier])
|
|
257
|
+
if account and email.casefold() != account.casefold():
|
|
258
|
+
raise SheetsError('Authenticated account does not match --account; no token was saved.', 3)
|
|
259
|
+
save(record)
|
|
260
|
+
return dict(authenticated=True, email=email, scopes=SCOPES)
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
def json_input(path):
|
|
264
|
+
try:
|
|
265
|
+
if path == '-':
|
|
266
|
+
return json.load(sys.stdin)
|
|
267
|
+
with open(path) as source:
|
|
268
|
+
return json.load(source)
|
|
269
|
+
except (OSError, ValueError, UnicodeError):
|
|
270
|
+
raise SheetsError('Could not read input; expected a JSON file or - for stdin.', 2) from None
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
class Parser(argparse.ArgumentParser):
|
|
274
|
+
def error(self, message):
|
|
275
|
+
# argparse may include an invalid argument verbatim in its error text.
|
|
276
|
+
raise SheetsError('Invalid command or arguments. Use toucan-sheets --help.', 2)
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
def main(argv=None):
|
|
280
|
+
parser = Parser(prog='toucan-sheets', description=__doc__)
|
|
281
|
+
sub = parser.add_subparsers(dest='command', required=True)
|
|
282
|
+
login = sub.add_parser('auth')
|
|
283
|
+
login.add_argument('--port', type=int, default=8765)
|
|
284
|
+
login.add_argument('--force', action='store_true')
|
|
285
|
+
login.add_argument('--account')
|
|
286
|
+
sub.add_parser('status')
|
|
287
|
+
create = sub.add_parser('create')
|
|
288
|
+
create.add_argument('title')
|
|
289
|
+
create.add_argument('--values')
|
|
290
|
+
create.add_argument('--range', default='Sheet1!A1')
|
|
291
|
+
read = sub.add_parser('read')
|
|
292
|
+
read.add_argument('spreadsheet_id')
|
|
293
|
+
read.add_argument('range')
|
|
294
|
+
write = sub.add_parser('write')
|
|
295
|
+
write.add_argument('spreadsheet_id')
|
|
296
|
+
write.add_argument('range')
|
|
297
|
+
write.add_argument('values')
|
|
298
|
+
write.add_argument('--input-option', choices=['RAW', 'USER_ENTERED'], default='RAW')
|
|
299
|
+
batch = sub.add_parser('batch-update')
|
|
300
|
+
batch.add_argument('spreadsheet_id')
|
|
301
|
+
batch.add_argument('body')
|
|
302
|
+
for command in sub.choices.values():
|
|
303
|
+
command.add_argument('--json', action='store_true', help='Output JSON (the default)')
|
|
304
|
+
options = None
|
|
305
|
+
try:
|
|
306
|
+
options = parser.parse_args(argv)
|
|
307
|
+
if options.command == 'auth':
|
|
308
|
+
result = auth(options.port, options.force, options.account)
|
|
309
|
+
else:
|
|
310
|
+
record = credentials()
|
|
311
|
+
if options.command == 'status':
|
|
312
|
+
result = dict(authenticated=True, email=identity(record), scopes=record['scopes'],
|
|
313
|
+
has_refresh_token=bool(record.get('refresh_token')),
|
|
314
|
+
expiry=datetime.fromtimestamp(record['expires_at'], timezone.utc).isoformat())
|
|
315
|
+
elif options.command == 'create':
|
|
316
|
+
result = request(SHEETS, 'POST', record, body={'properties': {'title': options.title}})
|
|
317
|
+
if options.values:
|
|
318
|
+
try:
|
|
319
|
+
url = SHEETS + '/' + urllib.parse.quote(result['spreadsheetId'], safe='') + '/values/' + urllib.parse.quote(options.range, safe='')
|
|
320
|
+
result['initialValuesUpdate'] = request(url + '?valueInputOption=RAW', 'PUT', record,
|
|
321
|
+
body={'values': json_input(options.values)})
|
|
322
|
+
except Exception:
|
|
323
|
+
print(json.dumps(dict(createdSpreadsheetId=result.get('spreadsheetId'),
|
|
324
|
+
spreadsheetUrl=result.get('spreadsheetUrl'), initialValuesWritten=False)), file=sys.stderr)
|
|
325
|
+
raise
|
|
326
|
+
else:
|
|
327
|
+
url = SHEETS + '/' + urllib.parse.quote(options.spreadsheet_id, safe='')
|
|
328
|
+
if options.command == 'batch-update':
|
|
329
|
+
result = request(url + ':batchUpdate', 'POST', record, body=json_input(options.body))
|
|
330
|
+
else:
|
|
331
|
+
url += '/values/' + urllib.parse.quote(options.range, safe='')
|
|
332
|
+
if options.command == 'read':
|
|
333
|
+
result = request(url, record=record)
|
|
334
|
+
else:
|
|
335
|
+
result = request(url + '?valueInputOption=' + options.input_option, 'PUT', record,
|
|
336
|
+
body={'values': json_input(options.values)})
|
|
337
|
+
print(json.dumps(result, indent=2))
|
|
338
|
+
return 0
|
|
339
|
+
except (SheetsError, OSError, ValueError, KeyError, TypeError, OverflowError) as error:
|
|
340
|
+
reason = str(error) if isinstance(error, SheetsError) else 'Could not complete the Sheets operation; check local files and configuration.'
|
|
341
|
+
if options and options.command == 'status':
|
|
342
|
+
print(json.dumps(dict(authenticated=False, reason=reason)))
|
|
343
|
+
return 1
|
|
344
|
+
print(json.dumps({'error': reason}), file=sys.stderr)
|
|
345
|
+
return error.code if isinstance(error, SheetsError) else 1
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
if __name__ == '__main__':
|
|
349
|
+
sys.exit(main())
|
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
"""EXPERIMENTAL: uses Loom's unofficial API and may break."""
|
|
2
|
+
import argparse
|
|
3
|
+
import base64
|
|
4
|
+
from concurrent.futures import ThreadPoolExecutor
|
|
5
|
+
import fcntl
|
|
6
|
+
import hashlib
|
|
7
|
+
import hmac
|
|
8
|
+
import json
|
|
9
|
+
import os
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
import re
|
|
12
|
+
import sys
|
|
13
|
+
import tempfile
|
|
14
|
+
import time
|
|
15
|
+
import urllib.error
|
|
16
|
+
import urllib.parse
|
|
17
|
+
import urllib.request
|
|
18
|
+
import uuid
|
|
19
|
+
from datetime import datetime, timezone
|
|
20
|
+
|
|
21
|
+
CLIENT_ID = os.environ.get("TOUCAN_LOOM_CLIENT_ID", "zjAE9OPtU9ercrK02sZowEPwoPlrRPTg")
|
|
22
|
+
AUTH_BASE = "https://auth.atlassian.com"
|
|
23
|
+
SCOPES = "read:loom:twg-cli write:loom:twg-cli offline_access"
|
|
24
|
+
CONFIG = Path.home() / ".config/toucan/loom"
|
|
25
|
+
CREDENTIALS = CONFIG / "oauth.json"
|
|
26
|
+
GRAPHQL = "https://api.atlassian.com/graphql"
|
|
27
|
+
PART_SIZE = 5 * 1024 * 1024
|
|
28
|
+
CREATE_UPLOAD = """mutation TwgCLI_LoomCreateUploadCredentials($siteId: ID!) {
|
|
29
|
+
loom_createFileUploadCredentials(siteId: $siteId) {
|
|
30
|
+
success credentials { __typename
|
|
31
|
+
... on LoomS3UploadInfo { credentialType accessKeyId bucket durationSeconds region path secretAccessKey sessionToken endpoint }
|
|
32
|
+
... on LoomObjectStoreUploadInfo { credentialType url hostname bucket workspaceType siteId activationId token { value expiration } }
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}"""
|
|
36
|
+
COMPLETE_UPLOAD = """mutation TwgCLI_LoomCompleteVideoUpload($input: LoomCompleteVideoFileUploadInput!) {
|
|
37
|
+
loom_completeVideoFileUpload(input: $input) { success video { id name url isComplete playableDuration } }
|
|
38
|
+
}"""
|
|
39
|
+
SPACE_QUERY = """query TwgCLI_LoomSpaceActivation($siteId: ID!) {
|
|
40
|
+
loom_spacesSearch(siteId: $siteId) { id }
|
|
41
|
+
}"""
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class AuthError(Exception):
|
|
46
|
+
pass
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def request(path, fields):
|
|
50
|
+
req = urllib.request.Request(
|
|
51
|
+
AUTH_BASE + path,
|
|
52
|
+
data=urllib.parse.urlencode(fields).encode(),
|
|
53
|
+
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"},
|
|
54
|
+
)
|
|
55
|
+
try:
|
|
56
|
+
with urllib.request.urlopen(req, timeout=30) as response:
|
|
57
|
+
result = json.load(response)
|
|
58
|
+
except urllib.error.HTTPError as error:
|
|
59
|
+
try:
|
|
60
|
+
result = json.loads(error.read())
|
|
61
|
+
except (ValueError, OSError):
|
|
62
|
+
raise AuthError(f"Atlassian authentication returned HTTP {error.code}.") from None
|
|
63
|
+
if not isinstance(result, dict):
|
|
64
|
+
raise AuthError(f"Atlassian authentication returned HTTP {error.code}.") from None
|
|
65
|
+
result["_http_status"] = error.code
|
|
66
|
+
except (urllib.error.URLError, TimeoutError, OSError, ValueError):
|
|
67
|
+
raise AuthError("Could not reach Atlassian authentication. No credentials were changed.") from None
|
|
68
|
+
if not isinstance(result, dict):
|
|
69
|
+
raise AuthError("Atlassian returned an invalid authentication response.")
|
|
70
|
+
return result
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def provider_error(result):
|
|
74
|
+
# Provider descriptions can contain user-controlled or credential material.
|
|
75
|
+
code = result.get("error", "invalid_response")
|
|
76
|
+
if not isinstance(code, str) or not re.fullmatch(r"[a-zA-Z0-9_]{1,80}", code):
|
|
77
|
+
code = "authentication_failed"
|
|
78
|
+
return AuthError(f"Atlassian authentication failed: {code}. Run toucan-upload-loom login to authenticate again.")
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def prepare_directory():
|
|
82
|
+
CONFIG.parent.mkdir(parents=True, exist_ok=True, mode=0o700)
|
|
83
|
+
CONFIG.mkdir(exist_ok=True, mode=0o700)
|
|
84
|
+
CONFIG.chmod(0o700)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def lock():
|
|
88
|
+
prepare_directory()
|
|
89
|
+
fd = os.open(CONFIG / "auth.lock", os.O_CREAT | os.O_RDWR, 0o600)
|
|
90
|
+
os.fchmod(fd, 0o600)
|
|
91
|
+
fcntl.flock(fd, fcntl.LOCK_EX)
|
|
92
|
+
return os.fdopen(fd, "w")
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def token_record(response, previous=None):
|
|
96
|
+
if response.get("error"):
|
|
97
|
+
raise provider_error(response)
|
|
98
|
+
access = response.get("access_token")
|
|
99
|
+
lifetime = response.get("expires_in")
|
|
100
|
+
refresh = response.get("refresh_token") or (previous or {}).get("refresh_token")
|
|
101
|
+
if not isinstance(access, str) or not access or not isinstance(lifetime, (int, float)) or lifetime <= 0:
|
|
102
|
+
raise AuthError("Atlassian returned incomplete credentials.")
|
|
103
|
+
if str(response.get("token_type", "")).lower() != "bearer":
|
|
104
|
+
raise AuthError("Atlassian returned an unsupported token type.")
|
|
105
|
+
if not isinstance(refresh, str) or not refresh:
|
|
106
|
+
raise AuthError("Atlassian did not grant offline access. Persistent login was not saved.")
|
|
107
|
+
try:
|
|
108
|
+
segment = access.split(".")[1]
|
|
109
|
+
claims = json.loads(base64.urlsafe_b64decode(segment + "=" * (-len(segment) % 4)))
|
|
110
|
+
audiences = claims.get("aud", [])
|
|
111
|
+
audiences = audiences if isinstance(audiences, list) else [audiences]
|
|
112
|
+
sites = sorted({m.group(1).lower() for value in audiences if isinstance(value, str)
|
|
113
|
+
if (m := re.fullmatch(r"ari:cloud:[a-z0-9-]+::site/([0-9a-fA-F-]{36})", value))})
|
|
114
|
+
except (ValueError, IndexError, TypeError):
|
|
115
|
+
raise AuthError("Could not identify the authorized site from the OAuth token.") from None
|
|
116
|
+
if len(sites) != 1:
|
|
117
|
+
raise AuthError("Authorize exactly one Loom site before saving this login.")
|
|
118
|
+
return {"access_token": access, "refresh_token": refresh,
|
|
119
|
+
"expires_at": time.time() + lifetime, "cloud_id": sites[0],
|
|
120
|
+
"scope": response.get("scope", SCOPES)}
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def save(record):
|
|
124
|
+
prepare_directory()
|
|
125
|
+
fd, temp = tempfile.mkstemp(prefix="oauth-", dir=CONFIG)
|
|
126
|
+
try:
|
|
127
|
+
os.fchmod(fd, 0o600)
|
|
128
|
+
with os.fdopen(fd, "w") as output:
|
|
129
|
+
json.dump(record, output)
|
|
130
|
+
output.flush()
|
|
131
|
+
os.fsync(output.fileno())
|
|
132
|
+
os.replace(temp, CREDENTIALS)
|
|
133
|
+
finally:
|
|
134
|
+
if os.path.exists(temp):
|
|
135
|
+
os.unlink(temp)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def login(force=False):
|
|
139
|
+
with lock():
|
|
140
|
+
if CREDENTIALS.exists() and not force:
|
|
141
|
+
raise AuthError("A login is already saved. Use toucan-upload-loom status, or toucan-upload-loom login --force to reauthenticate.")
|
|
142
|
+
response = request("/oauth/device/code", {"client_id": CLIENT_ID, "scope": SCOPES})
|
|
143
|
+
if response.get("error"):
|
|
144
|
+
raise provider_error(response)
|
|
145
|
+
code = response.get("device_code")
|
|
146
|
+
url = response.get("verification_uri_complete") or response.get("verification_uri")
|
|
147
|
+
expiry = response.get("expires_in")
|
|
148
|
+
if not isinstance(code, str) or not isinstance(url, str) or not isinstance(expiry, (int, float)):
|
|
149
|
+
raise AuthError("Atlassian returned an incomplete device authorization.")
|
|
150
|
+
if not url.startswith("https://auth.atlassian.com/"):
|
|
151
|
+
raise AuthError("Atlassian returned an unexpected authorization URL.")
|
|
152
|
+
print("Open this authorization URL in your browser:", flush=True)
|
|
153
|
+
print(url, flush=True)
|
|
154
|
+
print("Authorize your Loom workspace.", flush=True)
|
|
155
|
+
interval = max(float(response.get("interval", 5)), 5)
|
|
156
|
+
deadline = time.monotonic() + min(float(expiry), 900)
|
|
157
|
+
while time.monotonic() < deadline:
|
|
158
|
+
time.sleep(interval)
|
|
159
|
+
token = request("/oauth/token", {"grant_type": "urn:ietf:params:oauth:grant-type:device_code",
|
|
160
|
+
"client_id": CLIENT_ID, "device_code": code})
|
|
161
|
+
error = token.get("error")
|
|
162
|
+
if error == "authorization_pending":
|
|
163
|
+
continue
|
|
164
|
+
if error == "slow_down":
|
|
165
|
+
interval += 5
|
|
166
|
+
continue
|
|
167
|
+
if error:
|
|
168
|
+
raise provider_error(token)
|
|
169
|
+
record = token_record(token)
|
|
170
|
+
save(record)
|
|
171
|
+
print("Loom login saved. Automatic refresh is available.", flush=True)
|
|
172
|
+
return
|
|
173
|
+
raise AuthError("The authorization window expired. Run toucan-upload-loom login again.")
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def credentials(force_refresh=False):
|
|
177
|
+
with lock():
|
|
178
|
+
if not CREDENTIALS.exists():
|
|
179
|
+
raise AuthError("No Loom login is saved. Run toucan-upload-loom login first.")
|
|
180
|
+
with CREDENTIALS.open() as source:
|
|
181
|
+
record = json.load(source)
|
|
182
|
+
if force_refresh or record["expires_at"] <= time.time() + 120:
|
|
183
|
+
response = request("/oauth/token", {"grant_type": "refresh_token", "client_id": CLIENT_ID,
|
|
184
|
+
"refresh_token": record["refresh_token"]})
|
|
185
|
+
record = token_record(response, record)
|
|
186
|
+
save(record)
|
|
187
|
+
return record
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
class NoRedirect(urllib.request.HTTPRedirectHandler):
|
|
191
|
+
def redirect_request(self, req, fp, code, msg, headers, newurl):
|
|
192
|
+
return None
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
HTTP = urllib.request.build_opener(NoRedirect)
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def fetch(url, method="GET", headers=None, data=None, timeout=60):
|
|
199
|
+
if urllib.parse.urlsplit(url).scheme != "https":
|
|
200
|
+
raise AuthError("Refusing an upload endpoint without HTTPS.")
|
|
201
|
+
req = urllib.request.Request(url, data=data, headers=headers or {}, method=method)
|
|
202
|
+
try:
|
|
203
|
+
with HTTP.open(req, timeout=timeout) as response:
|
|
204
|
+
return response.read()
|
|
205
|
+
except urllib.error.HTTPError as error:
|
|
206
|
+
raise AuthError(f"{method} request failed with HTTP {error.code}.") from None
|
|
207
|
+
except (urllib.error.URLError, TimeoutError, OSError):
|
|
208
|
+
raise AuthError(f"{method} request failed due to a network error.") from None
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def gql(record, query, variables):
|
|
212
|
+
body = json.dumps({"query": query, "variables": variables}).encode()
|
|
213
|
+
raw = fetch(GRAPHQL, "POST", {"Content-Type": "application/json",
|
|
214
|
+
"Authorization": "Bearer " + record["access_token"]}, body)
|
|
215
|
+
result = json.loads(raw)
|
|
216
|
+
if result.get("errors"):
|
|
217
|
+
raise AuthError("Loom API rejected the operation.")
|
|
218
|
+
if not isinstance(result.get("data"), dict):
|
|
219
|
+
raise AuthError("Loom API returned an incomplete response.")
|
|
220
|
+
return result["data"]
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def activation(record):
|
|
224
|
+
spaces = gql(record, SPACE_QUERY, {"siteId": record["cloud_id"]})["loom_spacesSearch"]
|
|
225
|
+
values = {match.group(1) for space in spaces
|
|
226
|
+
if (match := re.search(r":space/activation/([^/]+)/", space["id"]))}
|
|
227
|
+
if len(values) != 1:
|
|
228
|
+
raise AuthError("Could not identify exactly one authorized Loom workspace.")
|
|
229
|
+
return values.pop()
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
def with_token(url, token, **parameters):
|
|
233
|
+
parsed = urllib.parse.urlsplit(url)
|
|
234
|
+
query = dict(urllib.parse.parse_qsl(parsed.query))
|
|
235
|
+
query.update(authToken=token, **parameters)
|
|
236
|
+
return urllib.parse.urlunsplit(parsed._replace(query=urllib.parse.urlencode(query)))
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def object_upload(path, credentials, content_type, key, cloud_id):
|
|
240
|
+
try:
|
|
241
|
+
token = credentials["token"]["value"]
|
|
242
|
+
segment = token.split(".")[1]
|
|
243
|
+
claims = json.loads(base64.urlsafe_b64decode(segment + "=" * (-len(segment) % 4)))
|
|
244
|
+
prefix = claims["oid"].split(".*")[0].split(".+")[0].strip()
|
|
245
|
+
except (KeyError, IndexError, ValueError):
|
|
246
|
+
raise AuthError("Loom returned an invalid storage credential.") from None
|
|
247
|
+
if not prefix or credentials["siteId"] != cloud_id:
|
|
248
|
+
raise AuthError("Loom returned storage credentials for an unexpected site.")
|
|
249
|
+
base = credentials["hostname"].rstrip("/")
|
|
250
|
+
pieces = [credentials[k] for k in ("bucket", "workspaceType", "siteId", "activationId")]
|
|
251
|
+
base += "/" + urllib.parse.quote(pieces[0], safe="") + "/upload/" + "/".join(urllib.parse.quote(p, safe="") for p in pieces[1:]) + "/multipart"
|
|
252
|
+
routing = base64.b64encode(json.dumps({"entityType": "site", "entityId": credentials["siteId"]}, separators=(",", ":")).encode()).decode().rstrip("=")
|
|
253
|
+
routed = {"X-Loom-Entity-Routing": routing}
|
|
254
|
+
start = with_token(base, token, objectId=prefix + key, contentType=content_type, shouldOverwrite="false")
|
|
255
|
+
initiated = json.loads(fetch(start, "POST", {**routed, "x-content-checksum-type": "SHA256", "x-time-to-live": "PT24H"}, b""))
|
|
256
|
+
upload_id = initiated.get("uploadId")
|
|
257
|
+
if not isinstance(upload_id, str) or not upload_id:
|
|
258
|
+
raise AuthError("Loom storage returned no upload ID.")
|
|
259
|
+
upload_base = base + "/" + urllib.parse.quote(upload_id, safe="")
|
|
260
|
+
size = path.stat().st_size
|
|
261
|
+
count = (size + PART_SIZE - 1) // PART_SIZE
|
|
262
|
+
|
|
263
|
+
def part(number):
|
|
264
|
+
with path.open("rb") as source:
|
|
265
|
+
source.seek((number - 1) * PART_SIZE)
|
|
266
|
+
data = source.read(min(PART_SIZE, size - (number - 1) * PART_SIZE))
|
|
267
|
+
expected = min(PART_SIZE, size - (number - 1) * PART_SIZE)
|
|
268
|
+
if len(data) != expected:
|
|
269
|
+
raise AuthError("The source video changed during upload.")
|
|
270
|
+
headers = {**routed, "Content-Type": "application/octet-stream", "Content-Length": str(len(data)),
|
|
271
|
+
"x-content-checksum": base64.b64encode(hashlib.sha256(data).digest()).decode(),
|
|
272
|
+
"x-content-checksum-type": "SHA256", "x-part-number": str(number)}
|
|
273
|
+
for attempt in range(3):
|
|
274
|
+
try:
|
|
275
|
+
fetch(with_token(upload_base + "/parts", token), "PUT", headers, data, timeout=180)
|
|
276
|
+
return
|
|
277
|
+
except AuthError as error:
|
|
278
|
+
# Replacing the same numbered part is idempotent; only transient failures retry.
|
|
279
|
+
transient = "network error" in str(error) or any(f"HTTP {code}" in str(error) for code in (429, 500, 502, 503, 504))
|
|
280
|
+
if not transient or attempt == 2:
|
|
281
|
+
raise
|
|
282
|
+
time.sleep(0.5 * (2 ** attempt))
|
|
283
|
+
try:
|
|
284
|
+
with ThreadPoolExecutor(max_workers=min(4, count)) as executor:
|
|
285
|
+
list(executor.map(part, range(1, count + 1)))
|
|
286
|
+
fetch(with_token(upload_base + "/complete", token), "POST", routed, b"", timeout=180)
|
|
287
|
+
except BaseException:
|
|
288
|
+
try:
|
|
289
|
+
fetch(with_token(upload_base, token), "DELETE", routed)
|
|
290
|
+
except AuthError:
|
|
291
|
+
pass
|
|
292
|
+
raise
|
|
293
|
+
return key, {"mode": "tdp-multipart", "bytes": size, "parts": count}
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
def s3_upload(path, credentials, content_type, key):
|
|
297
|
+
endpoint = credentials.get("endpoint", "").strip()
|
|
298
|
+
if endpoint and not endpoint.startswith("https://"):
|
|
299
|
+
if not re.fullmatch(r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z", endpoint):
|
|
300
|
+
raise AuthError("Loom returned an invalid S3 endpoint.")
|
|
301
|
+
key = endpoint + "/" + key
|
|
302
|
+
endpoint = ""
|
|
303
|
+
endpoint = endpoint or f"https://{credentials['bucket']}.s3.{credentials['region']}.amazonaws.com"
|
|
304
|
+
prefix = credentials["path"].rstrip("/") + "/"
|
|
305
|
+
if not prefix.startswith("uploads/"):
|
|
306
|
+
raise AuthError("Loom returned an unsupported S3 path.")
|
|
307
|
+
full_key = prefix + key
|
|
308
|
+
completion_key = full_key[len("uploads/"):].split("/", 1)[1]
|
|
309
|
+
url = endpoint.rstrip("/") + "/" + "/".join(urllib.parse.quote(p, safe="") for p in full_key.split("/"))
|
|
310
|
+
digest = hashlib.sha256()
|
|
311
|
+
with path.open("rb") as source:
|
|
312
|
+
for chunk in iter(lambda: source.read(1024 * 1024), b""):
|
|
313
|
+
digest.update(chunk)
|
|
314
|
+
payload_hash = digest.hexdigest()
|
|
315
|
+
now = datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%SZ")
|
|
316
|
+
parsed = urllib.parse.urlsplit(url)
|
|
317
|
+
headers = {"content-type": content_type, "host": parsed.netloc, "x-amz-content-sha256": payload_hash,
|
|
318
|
+
"x-amz-date": now, "x-amz-security-token": credentials["sessionToken"]}
|
|
319
|
+
signed = ";".join(sorted(headers))
|
|
320
|
+
canonical_headers = "".join(k + ":" + headers[k].strip() + "\n" for k in sorted(headers))
|
|
321
|
+
canonical = "\n".join(["PUT", parsed.path, parsed.query, canonical_headers, signed, payload_hash])
|
|
322
|
+
scope = f"{now[:8]}/{credentials['region']}/s3/aws4_request"
|
|
323
|
+
string = "\n".join(["AWS4-HMAC-SHA256", now, scope, hashlib.sha256(canonical.encode()).hexdigest()])
|
|
324
|
+
signing_key = ("AWS4" + credentials["secretAccessKey"]).encode()
|
|
325
|
+
for value in (now[:8], credentials["region"], "s3", "aws4_request"):
|
|
326
|
+
signing_key = hmac.new(signing_key, value.encode(), hashlib.sha256).digest()
|
|
327
|
+
signature = hmac.new(signing_key, string.encode(), hashlib.sha256).hexdigest()
|
|
328
|
+
headers["Authorization"] = f"AWS4-HMAC-SHA256 Credential={credentials['accessKeyId']}/{scope}, SignedHeaders={signed}, Signature={signature}"
|
|
329
|
+
headers["Content-Length"] = str(path.stat().st_size)
|
|
330
|
+
with path.open("rb") as source:
|
|
331
|
+
fetch(url, "PUT", headers, source, timeout=600)
|
|
332
|
+
return completion_key, {"mode": "s3", "bytes": path.stat().st_size}
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
def upload(record, args):
|
|
336
|
+
parser = argparse.ArgumentParser(prog="toucan-upload-loom upload")
|
|
337
|
+
parser.add_argument("file", type=Path)
|
|
338
|
+
parser.add_argument("--title")
|
|
339
|
+
parser.add_argument("--content-type", default="video/mp4")
|
|
340
|
+
options = parser.parse_args(args)
|
|
341
|
+
path = options.file.resolve()
|
|
342
|
+
if not path.is_file() or path.stat().st_size <= 0 or not path.suffix:
|
|
343
|
+
raise AuthError("Choose a readable, nonempty video file with an extension.")
|
|
344
|
+
if path.stat().st_size > 4 * 1024 ** 3:
|
|
345
|
+
raise AuthError("The video exceeds Loom's 4 GB upload limit.")
|
|
346
|
+
filename = options.title.strip() if options.title else path.name
|
|
347
|
+
if not Path(filename).suffix:
|
|
348
|
+
filename += path.suffix
|
|
349
|
+
if len(filename) > 255:
|
|
350
|
+
raise AuthError("The video title must be at most 255 characters.")
|
|
351
|
+
workspace = activation(record)
|
|
352
|
+
created = gql(record, CREATE_UPLOAD, {"siteId": record["cloud_id"]})["loom_createFileUploadCredentials"]
|
|
353
|
+
if not created or not created.get("success") or not created.get("credentials"):
|
|
354
|
+
raise AuthError("Loom did not issue upload credentials.")
|
|
355
|
+
storage = created["credentials"]
|
|
356
|
+
key = uuid.uuid4().hex + path.suffix.lower()
|
|
357
|
+
if storage["__typename"] == "LoomObjectStoreUploadInfo":
|
|
358
|
+
if storage["activationId"] != workspace:
|
|
359
|
+
raise AuthError("Loom returned storage credentials for another workspace.")
|
|
360
|
+
key_path, _ = object_upload(path, storage, options.content_type, key, record["cloud_id"])
|
|
361
|
+
elif storage["__typename"] == "LoomS3UploadInfo":
|
|
362
|
+
key_path, _ = s3_upload(path, storage, options.content_type, key)
|
|
363
|
+
else:
|
|
364
|
+
raise AuthError("Loom returned an unsupported upload transport.")
|
|
365
|
+
completion = {"workspaceId": f"ari:cloud:loom:{record['cloud_id']}:workspace/{workspace}", "keyPath": key_path, "fileName": filename}
|
|
366
|
+
completed = gql(record, COMPLETE_UPLOAD, {"input": completion})["loom_completeVideoFileUpload"]
|
|
367
|
+
if not completed or not completed.get("success") or not completed.get("video"):
|
|
368
|
+
raise AuthError("Loom did not confirm publishing. Check the library before retrying.")
|
|
369
|
+
video = completed["video"]
|
|
370
|
+
if not isinstance(video.get("url"), str) or not video["url"].startswith("https://") or not isinstance(video.get("id"), str):
|
|
371
|
+
raise AuthError("Loom returned an invalid video result.")
|
|
372
|
+
print(json.dumps({"url": video["url"], "id": video["id"]}))
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
def main(argv=None):
|
|
376
|
+
parser = argparse.ArgumentParser(prog="toucan-upload-loom")
|
|
377
|
+
commands = parser.add_subparsers(dest="command", required=True)
|
|
378
|
+
auth = commands.add_parser("login")
|
|
379
|
+
auth.add_argument("--force", action="store_true")
|
|
380
|
+
commands.add_parser("status")
|
|
381
|
+
send = commands.add_parser("upload")
|
|
382
|
+
send.add_argument("file")
|
|
383
|
+
send.add_argument("--title")
|
|
384
|
+
options = parser.parse_args(argv)
|
|
385
|
+
try:
|
|
386
|
+
if options.command == "login":
|
|
387
|
+
login(force=options.force)
|
|
388
|
+
elif options.command == "status":
|
|
389
|
+
if not CREDENTIALS.is_file():
|
|
390
|
+
print(json.dumps({"logged_in": False}))
|
|
391
|
+
return 1
|
|
392
|
+
record = credentials()
|
|
393
|
+
print(json.dumps({"logged_in": True, "workspace": activation(record)}))
|
|
394
|
+
else:
|
|
395
|
+
args = [options.file]
|
|
396
|
+
if options.title is not None:
|
|
397
|
+
args += ["--title", options.title]
|
|
398
|
+
upload(credentials(), args)
|
|
399
|
+
return 0
|
|
400
|
+
except (AuthError, KeyError, ValueError, TypeError, OSError, IndexError, AttributeError) as error:
|
|
401
|
+
if options.command == "status":
|
|
402
|
+
print(json.dumps({"logged_in": False}))
|
|
403
|
+
else:
|
|
404
|
+
message = str(error) if isinstance(error, AuthError) else "Invalid local authentication state or upload response."
|
|
405
|
+
print("Loom error: " + message, file=sys.stderr)
|
|
406
|
+
return 1
|
|
407
|
+
except KeyboardInterrupt:
|
|
408
|
+
return 130
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
if __name__ == "__main__":
|
|
412
|
+
sys.exit(main())
|
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
import base64
|
|
2
|
+
import fcntl
|
|
3
|
+
import hashlib
|
|
4
|
+
import io
|
|
5
|
+
import json
|
|
6
|
+
import os
|
|
7
|
+
import stat
|
|
8
|
+
import time
|
|
9
|
+
import urllib.error
|
|
10
|
+
from urllib.parse import parse_qs, urlsplit
|
|
11
|
+
|
|
12
|
+
import pytest
|
|
13
|
+
|
|
14
|
+
from toucan_router_tools import sheets
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@pytest.fixture
|
|
18
|
+
def sheets_home(tmp_path, monkeypatch):
|
|
19
|
+
path = tmp_path / 'sheets'
|
|
20
|
+
monkeypatch.setenv('TOUCAN_SHEETS_DIR', str(path))
|
|
21
|
+
|
|
22
|
+
def offline(*args, **kwargs):
|
|
23
|
+
pytest.fail('Unexpected network request')
|
|
24
|
+
|
|
25
|
+
monkeypatch.setattr(sheets.HTTP, 'open', offline)
|
|
26
|
+
return path
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def install_client(path):
|
|
30
|
+
path.mkdir(parents=True, exist_ok=True, mode=0o700)
|
|
31
|
+
(path / 'client.json').write_text(json.dumps({'installed': {
|
|
32
|
+
'client_id': 'desktop.example.com', 'client_secret': 'fictional-client-secret',
|
|
33
|
+
}}))
|
|
34
|
+
(path / 'client.json').chmod(0o600)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def install_token(path, expires_at=None):
|
|
38
|
+
record = dict(access_token='fictional-access-token', refresh_token='fictional-refresh-token',
|
|
39
|
+
expires_at=expires_at if expires_at is not None else time.time() + 3600,
|
|
40
|
+
scopes=['https://www.googleapis.com/auth/drive.file'])
|
|
41
|
+
(path / 'token.json').write_text(json.dumps(record))
|
|
42
|
+
(path / 'token.json').chmod(0o600)
|
|
43
|
+
return record
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def response(data):
|
|
47
|
+
return io.BytesIO(json.dumps(data).encode())
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class CallbackSocket:
|
|
51
|
+
def __init__(self, target):
|
|
52
|
+
self.input = io.BytesIO(f'GET {target} HTTP/1.0\r\nHost: 127.0.0.1\r\n\r\n'.encode())
|
|
53
|
+
self.output = bytearray()
|
|
54
|
+
|
|
55
|
+
def makefile(self, mode, *args):
|
|
56
|
+
return self.input
|
|
57
|
+
|
|
58
|
+
def sendall(self, data):
|
|
59
|
+
self.output.extend(data)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class AuthorizationServer:
|
|
63
|
+
def __init__(self, address, handler):
|
|
64
|
+
assert address == ('127.0.0.1', 8765)
|
|
65
|
+
self.handler = handler
|
|
66
|
+
|
|
67
|
+
def __enter__(self):
|
|
68
|
+
return self
|
|
69
|
+
|
|
70
|
+
def __exit__(self, *args):
|
|
71
|
+
pass
|
|
72
|
+
|
|
73
|
+
def handle_request(self):
|
|
74
|
+
connection = CallbackSocket('/?code=fictional-code&state=' + self.oauth_state)
|
|
75
|
+
self.handler(connection, ('127.0.0.1', 1555), self)
|
|
76
|
+
assert b'200 OK' in connection.output
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
@pytest.mark.parametrize('has_client', [False, True])
|
|
80
|
+
def test_status_without_setup_is_json(sheets_home, has_client, capsys):
|
|
81
|
+
if has_client:
|
|
82
|
+
install_client(sheets_home)
|
|
83
|
+
assert sheets.main(['status']) == 1
|
|
84
|
+
output = capsys.readouterr()
|
|
85
|
+
result = json.loads(output.out)
|
|
86
|
+
assert result['authenticated'] is False
|
|
87
|
+
assert ('Not authenticated' if has_client else 'No desktop OAuth client') in result['reason']
|
|
88
|
+
assert not output.err
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def test_auth_pkce_and_private_atomic_token(sheets_home, monkeypatch, capsys):
|
|
92
|
+
install_client(sheets_home)
|
|
93
|
+
sheets_home.chmod(0o755)
|
|
94
|
+
(sheets_home / 'client.json').chmod(0o644)
|
|
95
|
+
monkeypatch.setattr(sheets, 'CallbackServer', AuthorizationServer)
|
|
96
|
+
replace = sheets.os.replace
|
|
97
|
+
replacements = []
|
|
98
|
+
|
|
99
|
+
def atomic(source, target):
|
|
100
|
+
assert source != target
|
|
101
|
+
assert stat.S_IMODE(os.stat(source).st_mode) == 0o600
|
|
102
|
+
assert not target.exists()
|
|
103
|
+
replacements.append((source, target))
|
|
104
|
+
replace(source, target)
|
|
105
|
+
|
|
106
|
+
def network(req, timeout):
|
|
107
|
+
assert timeout == 60
|
|
108
|
+
if req.full_url == sheets.TOKEN:
|
|
109
|
+
fields = parse_qs(req.data.decode())
|
|
110
|
+
authorization = json.loads(capsys.readouterr().err)['authorization_url']
|
|
111
|
+
query = parse_qs(urlsplit(authorization).query)
|
|
112
|
+
assert urlsplit(authorization).netloc == 'accounts.google.com'
|
|
113
|
+
assert query['scope'] == ['https://www.googleapis.com/auth/drive.file']
|
|
114
|
+
assert query['code_challenge_method'] == ['S256']
|
|
115
|
+
assert query['code_challenge'] == [base64.urlsafe_b64encode(hashlib.sha256(
|
|
116
|
+
fields['code_verifier'][0].encode()).digest()).decode().rstrip('=')]
|
|
117
|
+
assert 43 <= len(fields['code_verifier'][0]) <= 128
|
|
118
|
+
assert query['access_type'] == ['offline'] and query['prompt'] == ['consent']
|
|
119
|
+
assert query['login_hint'] == ['alice@example.com']
|
|
120
|
+
assert fields['redirect_uri'] == query['redirect_uri'] == ['http://127.0.0.1:8765/']
|
|
121
|
+
assert fields['code'] == ['fictional-code']
|
|
122
|
+
assert fields['grant_type'] == ['authorization_code']
|
|
123
|
+
return response(dict(access_token='new-access-token', refresh_token='new-refresh-token',
|
|
124
|
+
token_type='Bearer', expires_in=3600))
|
|
125
|
+
assert req.full_url == sheets.IDENTITY
|
|
126
|
+
assert req.get_header('Authorization') == 'Bearer new-access-token'
|
|
127
|
+
return response({'user': {'emailAddress': 'ALICE@example.com'}})
|
|
128
|
+
|
|
129
|
+
monkeypatch.setattr(sheets.HTTP, 'open', network)
|
|
130
|
+
monkeypatch.setattr(sheets.os, 'replace', atomic)
|
|
131
|
+
previous = os.umask(0)
|
|
132
|
+
try:
|
|
133
|
+
assert sheets.main(['auth', '--account', 'alice@example.com']) == 0
|
|
134
|
+
finally:
|
|
135
|
+
os.umask(previous)
|
|
136
|
+
assert json.loads(capsys.readouterr().out) == dict(authenticated=True, email='ALICE@example.com', scopes=sheets.SCOPES)
|
|
137
|
+
saved = json.loads((sheets_home / 'token.json').read_text())
|
|
138
|
+
assert saved['access_token'] == 'new-access-token'
|
|
139
|
+
assert saved['refresh_token'] == 'new-refresh-token'
|
|
140
|
+
assert len(replacements) == 1
|
|
141
|
+
assert stat.S_IMODE(sheets_home.stat().st_mode) == 0o700
|
|
142
|
+
assert all(stat.S_IMODE(path.stat().st_mode) == 0o600 for path in sheets_home.iterdir())
|
|
143
|
+
assert not list(sheets_home.glob('.token-*'))
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
@pytest.mark.parametrize('existing,force,account,email,exit_code', [
|
|
147
|
+
(True, False, None, 'alice@example.com', 3),
|
|
148
|
+
(True, True, 'alice@example.com', 'bob@example.com', 3),
|
|
149
|
+
(False, False, 'alice@example.com', 'bob@example.com', 3),
|
|
150
|
+
(True, True, None, 'bob@example.com', 0),
|
|
151
|
+
])
|
|
152
|
+
def test_existing_authorization_and_account_guard(sheets_home, monkeypatch, capsys, existing, force, account, email, exit_code):
|
|
153
|
+
install_client(sheets_home)
|
|
154
|
+
original = install_token(sheets_home) if existing else None
|
|
155
|
+
monkeypatch.setattr(sheets, 'CallbackServer', AuthorizationServer)
|
|
156
|
+
calls = []
|
|
157
|
+
|
|
158
|
+
def network(req, timeout):
|
|
159
|
+
calls.append(req.full_url)
|
|
160
|
+
if req.full_url == sheets.TOKEN:
|
|
161
|
+
return response(dict(access_token='replacement-access', refresh_token='replacement-refresh',
|
|
162
|
+
token_type='Bearer', expires_in=3600))
|
|
163
|
+
return response({'user': {'emailAddress': email}})
|
|
164
|
+
|
|
165
|
+
monkeypatch.setattr(sheets.HTTP, 'open', network)
|
|
166
|
+
args = ['auth'] + (['--force'] if force else []) + (['--account', account] if account else [])
|
|
167
|
+
assert sheets.main(args) == exit_code
|
|
168
|
+
saved = json.loads((sheets_home / 'token.json').read_text()) if (sheets_home / 'token.json').exists() else None
|
|
169
|
+
output = capsys.readouterr()
|
|
170
|
+
if exit_code:
|
|
171
|
+
assert saved == original
|
|
172
|
+
assert ('already exists' if existing and not force else 'does not match') in output.err
|
|
173
|
+
else:
|
|
174
|
+
assert saved['access_token'] == 'replacement-access'
|
|
175
|
+
assert json.loads(output.out)['email'] == 'bob@example.com'
|
|
176
|
+
if existing and not force:
|
|
177
|
+
assert not calls
|
|
178
|
+
assert all(secret not in output.out + output.err for secret in (
|
|
179
|
+
'fictional-access-token', 'fictional-refresh-token', 'replacement-access', 'replacement-refresh', 'fictional-client-secret'))
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
@pytest.mark.parametrize('target,accepted,denied', [
|
|
183
|
+
('/?state=expected&code=code', True, False),
|
|
184
|
+
('/?state=wrong&code=code', False, False),
|
|
185
|
+
('/?code=code', False, False),
|
|
186
|
+
('/?state=expected&state=wrong&code=code', False, False),
|
|
187
|
+
('/?state=expected&code=one&code=two', False, False),
|
|
188
|
+
('/other?state=expected&code=code', False, False),
|
|
189
|
+
('/?state=expected&error=access_denied', True, True),
|
|
190
|
+
('/?state=expected&error=access_denied&code=code', False, False),
|
|
191
|
+
])
|
|
192
|
+
def test_callback_state_validation(target, accepted, denied, capsys):
|
|
193
|
+
server = AuthorizationServer(('127.0.0.1', 8765), sheets.CallbackHandler)
|
|
194
|
+
server.oauth_state, server.code, server.denied = 'expected', None, False
|
|
195
|
+
connection = CallbackSocket(target)
|
|
196
|
+
sheets.CallbackHandler(connection, ('127.0.0.1', 1555), server)
|
|
197
|
+
assert (b'200 OK' if accepted else b'400 Bad Request') in connection.output
|
|
198
|
+
assert server.code == ('code' if accepted and not denied else None)
|
|
199
|
+
assert server.denied is denied
|
|
200
|
+
assert capsys.readouterr() == ('', '')
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def test_auth_timeout_does_not_save(sheets_home, monkeypatch, capsys):
|
|
204
|
+
install_client(sheets_home)
|
|
205
|
+
monkeypatch.setattr(sheets, 'CallbackServer', AuthorizationServer)
|
|
206
|
+
times = iter([0, 901])
|
|
207
|
+
monkeypatch.setattr(sheets.time, 'monotonic', lambda: next(times))
|
|
208
|
+
assert sheets.main(['auth']) == 1
|
|
209
|
+
assert '15 minutes' in json.loads(capsys.readouterr().err.splitlines()[-1])['error']
|
|
210
|
+
assert not (sheets_home / 'token.json').exists()
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
@pytest.mark.parametrize('token_response', [
|
|
214
|
+
dict(access_token='new-access-token', token_type='Bearer', expires_in=3600),
|
|
215
|
+
dict(access_token='new-access-token', refresh_token='new-refresh-token', token_type='Bearer',
|
|
216
|
+
expires_in=3600, scope='https://www.googleapis.com/auth/drive'),
|
|
217
|
+
])
|
|
218
|
+
def test_auth_rejects_missing_offline_access_or_extra_scopes(sheets_home, monkeypatch, capsys, token_response):
|
|
219
|
+
install_client(sheets_home)
|
|
220
|
+
monkeypatch.setattr(sheets, 'CallbackServer', AuthorizationServer)
|
|
221
|
+
monkeypatch.setattr(sheets.HTTP, 'open', lambda req, timeout: response(token_response))
|
|
222
|
+
assert sheets.main(['auth']) == 1
|
|
223
|
+
output = capsys.readouterr()
|
|
224
|
+
assert json.loads(output.err.splitlines()[-1])['error']
|
|
225
|
+
assert 'new-access-token' not in output.out + output.err
|
|
226
|
+
assert not (sheets_home / 'token.json').exists()
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def test_invalid_client_does_not_start_authorization(sheets_home, capsys):
|
|
230
|
+
install_client(sheets_home)
|
|
231
|
+
(sheets_home / 'client.json').write_text('{"web":{"client_secret":"fictional-client-secret"}}')
|
|
232
|
+
assert sheets.main(['auth']) == 2
|
|
233
|
+
output = capsys.readouterr()
|
|
234
|
+
assert 'installed desktop OAuth client' in json.loads(output.err)['error']
|
|
235
|
+
assert 'fictional-client-secret' not in output.err and not output.out
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def test_refresh_holds_lock_and_preserves_refresh_token(sheets_home, monkeypatch, capsys):
|
|
239
|
+
install_client(sheets_home)
|
|
240
|
+
install_token(sheets_home, expires_at=0)
|
|
241
|
+
calls = []
|
|
242
|
+
|
|
243
|
+
def network(req, timeout):
|
|
244
|
+
calls.append(req.full_url)
|
|
245
|
+
if req.full_url == sheets.TOKEN:
|
|
246
|
+
with (sheets_home / 'token.lock').open() as other:
|
|
247
|
+
with pytest.raises(BlockingIOError):
|
|
248
|
+
fcntl.flock(other.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
249
|
+
fields = parse_qs(req.data.decode())
|
|
250
|
+
assert fields['grant_type'] == ['refresh_token']
|
|
251
|
+
assert fields['refresh_token'] == ['fictional-refresh-token']
|
|
252
|
+
return response(dict(access_token='refreshed-access', token_type='Bearer', expires_in=3600))
|
|
253
|
+
assert req.full_url == sheets.IDENTITY
|
|
254
|
+
assert req.get_header('Authorization') == 'Bearer refreshed-access'
|
|
255
|
+
return response({'user': {'emailAddress': 'alice@example.com'}})
|
|
256
|
+
|
|
257
|
+
monkeypatch.setattr(sheets.HTTP, 'open', network)
|
|
258
|
+
for attempt in range(2):
|
|
259
|
+
assert sheets.main(['status', '--json']) == 0
|
|
260
|
+
result = json.loads(capsys.readouterr().out)
|
|
261
|
+
assert result['authenticated'] is True
|
|
262
|
+
assert result['email'] == 'alice@example.com'
|
|
263
|
+
assert result['scopes'] == sheets.SCOPES and result['has_refresh_token'] is True
|
|
264
|
+
assert result['expiry'].endswith('+00:00')
|
|
265
|
+
assert calls.count(sheets.TOKEN) == 1
|
|
266
|
+
saved = json.loads((sheets_home / 'token.json').read_text())
|
|
267
|
+
assert saved['access_token'] == 'refreshed-access' and saved['refresh_token'] == 'fictional-refresh-token'
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
def test_atomic_replace_failure_keeps_old_token(sheets_home, monkeypatch, capsys):
|
|
271
|
+
install_client(sheets_home)
|
|
272
|
+
original = install_token(sheets_home, expires_at=0)
|
|
273
|
+
monkeypatch.setattr(sheets.HTTP, 'open', lambda req, timeout: response(dict(
|
|
274
|
+
access_token='new-access-token', token_type='Bearer', expires_in=3600)))
|
|
275
|
+
|
|
276
|
+
def fail(source, target):
|
|
277
|
+
raise OSError('fictional-access-token must not leak from exceptions')
|
|
278
|
+
|
|
279
|
+
monkeypatch.setattr(sheets.os, 'replace', fail)
|
|
280
|
+
assert sheets.main(['status']) == 1
|
|
281
|
+
assert json.loads((sheets_home / 'token.json').read_text()) == original
|
|
282
|
+
assert not list(sheets_home.glob('.token-*'))
|
|
283
|
+
assert 'fictional-access-token' not in capsys.readouterr().out
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
@pytest.mark.parametrize('operation', ['read', 'refresh'])
|
|
287
|
+
def test_google_error_messages_redact_credentials(sheets_home, monkeypatch, capsys, operation):
|
|
288
|
+
install_client(sheets_home)
|
|
289
|
+
install_token(sheets_home, expires_at=0 if operation == 'refresh' else None)
|
|
290
|
+
|
|
291
|
+
def network(req, timeout):
|
|
292
|
+
message = 'Denied fictional-access-token fictional-refresh-token'
|
|
293
|
+
if operation == 'refresh':
|
|
294
|
+
message += ' fictional-client-secret'
|
|
295
|
+
body = {'error': {'message': message}}
|
|
296
|
+
raise urllib.error.HTTPError(req.full_url, 403, 'Forbidden', {}, response(body))
|
|
297
|
+
|
|
298
|
+
monkeypatch.setattr(sheets.HTTP, 'open', network)
|
|
299
|
+
assert sheets.main(['read', 'sheet', 'A1']) == 1
|
|
300
|
+
output = capsys.readouterr()
|
|
301
|
+
result = json.loads(output.err)
|
|
302
|
+
assert 'Google API HTTP 403: Denied' in result['error']
|
|
303
|
+
assert all(value not in output.err + output.out for value in (
|
|
304
|
+
'fictional-access-token', 'fictional-refresh-token', 'fictional-client-secret', 'Authorization', 'Bearer'))
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
@pytest.mark.parametrize('command,method,suffix,payload', [
|
|
308
|
+
(['read', 'sheet/id', "'Sales / 2026'!A1:B2"], 'GET',
|
|
309
|
+
'/sheet%2Fid/values/%27Sales%20%2F%202026%27%21A1%3AB2', None),
|
|
310
|
+
(['write', 'sheet/id', 'A1:B2', '-', '--input-option', 'USER_ENTERED'], 'PUT',
|
|
311
|
+
'/sheet%2Fid/values/A1%3AB2?valueInputOption=USER_ENTERED', {'values': [[1, 2]]}),
|
|
312
|
+
(['write', 'sheet', 'A1', '-'], 'PUT', '/sheet/values/A1?valueInputOption=RAW', {'values': [[1, 2]]}),
|
|
313
|
+
(['batch-update', 'sheet/id', '-'], 'POST', '/sheet%2Fid:batchUpdate',
|
|
314
|
+
{'requests': [{'addSheet': {'properties': {'title': 'Example'}}}]}),
|
|
315
|
+
])
|
|
316
|
+
def test_sheets_api_commands(sheets_home, monkeypatch, capsys, command, method, suffix, payload):
|
|
317
|
+
install_client(sheets_home)
|
|
318
|
+
install_token(sheets_home)
|
|
319
|
+
input_body = payload if command[0] == 'batch-update' else [[1, 2]]
|
|
320
|
+
monkeypatch.setattr(sheets.sys, 'stdin', io.StringIO(json.dumps(input_body)))
|
|
321
|
+
|
|
322
|
+
def network(req, timeout):
|
|
323
|
+
assert req.full_url == sheets.SHEETS + suffix
|
|
324
|
+
assert req.method == method
|
|
325
|
+
assert (json.loads(req.data) if req.data else None) == payload
|
|
326
|
+
assert req.get_header('Authorization') == 'Bearer fictional-access-token'
|
|
327
|
+
return response({'result': 'done'})
|
|
328
|
+
|
|
329
|
+
monkeypatch.setattr(sheets.HTTP, 'open', network)
|
|
330
|
+
assert sheets.main(command) == 0
|
|
331
|
+
assert json.loads(capsys.readouterr().out) == {'result': 'done'}
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
@pytest.mark.parametrize('failure', [None, 'api', 'input'])
|
|
335
|
+
def test_create_initial_values_and_recovery_receipt(sheets_home, tmp_path, monkeypatch, capsys, failure):
|
|
336
|
+
install_client(sheets_home)
|
|
337
|
+
install_token(sheets_home)
|
|
338
|
+
values = tmp_path / 'values.json'
|
|
339
|
+
values.write_text('invalid JSON' if failure == 'input' else '[["alice", 1]]')
|
|
340
|
+
calls = []
|
|
341
|
+
|
|
342
|
+
def network(req, timeout):
|
|
343
|
+
calls.append(req.full_url)
|
|
344
|
+
if req.full_url == sheets.SHEETS:
|
|
345
|
+
assert json.loads(req.data) == {'properties': {'title': 'Example'}}
|
|
346
|
+
return response({'spreadsheetId': 'created/id', 'spreadsheetUrl': 'https://example.com/sheet'})
|
|
347
|
+
assert req.full_url == sheets.SHEETS + '/created%2Fid/values/Sheet1%21A1?valueInputOption=RAW'
|
|
348
|
+
assert json.loads(req.data) == {'values': [['alice', 1]]}
|
|
349
|
+
if failure == 'api':
|
|
350
|
+
raise urllib.error.HTTPError(req.full_url, 400, 'Bad Request', {}, response({'error': {'message': 'Bad range'}}))
|
|
351
|
+
return response({'updatedCells': 2})
|
|
352
|
+
|
|
353
|
+
monkeypatch.setattr(sheets.HTTP, 'open', network)
|
|
354
|
+
assert sheets.main(['create', 'Example', '--values', str(values)]) == (2 if failure == 'input' else 1 if failure else 0)
|
|
355
|
+
output = capsys.readouterr()
|
|
356
|
+
assert len(calls) == (1 if failure == 'input' else 2)
|
|
357
|
+
if failure:
|
|
358
|
+
receipt, error = map(json.loads, output.err.splitlines())
|
|
359
|
+
assert receipt == dict(createdSpreadsheetId='created/id', spreadsheetUrl='https://example.com/sheet', initialValuesWritten=False)
|
|
360
|
+
assert error['error'] and not output.out
|
|
361
|
+
else:
|
|
362
|
+
assert json.loads(output.out)['initialValuesUpdate'] == {'updatedCells': 2}
|
|
363
|
+
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import base64
|
|
2
|
+
import json
|
|
3
|
+
import os
|
|
4
|
+
import stat
|
|
5
|
+
import time
|
|
6
|
+
|
|
7
|
+
import pytest
|
|
8
|
+
|
|
9
|
+
from toucan_router_tools import upload_loom
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@pytest.fixture
|
|
13
|
+
def loom_home(tmp_path, monkeypatch):
|
|
14
|
+
config = tmp_path / 'toucan' / 'loom'
|
|
15
|
+
monkeypatch.setattr(upload_loom, 'CONFIG', config)
|
|
16
|
+
monkeypatch.setattr(upload_loom, 'CREDENTIALS', config / 'oauth.json')
|
|
17
|
+
return config
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def test_logged_out_status_is_json(loom_home, capsys):
|
|
21
|
+
assert upload_loom.main(['status']) == 1
|
|
22
|
+
assert json.loads(capsys.readouterr().out) == {'logged_in': False}
|
|
23
|
+
assert not loom_home.exists()
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def test_login_saves_private_credentials(loom_home, monkeypatch, capsys):
|
|
27
|
+
site = '00000000-0000-0000-0000-000000000001'
|
|
28
|
+
payload = base64.urlsafe_b64encode(json.dumps({'aud': [f'ari:cloud:loom::site/{site}']}).encode()).decode().rstrip('=')
|
|
29
|
+
token = 'header.' + payload + '.signature'
|
|
30
|
+
responses = iter([
|
|
31
|
+
{'device_code': 'device', 'verification_uri': 'https://auth.atlassian.com/authorize', 'expires_in': 60},
|
|
32
|
+
{'access_token': token, 'refresh_token': 'saved-refresh-token', 'expires_in': 3600, 'token_type': 'Bearer'},
|
|
33
|
+
])
|
|
34
|
+
monkeypatch.setattr(upload_loom, 'request', lambda path, fields: next(responses))
|
|
35
|
+
monkeypatch.setattr(upload_loom.time, 'sleep', lambda seconds: None)
|
|
36
|
+
previous = os.umask(0)
|
|
37
|
+
try:
|
|
38
|
+
assert upload_loom.main(['login']) == 0
|
|
39
|
+
finally:
|
|
40
|
+
os.umask(previous)
|
|
41
|
+
saved = json.loads((loom_home / 'oauth.json').read_text())
|
|
42
|
+
assert saved['cloud_id'] == site
|
|
43
|
+
assert saved['access_token'] == token
|
|
44
|
+
assert stat.S_IMODE(loom_home.stat().st_mode) == 0o700
|
|
45
|
+
assert all(stat.S_IMODE(path.stat().st_mode) == 0o600 for path in loom_home.iterdir())
|
|
46
|
+
output = capsys.readouterr().out
|
|
47
|
+
assert token not in output and 'saved-refresh-token' not in output
|
|
48
|
+
assert upload_loom.main(['login']) == 1
|
|
49
|
+
assert json.loads((loom_home / 'oauth.json').read_text()) == saved
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def test_upload_and_status_json_with_mocked_network(loom_home, tmp_path, monkeypatch, capsys):
|
|
53
|
+
record = {'access_token': 'access', 'refresh_token': 'refresh', 'expires_at': time.time() + 3600,
|
|
54
|
+
'cloud_id': '00000000-0000-0000-0000-000000000001'}
|
|
55
|
+
upload_loom.save(record)
|
|
56
|
+
movie = tmp_path / 'clip.mp4'
|
|
57
|
+
movie.write_bytes(b'video-content')
|
|
58
|
+
sent, completion = [], []
|
|
59
|
+
|
|
60
|
+
def network(url, method='GET', headers=None, data=None, timeout=60):
|
|
61
|
+
if url == upload_loom.GRAPHQL:
|
|
62
|
+
message = json.loads(data)
|
|
63
|
+
query = message['query']
|
|
64
|
+
if 'loom_spacesSearch' in query:
|
|
65
|
+
result = {'loom_spacesSearch': [{'id': 'ari:cloud:loom:site:space/activation/workspace/space'}]}
|
|
66
|
+
elif 'loom_createFileUploadCredentials' in query:
|
|
67
|
+
result = {'loom_createFileUploadCredentials': {'success': True, 'credentials': {
|
|
68
|
+
'__typename': 'LoomS3UploadInfo', 'endpoint': 'https://storage.example.com',
|
|
69
|
+
'path': 'uploads/workspace', 'region': 'us-east-1', 'accessKeyId': 'key',
|
|
70
|
+
'secretAccessKey': 'secret', 'sessionToken': 'session'}}}
|
|
71
|
+
else:
|
|
72
|
+
completion.append(message['variables']['input'])
|
|
73
|
+
result = {'loom_completeVideoFileUpload': {'success': True,
|
|
74
|
+
'video': {'id': 'video-id', 'url': 'https://www.loom.com/share/video-id'}}}
|
|
75
|
+
return json.dumps({'data': result}).encode()
|
|
76
|
+
assert method == 'PUT'
|
|
77
|
+
sent.append(data.read())
|
|
78
|
+
return b''
|
|
79
|
+
|
|
80
|
+
monkeypatch.setattr(upload_loom, 'fetch', network)
|
|
81
|
+
assert upload_loom.main(['status']) == 0
|
|
82
|
+
assert json.loads(capsys.readouterr().out) == {'logged_in': True, 'workspace': 'workspace'}
|
|
83
|
+
assert upload_loom.main(['upload', str(movie), '--title', 'Review']) == 0
|
|
84
|
+
assert json.loads(capsys.readouterr().out) == {'url': 'https://www.loom.com/share/video-id', 'id': 'video-id'}
|
|
85
|
+
assert sent == [b'video-content']
|
|
86
|
+
assert completion[0]['fileName'] == 'Review.mp4'
|