Skip to content

Commit

Permalink
Clear Keychain after fresh install (iOS-315)
Browse files Browse the repository at this point in the history
Files are removed when an app is deleted from device, but the Keychain is not cleared. So now if we find no cached account information upon launch, we remove any accounts remaining in Keychain and the app launches to the Welcome screen.

Fixes iOS-315
  • Loading branch information
whattherestimefor committed Nov 20, 2024
1 parent 299e4f2 commit ef31fc4
Showing 1 changed file with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,26 @@ public extension AuthenticationServiceProvider {

@MainActor
private func restoreFromKeychain() {
self.authentications = Self.keychain.allKeys().compactMap {
var keychainAuthentications: [MastodonAuthentication] = Self.keychain.allKeys().compactMap {
guard
let encoded = Self.keychain[$0],
let data = Data(base64Encoded: encoded)
else { return nil }
return try? JSONDecoder().decode(MastodonAuthentication.self, from: data)
}
.sorted(by: { $0.activedAt > $1.activedAt })
let cachedAccounts = keychainAuthentications.compactMap {
$0.cachedAccount()
}
if cachedAccounts.count == 0 {
// Assume this is a fresh install.
// Clear the keychain of any accounts remaining from previous installs.
for authentication in keychainAuthentications {
try? delete(authentication: authentication)
}
keychainAuthentications = []
}
self.authentications = keychainAuthentications
}

func updateAccountCreatedAt(_ newCreatedAt: Date, forAuthentication outdated: MastodonAuthentication) {
Expand Down

0 comments on commit ef31fc4

Please sign in to comment.