( customPath: string )
| 462 | }; |
| 463 | |
| 464 | export const setCustomKeyPath = async ( |
| 465 | customPath: string |
| 466 | ): Promise<Result<{ user: User }>> => { |
| 467 | try { |
| 468 | const user = await getCurrentUser(); |
| 469 | if (!user?.username) { |
| 470 | return { success: false, error: "Not authenticated" }; |
| 471 | } |
| 472 | |
| 473 | try { |
| 474 | await fs.access(customPath); |
| 475 | } catch { |
| 476 | return { success: false, error: "Custom path is not accessible" }; |
| 477 | } |
| 478 | |
| 479 | const result = await updateUserSettings({ |
| 480 | encryptionSettings: { |
| 481 | method: user.encryptionSettings?.method || "pgp", |
| 482 | hasKeys: user.encryptionSettings?.hasKeys ?? false, |
| 483 | autoDecrypt: user.encryptionSettings?.autoDecrypt ?? true, |
| 484 | customKeyPath: customPath, |
| 485 | }, |
| 486 | }); |
| 487 | |
| 488 | if (!result.success || !result.data) { |
| 489 | await logAudit({ |
| 490 | level: "ERROR", |
| 491 | action: "encryption_key_path_changed", |
| 492 | category: "encryption", |
| 493 | success: false, |
| 494 | errorMessage: result.error || "Failed to set custom key path", |
| 495 | }); |
| 496 | return { |
| 497 | success: false, |
| 498 | error: result.error || "Failed to set custom key path", |
| 499 | }; |
| 500 | } |
| 501 | |
| 502 | await logAudit({ |
| 503 | level: "INFO", |
| 504 | action: "encryption_key_path_changed", |
| 505 | category: "encryption", |
| 506 | success: true, |
| 507 | metadata: { customPath }, |
| 508 | }); |
| 509 | |
| 510 | return { success: true, data: { user: result.data.user } }; |
| 511 | } catch (error) { |
| 512 | console.error("Error setting custom key path:", error); |
| 513 | await logAudit({ |
| 514 | level: "ERROR", |
| 515 | action: "encryption_key_path_changed", |
| 516 | category: "encryption", |
| 517 | success: false, |
| 518 | errorMessage: "Failed to set custom key path", |
| 519 | }); |
| 520 | return { success: false, error: "Failed to set custom key path" }; |
| 521 | } |
no test coverage detected