|
| 1 | +--- |
| 2 | +title: さまざまなウィンドウやプラットフォームでのセキュリティ・レベル |
| 3 | +sidebar: |
| 4 | + order: 11 |
| 5 | +i18nReady: true |
| 6 | +--- |
| 7 | + |
| 8 | +import { Steps } from '@astrojs/starlight/components'; |
| 9 | +import ShowSolution from '@components/ShowSolution.astro'; |
| 10 | +import Cta from '@fragments/cta.mdx'; |
| 11 | +import TranslationNote from '@components/i18n/TranslationNote.astro'; |
| 12 | + |
| 13 | +この章は、Tauri アプリのセキュリティ・レベルをカスタマイズする方法について説明いたします。 |
| 14 | + |
| 15 | +## この章の内容 |
| 16 | + |
| 17 | +- Tauri アプリで複数のウィンドウを作成する |
| 18 | +- ウィンドウごとに異なるセキュリティ・レベルを使用する |
| 19 | +- プラットフォームをに依存するセキュリティ・レベルを使用する |
| 20 | + |
| 21 | +## 事前準備 |
| 22 | + |
| 23 | +この章の演習内容は、「[プラグイン・アクセス権の使用](/ja/learn/security/using-plugin-permissions/)」を読了後に行なうことを前提としています。 |
| 24 | + |
| 25 | +## 手順 |
| 26 | + |
| 27 | +<Steps> |
| 28 | +1. ### Tauri アプリで複数のウィンドウを作成する |
| 29 | + |
| 30 | + この演習では、`first`(第一)と `second`(第二)というラベルの付いた二つのウィンドウを持つアプリを作成します。 |
| 31 | + Tauri アプリケーションでウィンドウを作成する方法はいくつかあります。 |
| 32 | + |
| 33 | + #### Tauri 設定ファイルを使用してウィンドウを作成 |
| 34 | + |
| 35 | + Tauri 設定ファイル(通常は `tauri.conf.json` という名前)では次のようになります: |
| 36 | + |
| 37 | + <ShowSolution> |
| 38 | + ```javascript |
| 39 | + "productName": "multiwindow", |
| 40 | + ... |
| 41 | + "app": { |
| 42 | + "windows": [ |
| 43 | + { |
| 44 | + "label": "first", |
| 45 | + "title": "First", |
| 46 | + "width": 800, |
| 47 | + "height": 600 |
| 48 | + }, |
| 49 | + { |
| 50 | + "label": "second", |
| 51 | + "title": "Second", |
| 52 | + "width": 800, |
| 53 | + "height": 600 |
| 54 | + } |
| 55 | + ], |
| 56 | + }, |
| 57 | + ... |
| 58 | + } |
| 59 | + ``` |
| 60 | + </ShowSolution> |
| 61 | + |
| 62 | + #### プログラムでウィンドウを作成 |
| 63 | + |
| 64 | + Rust コード内で Tauri アプリを作成します: |
| 65 | + |
| 66 | + <ShowSolution> |
| 67 | + ```rust |
| 68 | + tauri::Builder::default() |
| 69 | + .invoke_handler(tauri::generate_handler![greet]) |
| 70 | + .setup(|app| { |
| 71 | + let webview_url = tauri::WebviewUrl::App("index.html".into()); |
| 72 | + // First window(第一ウィンドウ) |
| 73 | + tauri::WebviewWindowBuilder::new(app, "first", webview_url.clone()) |
| 74 | + .title("First") |
| 75 | + .build()?; |
| 76 | + // Second window(第二ウィンドウ) |
| 77 | + tauri::WebviewWindowBuilder::new(app, "second", webview_url) |
| 78 | + .title("Second") |
| 79 | + .build()?; |
| 80 | + Ok(()) |
| 81 | + }) |
| 82 | + .run(context) |
| 83 | + .expect("error while running tauri application"); |
| 84 | + ``` |
| 85 | + </ShowSolution> |
| 86 | + |
| 87 | + |
| 88 | +2. ### 異なるウィンドウに異なるセキュリティ・レベルを適用 |
| 89 | + |
| 90 | + Tauri アプリのウィンドウでは、Tauri バックエンドのさまざまな機能やプラグインを使用できます。 |
| 91 | + セキュリティをより強固にするために、各ウィンドウに必要なセキュリティ機能のみを付与することをお勧めします。 |
| 92 | + この演習では、「第一の `first`」ウィンドウでファイルシステムとダイアログ機能を使用し、「第二の `second`」ウィンドウではダイアログ機能のみを必要としている状況を想定して進めます。 |
| 93 | + |
| 94 | + #### カテゴリごとの個別「セキュリティ・レベル」ファイル |
| 95 | + |
| 96 | + 有効化するアクションのカテゴリごとに「セキュリティ・レベル」ファイルを分割することをお勧めします。 |
| 97 | + |
| 98 | + <ShowSolution> |
| 99 | + `src-tauri/capabilities` 内の JSON ファイルは、「セキュリティ・レベル」システムに対応済みです。 |
| 100 | + そこで、ファイルシステムとダイアログ・ウィンドウに関連するセキュリティ・レベルを `filesystem.json` と `dialog.json` に分割します。 |
| 101 | + |
| 102 | + _Tauri プロジェクトのファイルツリー:_ |
| 103 | + |
| 104 | + ``` |
| 105 | + /src |
| 106 | + /src-tauri |
| 107 | + /capabilities |
| 108 | + filesystem.json |
| 109 | + dialog.json |
| 110 | + tauri.conf.json |
| 111 | + package.json |
| 112 | + README.md |
| 113 | + ``` |
| 114 | + </ShowSolution> |
| 115 | + |
| 116 | + #### 「第一 `first`」ウィンドウにファイルシステムのセキュリティ・レベルを付与 |
| 117 | + |
| 118 | + 「`first`」ウィンドウに、`$HOME` ディレクトリの内容への読み取りアクセス権を付与するようにセキュリティ・レベルを設定します。 |
| 119 | + |
| 120 | + <ShowSolution> |
| 121 | + |
| 122 | + 一つまたは複数のウィンドウ・ラベルを持つ「セキュリティ機能 capability」ファイル内の「`windows` フィールド」を使用します。 |
| 123 | + |
| 124 | + ```json title="filesystem.json" |
| 125 | + { |
| 126 | + "identifier": "fs-read-home", |
| 127 | + "description": "Allow access file access to home directory", |
| 128 | + "local": true, |
| 129 | + "windows": ["first"], |
| 130 | + "permissions": ["fs:allow-home-read"] |
| 131 | + } |
| 132 | + ``` |
| 133 | + |
| 134 | + <TranslationNote lang="ja"> |
| 135 | + |
| 136 | + 「filesystem.json」ファイルの内容抄訳: |
| 137 | + |
| 138 | + - identifier: 識別子名 |
| 139 | + - description: セキュリティ内容説明(home ディレクトリへのファイル・アクセスを許可) |
| 140 | + - local: true |
| 141 | + - windows: ウィンドウ名 |
| 142 | + - permissions: アクセス権設定 |
| 143 | + |
| 144 | + </TranslationNote> |
| 145 | + |
| 146 | + </ShowSolution> |
| 147 | + |
| 148 | + #### 「第一 `first`」と「第二 `second`」ウィンドウにダイアログ機能を付与 |
| 149 | + |
| 150 | + 「`first`」と「`second`」ウィンドウに「Yes/No」ダイアログを作成する機能を追加します。 |
| 151 | + |
| 152 | + <ShowSolution> |
| 153 | + |
| 154 | + 一つまたは複数のウィンドウ・ラベルを持つ「セキュリティ機能」ファイル内の `windows` フィールドを使用します。 |
| 155 | + |
| 156 | + ```json title="dialog.json" |
| 157 | + { |
| 158 | + "identifier": "dialog", |
| 159 | + "description": "Allow to open a dialog", |
| 160 | + "local": true, |
| 161 | + "windows": ["first", "second"], |
| 162 | + "permissions": ["dialog:allow-ask"] |
| 163 | + } |
| 164 | + ``` |
| 165 | + |
| 166 | + </ShowSolution> |
| 167 | + |
| 168 | +3. ### セキュリティ機能をプラットフォーム依存にする |
| 169 | + |
| 170 | + 次に、特定のプラットフォームでのみアクティブになるようにセキュリティ機能をカスタマイズします。 |
| 171 | + 以下の事例では、ファイルシステムのセキュリティ機能を `linux` と `windows` でのみ有効にします。 |
| 172 | + |
| 173 | + <ShowSolution> |
| 174 | + |
| 175 | + プラットフォーム別に設定するには、セキュリティ機能ファイルの「`platforms` フィールド」で対象プラットフォームを指定します。 |
| 176 | + |
| 177 | + ```json title="filesystem.json" |
| 178 | + { |
| 179 | + "identifier": "fs-read-home", |
| 180 | + "description": "Allow access file access to home directory", |
| 181 | + "local": true, |
| 182 | + "windows": ["first"], |
| 183 | + "permissions": ["fs:allow-home-read"], |
| 184 | + "platforms": ["linux", "windows"] |
| 185 | + } |
| 186 | + ``` |
| 187 | + |
| 188 | + 現在指定可能なプラットフォームは、`linux`、`windows`、`macos`、`android`、および `ios` です。 |
| 189 | + |
| 190 | + </ShowSolution> |
| 191 | + |
| 192 | +</Steps> |
| 193 | + |
| 194 | +## 本章のまとめ と 参考資料 |
| 195 | + |
| 196 | +Tauri アプリで複数のウィンドウを作成し、それぞれに別々のセキュリティ機能を与える方法を学びました。さらに、こうしたセキュリティ機能は特定のプラットフォーム向けにカスタマイズすることも可能です。 |
| 197 | + |
| 198 | +ウィンドウ機能を使用したサンプル・アプリケーションは、[Tauri Github リポジトリ](https://github.com/tauri-apps/tauri) の [`api` サンプル](https://github.com/tauri-apps/tauri/tree/dev/examples/api) 内にあります。 |
| 199 | +「セキュリティ機能ファイル capability file」で使用できるフィールドは、TAURI Doc の「メニュー Menu」内にある「Reference」の [Capability](/reference/acl/capability/) 部分にリストされています。 |
| 200 | + |
| 201 | +<div style="text-align: right;"> |
| 202 | + 【※ この日本語版は、「Feb 22, 2025 英語版」に基づいています】 |
| 203 | +</div> |
0 commit comments