From fd8d0f20d1cf1798d35ea8aa4929d55cc749f7b9 Mon Sep 17 00:00:00 2001 From: Anya Tokugawa Date: Sun, 1 Nov 2026 21:31:04 +0900 Subject: [PATCH] Enable CLI language-aware editing and reuse OIDC authentication --- README.md | 33 ++++++++++++++----- pages | 61 +++++++++++++++++++++++++--------- test_pages.py | 90 +++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 154 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index fa8b3e3..95ca0c3 100644 --- a/README.md +++ b/README.md @@ -48,8 +48,10 @@ pages login --oidc --no-browser CLIに表示されたURLを手元のPC/スマホで開き、OIDCでログインします。 続いてアカウント・接続先を確認し、ターミナルの確認コードを入力して承認します。 -ローカルTOTPを設定済みならTOTPも必要です。承認後はCLIが専用セッションを受け取ります。 +OIDCで認証した場合は、追加のローカルTOTPを要求しません。承認後はCLIが専用セッションを受け取ります。 OIDCプロバイダーのパスワードやトークンをターミナルに入力する必要はありません。 +プロバイダー側のMFAポリシーはそのまま適用されます。パスワードログイン時のローカルTOTPも変更しません。 +この追加TOTP省略はv0.3.0より後の変更です。v0.3.0のサーバーでは従来どおりTOTPを要求します。 ユーザー名の指定は省略できます。指定済みならブラウザで認証したユーザーとの一致を確認し、 不一致時は発行セッションの失効を試みて保存しません。`pages logout` 後に設定を変更できます。 @@ -66,7 +68,7 @@ configureの接続先URLを確認し、サーバーも対応版へ更新して ```sh pages configure # 設定を表示(トークンは表示しない) -pages configure --lang ja # 現在の既定接続の編集言語 +pages configure --lang auto # 表示対象の言語版を編集 pages configure list # 接続先一覧、★が既定 pages configure set --profile work --origin https://work.example.test --username anya --lang en pages configure use work # 以後はworkを指定不要 @@ -74,11 +76,13 @@ pages status pages edit /guide ``` -既定プロファイル名は `default`、既定言語は `und`(言語未指定)です。OSや表示言語から -書き込み先の言語を推測しません。最初に名前付きプロファイルを作った場合はそれを既定とします。 -言語は接続先ごとに保持し、編集時はその言語の既存Revisionを使用します。 +既定プロファイル名は `default`、新しいプロファイルの既定言語は `auto` です。 +`auto` はサーバーが通常表示するRevisionの言語を取得し、その言語の最新版を編集します。 +OSの言語からは推測しません。最初に名前付きプロファイルを作った場合はそれを既定とします。 +言語は接続先ごとに保持します。既存設定の `und`(言語未指定)や `ja` は自動変更しません。 +以前のCLIで設定済みの場合は `pages configure --lang auto` で切り替えられます。 -一度だけ変更したいときは `pages --profile personal --lang ja edit /guide` も使えます。 +一度だけ変更したいときは `pages edit /guide --lang auto`、`pages --profile personal --lang ja edit /guide` も使えます。 プロファイル自体は先にconfigureで登録します。引数の上書きは設定ファイルを書き換えません。 設定ファイルは `~/.config/pages-cli/config.json`(`XDG_CONFIG_HOME`対応)。 @@ -91,7 +95,7 @@ pages edit /guide "default": { "origin": "https://pages.example.test", "username": "anya", - "lang": "und" + "lang": "auto" } } } @@ -122,7 +126,20 @@ pages edit /guide `pages --help`、`pages configure --help`、`pages edit --help` で引数を確認できます。 Tab補完、上下キーのプロセス内履歴に対応。複雑な引用位置の補完、Ctrl+Pのfuzzy UIは後続です。 -初期版は既存Page・既存言語の更新まで。新規作成、バイナリ更新は未実装です。 +### 言語を指定して編集する + +```sh +pages edit /guide --lang auto # 表示される言語の最新版を編集 +pages edit /guide --lang ja # 日本語版。未作成なら既存内容から開始 +pages edit /guide --lang en # 英語版。未作成なら既存内容から開始 +``` + +編集開始時に対象言語を表示します。指定言語のRevisionがない場合は、表示中の版の内容を +下書きとして `$EDITOR` を開き、変更を保存するとその言語の最初のRevisionを作ります。 +自動翻訳ではありません。元の言語版は変更せず、編集せずに閉じた場合は何も作成しません。 +同じ言語版が先に作成された場合も、競合として保存を止めます。 + +対応範囲は既存Pageの更新と新規言語版の作成です。新規Path/Page作成、バイナリ更新は未実装です。 競合や通信断で自動上書きせず、ローカル原稿を保持します。HTTPS必須(開発用loopbackのみHTTP可)。 詳しいURI・保存・安全性の契約は[設計書](../docs/design/ja/remote-cli.md)を参照してください。 diff --git a/pages b/pages index 9c95375..3007e77 100755 --- a/pages +++ b/pages @@ -71,7 +71,7 @@ class NoRedirect(http.HTTPRedirectHandler): class Client: - def __init__(self, base, token='', lang='und'): + def __init__(self, base, token='', lang='auto'): self.base, self.token, self.lang = origin(base), token, lang self.opener = http.build_opener(NoRedirect()) @@ -200,24 +200,45 @@ class Client: return self.json('/_/api/v1/path-picker?' + url.urlencode({'path': path, 'mode': 'children', 'prefix': prefix})) def raw(self, addr, method='GET'): - raw, headers = self.request('/v/raw/' + url.quote(addr, safe='/:') + '?' + url.urlencode({'lang': self.lang}), method=method, limit=64 << 20) + query = '' if self.lang == 'auto' else '?' + url.urlencode({'lang': self.lang}) + raw, headers = self.request('/v/raw/' + url.quote(addr, safe='/:') + query, method=method, limit=64 << 20) digest = headers.get('X-PagesWiki-Document-SHA256') if method == 'GET' and (not digest or hashlib.sha256(raw).hexdigest() != digest): raise Failure('Document SHA-256 mismatch.') return raw, headers - def snapshot(self, addr): + def snapshot(self, addr, lang=None): if addr.startswith('rev:'): raise Failure('Revision addresses are immutable; edit a path: or uuid: address.') _, headers = self.raw(addr, 'HEAD') ident = headers.get('X-PagesWiki-Object-UUID') if not ident: raise Failure('Missing Object UUID metadata.') - state = self.json('/_/api/v1/editor/state?' + url.urlencode({'uuid': ident, 'lang': self.lang})) + target = self.lang if lang is None else lang + visible = headers.get('X-PagesWiki-Revision-UUID', '') + def load(language, **extra): + return self.json('/_/api/v1/editor/state?' + url.urlencode({'uuid': ident, 'lang': language, **extra})) + if target == 'auto': + # Resolve the viewer's language, then obtain that exact language's + # current head. Never save against a head from a different language. + target = load('und', revision=visible, metadata=1)['lang'] if visible else 'und' + state = load(target) if not state.get('editable') or not state.get('principal_uuid'): raise Failure('Not an authenticated editable text document; use the browser.') - if not state.get('head_revision_uuid') or not state.get('content_type'): - raise Failure('No saved revision in this exact language; creation/translation is not supported yet.') + if not state.get('head_revision_uuid'): + # An empty CAS base asserts absence of this language. Seed from the + # visible source without changing it or pretending it is the base. + if visible: + source = load(state['lang'], revision=visible) + if not source.get('editable') or source.get('principal_uuid') != state['principal_uuid']: + raise Failure('Cannot fork this document or the editing account changed.') + for key in ('body', 'markdown', 'content_type', 'filename'): + state[key] = source[key] + state['source_revision_uuid'] = visible + state['source_language'] = source['lang'] + state['new_language'] = True + if not state.get('content_type'): + raise Failure('Missing document format; use the browser.') state['object_uuid'] = ident return state @@ -229,7 +250,7 @@ class Client: raise Failure('Markdown authoring is supported only for HTML Wiki documents.') text = self.json('/_/api/v1/render/markdown', method='POST', data={ 'markdown': text, 'authoring': True, 'projection': False})['html'] - if text == state['body']: + if state['head_revision_uuid'] and text == state['body']: return {'status': 'unchanged', 'revision_uuid': state['head_revision_uuid']} return self.json('/_/api/v1/revisions/save', method='POST', data={ 'object_uuid': state['object_uuid'], 'body': text, 'lang': state['lang'], @@ -294,7 +315,7 @@ def profile_name(name): def language(value): if not isinstance(value, str) or not re.fullmatch(r'[A-Za-z]{1,8}(?:-[A-Za-z0-9]{1,8})*', value): - raise Failure('Use a language tag such as und, ja or en-US; the server validates the exact tag.') + raise Failure('Use auto or a language tag such as und, ja or en-US; the server validates the exact tag.') return value @@ -325,7 +346,7 @@ class Configuration(PrivateJSON): raise Failure('Profiles accept only origin, username and lang; never store a password here.') if not isinstance(spec.get('username', ''), str) or any(unicodedata.category(c).startswith('C') for c in spec.get('username', '')): raise Failure('Invalid configured username.') - normalized[name] = dict(spec, origin=origin(spec.get('origin', '')), lang=language(spec.get('lang', 'und'))) + normalized[name] = dict(spec, origin=origin(spec.get('origin', '')), lang=language(spec.get('lang', 'auto'))) if profiles and default not in profiles: raise Failure('Default profile does not exist.') return {'version': 1, 'default_profile': default, 'profiles': normalized} @@ -347,7 +368,7 @@ def configure(words, root=None): print(json.dumps(data, ensure_ascii=False, indent=2)); return if args.action == 'list': for name, spec in data['profiles'].items(): - print(safe_text(('★ ' if name == data['default_profile'] else ' ') + name + ' ' + spec['origin'] + ' ' + spec.get('username', '') + ' [' + spec.get('lang', 'und') + ']')) + print(safe_text(('★ ' if name == data['default_profile'] else ' ') + name + ' ' + spec['origin'] + ' ' + spec.get('username', '') + ' [' + spec.get('lang', 'auto') + ']')) if not data['profiles']: print('No connections. Use: pages configure --origin https://wiki.example --username NAME') return if args.action == 'use': @@ -356,7 +377,7 @@ def configure(words, root=None): data['default_profile'] = name else: name = profile_name(args.profile or data['default_profile']) - spec = dict(data['profiles'].get(name, {'lang': 'und'})) + spec = dict(data['profiles'].get(name, {'lang': 'auto'})) if args.origin is not None: spec['origin'] = origin(args.origin) if args.username is not None: spec['username'] = args.username.strip() if args.lang is not None: spec['lang'] = language(args.lang) @@ -378,6 +399,9 @@ def configure(words, root=None): def editor(client, addr, mode): state = client.snapshot(addr) + print('Editing language:', safe_text(state['lang'])) + if state.get('new_language'): + print('Creating a new language revision; source remains unchanged:', safe_text(state.get('source_language', '(empty)'))) markdown = mode == 'markdown' or (mode == 'auto' and state['content_type'].startswith('text/html')) initial = state['markdown'] if markdown else state['body'] directory = Path(tempfile.mkdtemp(prefix='pageswiki-edit-')) @@ -397,7 +421,7 @@ def editor(client, addr, mode): result = client.save(state, text, markdown) except Failure as error: if error.status == 409: - latest = client.snapshot('uuid:' + state['object_uuid']) + latest = client.snapshot('uuid:' + state['object_uuid'], lang=state['lang']) remote = latest['markdown'] if markdown else latest['body'] print(safe_text(''.join(difflib.unified_diff(remote.splitlines(True), text.splitlines(True), fromfile='remote', tofile='local')))) raise @@ -411,7 +435,8 @@ def editor(client, addr, mode): metadata = directory / 'recovery.json' metadata.touch(mode=0o600) metadata.write_text(json.dumps({'origin': client.base, 'address': 'uuid:' + state['object_uuid'], - 'base_revision_uuid': state['head_revision_uuid'], 'lang': state['lang'], 'format': 'markdown' if markdown else 'raw'})) + 'base_revision_uuid': state['head_revision_uuid'], 'source_revision_uuid': state.get('source_revision_uuid', ''), + 'lang': state['lang'], 'format': 'markdown' if markdown else 'raw'})) print('Local edit retained:', safe_text(file), file=sys.stderr) @@ -423,6 +448,7 @@ def execute(client, profile, words): p.add_argument('path', nargs='?', default='.') if command == 'edit': p.add_argument('--format', choices=['auto', 'raw', 'markdown'], default='auto') + p.add_argument('--lang', help='auto edits the visible language; an explicit tag can create a language revision') p = sub.add_parser('get'); p.add_argument('path'); p.add_argument('local') p = sub.add_parser('put'); p.add_argument('local'); p.add_argument('path'); p.add_argument('--base', required=True) p.add_argument('--format', choices=['raw', 'markdown'], default='raw') @@ -462,9 +488,12 @@ def execute(client, profile, words): route = addr[5:] if addr.startswith('path:') else '/o/' + addr if addr.startswith('uuid:') else '/r/view/' + addr[4:] webbrowser.open(client.base + url.quote(route, safe='/:'), new=2) elif cmd == 'edit': - editor(client, addr, args.format) + editing = Client(client.base, client.token, language(args.lang)) if args.lang else client + editor(editing, addr, args.format) elif cmd == 'put': state = client.snapshot(addr) + if not state['head_revision_uuid']: + raise Failure('No revision in the target language; use edit --lang to create a language revision.') if state['head_revision_uuid'] != args.base: raise Failure('Conflict: supplied base is not the current language head.', 409) text = Path(args.local).read_text(encoding='utf-8') print('Revision:', safe_text(client.save(state, text, args.format == 'markdown')['revision_uuid'])) @@ -529,7 +558,7 @@ def shell(client, profile): def main(argv=None): parser = argparse.ArgumentParser(prog='pages', description=__doc__, epilog='Commands: configure, login, logout, status, shell, ls, cd, pwd, cat, view, get, put, edit, search, open. Use pages COMMAND --help.') parser.add_argument('--profile', help='Use another configured connection for this invocation') - parser.add_argument('--lang', help='Override the configured exact language for this invocation') + parser.add_argument('--lang', help='auto or an exact language tag for this invocation') parser.add_argument('command', nargs='?', default='shell') parser.add_argument('args', nargs=argparse.REMAINDER) args = parser.parse_args(argv) @@ -553,7 +582,7 @@ def main(argv=None): spec = settings['profiles'].get(name) if spec is None: raise Failure('No connection configured. Run: pages configure --origin https://HOST --username NAME') - lang = language(args.lang or spec.get('lang', 'und')) + lang = language(args.lang or spec.get('lang', 'auto')) # Hold the profile lock before releasing the configuration lock. profile = stack.enter_context(Profile(name).locked()) if args.command == 'login': diff --git a/test_pages.py b/test_pages.py index a40f9c2..db55865 100644 --- a/test_pages.py +++ b/test_pages.py @@ -139,11 +139,14 @@ class RemoteTests(unittest.TestCase): self.configure(root,'use','work') settings=json.loads(self.configure(root,'show')) self.assertEqual(settings['default_profile'],'work') - self.assertEqual(settings['profiles']['default']['lang'],'und') + self.assertEqual(settings['profiles']['default']['lang'],'auto') self.assertEqual(settings['profiles']['work']['lang'],'ja') self.configure(root,'--lang','en-US') settings=json.loads(self.configure(root,'show')) self.assertEqual(settings['profiles']['work']['lang'],'en-US') + self.configure(root,'--lang','und') + settings=json.loads(self.configure(root,'show')) + self.assertEqual(settings['profiles']['work']['lang'],'und') with m['Configuration'](root).locked() as cfg: self.assertEqual(stat.S_IMODE(cfg.path.stat().st_mode),0o600) with self.assertRaises(Failure): self.configure(root,'use','missing') @@ -274,6 +277,62 @@ class RemoteTests(unittest.TestCase): self.assertEqual(stat.S_IMODE(Path(file).stat().st_mode),0o600) with self.assertRaises(FileExistsError): m['execute'](client,profile,['get','/path',file]) + def test_auto_snapshot_pins_visible_language_and_head(self): + client=Client('https://example.test','token') + state={'lang':'ja','head_revision_uuid':'current-ja','content_type':'text/plain','filename':'x.txt','body':'new','markdown':'new','editable':True,'principal_uuid':'owner'} + headers={'X-PagesWiki-Object-UUID':'object','X-PagesWiki-Revision-UUID':'visible-ja'} + with patch.object(client,'raw',return_value=(b'',headers)),patch.object(client,'json',side_effect=[{'lang':'ja'},state]) as api: + result=client.snapshot('path:/x') + self.assertEqual(result['lang'],'ja') + self.assertEqual(result['head_revision_uuid'],'current-ja') + self.assertIn('revision=visible-ja',api.call_args_list[0].args[0]) + self.assertIn('lang=ja',api.call_args_list[1].args[0]) + self.assertNotIn('revision=',api.call_args_list[1].args[0]) + + def test_fork_uses_empty_base_and_preserves_source(self): + client=Client('https://example.test','token','en') + target={'lang':'en','head_revision_uuid':'','content_type':'text/html','filename':'','body':'','markdown':'','editable':True,'principal_uuid':'owner'} + source=dict(target,lang='ja',body='

Source

',markdown='# Source',revision_uuid='ja-base') + headers={'X-PagesWiki-Object-UUID':'object','X-PagesWiki-Revision-UUID':'ja-base'} + with patch.object(client,'raw',return_value=(b'',headers)),patch.object(client,'json',side_effect=[target,source]): + result=client.snapshot('path:/x') + self.assertEqual(result['lang'],'en'); self.assertEqual(result['head_revision_uuid'],'') + self.assertEqual(result['source_language'],'ja'); self.assertEqual(result['body'],source['body']) + with patch.object(client,'json',return_value={'revision_uuid':'en-new'}) as api: + client.save(result,result['body']) + self.assertEqual(api.call_args.kwargs['data']['base_revision_uuid'],'') + self.assertEqual(api.call_args.kwargs['data']['lang'],'en') + for changes in ({'editable':False},{'principal_uuid':'someone-else'}): + with patch.object(client,'raw',return_value=(b'',headers)),patch.object(client,'json',side_effect=[dict(target,head_revision_uuid=''),dict(source,**changes)]): + with self.assertRaises(Failure): client.snapshot('path:/x') + + def test_editor_unchanged_fork_does_not_save(self): + client=Client('https://example.test','token','ja') + state={'object_uuid':'object','head_revision_uuid':'','principal_uuid':'owner','lang':'ja', + 'content_type':'text/plain','filename':'x.txt','body':'source','markdown':'source','new_language':True,'source_language':'und'} + with patch.object(client,'snapshot',return_value=state),patch.object(client,'save') as save,patch('subprocess.run') as command,contextlib.redirect_stdout(io.StringIO()): + command.return_value.returncode=0 + m['editor'](client,'path:/x','auto') + save.assert_not_called() + self.assertFalse(Path(command.call_args.args[0][-1]).exists()) + + def test_edit_language_override_does_not_change_profile(self): + client=Client('https://example.test','token','und') + with tempfile.TemporaryDirectory() as root: + profile=Profile('test',root) + profile.data={'cwd':'/notes'} + def editor_override(editing,addr,mode): + self.assertEqual((editing.base,editing.token,editing.lang),(client.base,client.token,'auto')) + self.assertEqual((addr,mode),('path:/notes/page','auto')) + with patch.dict(m['execute'].__globals__,{'editor':editor_override}): + m['execute'](client,profile,['edit','page','--lang','auto']) + self.assertEqual(client.lang,'und') + self.assertEqual(profile.data,{'cwd':'/notes'}) + with patch.object(client,'snapshot',return_value={'head_revision_uuid':''}),patch.object(client,'save') as save: + with self.assertRaisesRegex(Failure,'use edit --lang'): + m['execute'](client,profile,['put','unused','page','--base','']) + save.assert_not_called() + def test_editor_conflict_retains_private_recovery(self): state={'object_uuid':'object','head_revision_uuid':'base','principal_uuid':'person','lang':'und', 'content_type':'text/plain','filename':'x.txt','body':'old','markdown':'old'} @@ -284,8 +343,9 @@ class RemoteTests(unittest.TestCase): Path(args[-1]).write_text('my edit') class Result: returncode=0 return Result() - with patch('tempfile.mkdtemp',return_value=str(directory)), patch('subprocess.run',side_effect=edit), patch.object(client,'snapshot',return_value=state), patch.object(client,'save',side_effect=Failure('conflict',409)), contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()): + with patch('tempfile.mkdtemp',return_value=str(directory)), patch('subprocess.run',side_effect=edit), patch.object(client,'snapshot',return_value=state) as snapshot, patch.object(client,'save',side_effect=Failure('conflict',409)), contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()): with self.assertRaises(Failure): m['editor'](client,'path:/x','auto') + snapshot.assert_called_with('uuid:object',lang='und') self.assertEqual((directory/'document.txt').read_text(),'my edit') recovery=(directory/'recovery.json').read_text() self.assertNotIn('secret-token',recovery) @@ -342,15 +402,33 @@ def integration(): m['main'](['logout']) assert session_token not in output.getvalue() with Profile('default',root).locked() as session: assert 'token' not in session.data - client=Client(fixture['origin']) + client=Client(fixture['origin'], lang='und') client.login('admin','a long test password') addr='path:/editor-fixture' state=client.snapshot(addr) assert state['lang']=='und' and state['principal_uuid'] missing=Client(fixture['origin'], client.token, 'ja') - try: missing.snapshot(addr) - except Failure: pass - else: raise AssertionError('missing language silently fell back') + fork=missing.snapshot(addr) + assert fork['lang']=='ja' and fork['head_revision_uuid']=='' and fork['new_language'] + assert fork['body']==state['body'] and fork['source_language']=='und' + missing.save(fork,'# Japanese draft\n\n{{children}}\n',True) + assert missing.snapshot(addr)['lang']=='ja' + assert client.snapshot(addr)['head_revision_uuid']==state['head_revision_uuid'] + try: missing.save(fork,'competing first revision') + except Failure as e: assert e.status==409 + else: raise AssertionError('concurrent language creation overwrote the head') + auto=Client(fixture['origin'],client.token) + auto_state=auto.snapshot('path:/ja-only') + assert auto_state['lang']=='ja' and auto_state['head_revision_uuid'] + def edit_japanese(args, **kwargs): + path=Path(args[-1]); path.write_text(path.read_text()+'\nJapanese edit\n') + class Result: returncode=0 + return Result() + with patch('subprocess.run',side_effect=edit_japanese),contextlib.redirect_stdout(io.StringIO()): + m['editor'](auto,'path:/ja-only','auto') + assert 'Japanese edit' in auto.snapshot('path:/ja-only')['body'] + pending=Client(fixture['origin'],client.token,'und').snapshot('path:/ja-only') + assert pending['head_revision_uuid']=='' assert client.listing('/')['entries'] assert client.save(state,'# Remote\n\n{{children}}\n',True)['revision_uuid'] != state['head_revision_uuid'] latest=client.snapshot(addr)