Skip to content

Commit

Permalink
Merge pull request #1237 from Infomaniak/fix-lost-token
Browse files Browse the repository at this point in the history
fix: Prevent replacing wrong token
  • Loading branch information
PhilippeWeidmann committed Jan 24, 2024
2 parents d91d904 + 90cfbe9 commit a1cf2c2
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions MailCore/API/SyncedAuthenticator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,29 @@ final class SyncedAuthenticator: OAuthAuthenticator {
return
}

if let storedToken = tokenStore.tokenFor(userId: credential.userId, fetchLocation: .keychain) {
// Someone else refreshed our token and we already have an infinite token
if storedToken.expirationDate == nil && credential.expirationDate != nil {
SentrySDK.addBreadcrumb(storedToken.generateBreadcrumb(
level: .info,
message: "Refreshing token - Success with local (infinite)"
))
completion(.success(storedToken))
return
}
// Someone else refreshed our token and we don't have an infinite token
if let storedTokenExpirationDate = storedToken.expirationDate,
let tokenExpirationDate = credential.expirationDate,
tokenExpirationDate > storedTokenExpirationDate {
SentrySDK.addBreadcrumb(storedToken.generateBreadcrumb(
level: .info,
message: "Refreshing token - Success with local"
))
completion(.success(storedToken))
return
}
}

// It is absolutely necessary that the app stays awake while we refresh the token
BackgroundExecutor.executeWithBackgroundTask { endBackgroundTask in
self.networkLoginService.refreshToken(token: credential) { token, error in
Expand Down

0 comments on commit a1cf2c2

Please sign in to comment.