Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/main/classes/controllers/AccountController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ export class AccountController {
AccountController.instance = this
}

/**
* Wait for safeStorage encryption to become available
* @param maxRetries Maximum number of retries (default 20)
* @param delayMs Delay between retries in milliseconds (default 500)
* @returns true if encryption became available, false if timed out
*/
private async waitForSafeStorage(maxRetries: number = 20, delayMs: number = 500): Promise<boolean> {
let retries = 0
while (!safeStorage.isEncryptionAvailable() && retries < maxRetries) {
Log.info(`waiting for safeStorage to become available (attempt ${retries + 1}/${maxRetries})`)
await new Promise(resolve => setTimeout(resolve, delayMs))
retries++
}
return safeStorage.isEncryptionAvailable()
}

listAvailableAccounts(): Account[] {
const authAppData = store.store.auth
if (authAppData) return Object.values(authAppData.availableAccounts)
Expand Down Expand Up @@ -76,6 +92,11 @@ export class AccountController {
const lastLoggedAccount = authAppData.availableAccounts[authAppData.lastUser]
if (lastLoggedAccount && authAppData.lastUserCryptPsw) {
try {
// Wait for encryption to become available (max 10 seconds with 500ms intervals)
if (!await this.waitForSafeStorage()) {
Log.warning('auto login failed: safeStorage encryption not available after waiting')
return false
}
try {
const bfs = Object.values(authAppData.lastUserCryptPsw) as any;
if (bfs[0] === 'Buffer') {
Expand Down Expand Up @@ -181,6 +202,11 @@ export class AccountController {

async saveLoggedAccount(account: Account, password: string): Promise<Account> {
try {
// Wait for encryption to become available (max 10 seconds with 500ms intervals)
if (!await this.waitForSafeStorage()) {
Log.error('saveLoggedAccount: safeStorage encryption not available after waiting')
throw new Error('Encryption not available')
}
const clearString = JSON.stringify({ host: account.host, username: account.username, password: password })
const cryptString = safeStorage.encryptString(clearString)
const accountUID = getAccountUID(account)
Expand Down
2 changes: 1 addition & 1 deletion src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function startup() {
if (LoginController.instance && LoginController.instance.window.isOpen() && password && account) {
Log.info("LOGIN SUCCESS")
await LoginController.instance.quit()
AccountController.instance.saveLoggedAccount(account, password)
await AccountController.instance.saveLoggedAccount(account, password)
}
store.saveToDisk()

Expand Down