Skip to content

Commit 33580b0

Browse files
committed
chore: trust ring porting in electron
1 parent 0c5dfc9 commit 33580b0

File tree

1 file changed

+40
-16
lines changed

1 file changed

+40
-16
lines changed

src/phoenix/trust_ring.js

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -185,37 +185,52 @@ const VERSION_PORTER_KEY = Phoenix.isTestWindow ? "VERSION_PORTER_TEST" : "VERSI
185185
const { key, iv } = _selectKeys();
186186

187187
async function setCredential(credKey, secret) {
188-
if(!window.__TAURI__){
189-
throw new Error("Phoenix API key can only be set in tauri shell!");
188+
if(!window.__IS_NATIVE_SHELL__){
189+
throw new Error("Phoenix API key can only be set in native shell!");
190190
}
191191
if(!credKey){
192192
throw new Error("credKey is required to set credential!");
193193
}
194-
return window.__TAURI__.tauri.invoke("store_credential", {scopeName: credKey, secretVal: secret});
194+
if(window.__TAURI__) {
195+
return window.__TAURI__.tauri.invoke("store_credential", {scopeName: credKey, secretVal: secret});
196+
}
197+
if(window.__ELECTRON__) {
198+
return window.electronAPI.storeCredential(credKey, secret);
199+
}
195200
}
196201

197202
async function getCredential(credKey) {
198-
if(!window.__TAURI__){
199-
throw new Error("Phoenix API key can only be get in tauri shell!");
203+
if(!window.__IS_NATIVE_SHELL__){
204+
throw new Error("Phoenix API key can only be get in native shell!");
200205
}
201206
if(!credKey){
202207
throw new Error("credKey is required to get credential!");
203208
}
204-
const encryptedKey = await window.__TAURI__.tauri.invoke("get_credential", {scopeName: credKey});
209+
let encryptedKey;
210+
if(window.__TAURI__) {
211+
encryptedKey = await window.__TAURI__.tauri.invoke("get_credential", {scopeName: credKey});
212+
} else if(window.__ELECTRON__) {
213+
encryptedKey = await window.electronAPI.getCredential(credKey);
214+
}
205215
if(!encryptedKey){
206216
return null;
207217
}
208218
return AESDecryptString(encryptedKey, key, iv);
209219
}
210220

211221
async function removeCredential(credKey) {
212-
if(!window.__TAURI__){
213-
throw new Error("Phoenix API key can only be set in tauri shell!");
222+
if(!window.__IS_NATIVE_SHELL__){
223+
throw new Error("Phoenix API key can only be removed in native shell!");
214224
}
215225
if(!credKey){
216226
throw new Error("credKey is required to remove credential!");
217227
}
218-
return window.__TAURI__.tauri.invoke("delete_credential", {scopeName: credKey});
228+
if(window.__TAURI__) {
229+
return window.__TAURI__.tauri.invoke("delete_credential", {scopeName: credKey});
230+
}
231+
if(window.__ELECTRON__) {
232+
return window.electronAPI.deleteCredential(credKey);
233+
}
219234
}
220235

221236
let _dismatled = false;
@@ -231,25 +246,34 @@ async function dismantleKeyring() {
231246
console.error("Invalid kernal keys supplied to shutdown. Ignoring kernal trust reset at shutdown.");
232247
return;
233248
}
234-
if(!window.__TAURI__){
249+
if(!window.__IS_NATIVE_SHELL__){
235250
return;
236251
}
237-
return window.__TAURI__.tauri.invoke("remove_trust_window_aes_key", {key, iv});
252+
if(window.__TAURI__) {
253+
return window.__TAURI__.tauri.invoke("remove_trust_window_aes_key", {key, iv});
254+
}
255+
if(window.__ELECTRON__) {
256+
return window.electronAPI.removeTrustWindowAesKey(key, iv);
257+
}
238258
}
239259

240260
export async function initTrustRing() {
241-
if(!window.__TAURI__){
261+
if(!window.__IS_NATIVE_SHELL__){
242262
return;
243263
}
244264
// this will only work once in a window unless dismantleKeyring is called. So this is safe as
245265
// a public export as essentially this is a fn that only works in the boot and shutdown phase.
246-
await window.__TAURI__.tauri.invoke("trust_window_aes_key", {key, iv});
266+
if(window.__TAURI__) {
267+
await window.__TAURI__.tauri.invoke("trust_window_aes_key", {key, iv});
268+
} else if(window.__ELECTRON__) {
269+
await window.electronAPI.trustWindowAesKey(key, iv);
270+
}
247271

248-
_portCredentials();
272+
await _portCredentials();
249273
}
250274
async function reinstallCreds() {
251-
if(!window.__TAURI__){
252-
throw new Error("reinstallCreds can only be called in tauri shell!");
275+
if(!window.__IS_NATIVE_SHELL__){
276+
throw new Error("reinstallCreds can only be called in native shell!");
253277
}
254278
// Read current credential values
255279
const apiKey = await getCredential(CRED_KEY_API);

0 commit comments

Comments
 (0)