Skip to content

Commit

Permalink
Merge pull request #1228 from Infomaniak/end-of-refresh-token
Browse files Browse the repository at this point in the history
feat: End of refresh token
  • Loading branch information
PhilippeWeidmann committed Jan 17, 2024
2 parents 468cd6d + 9b60d4b commit d43dc2e
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 52 deletions.
42 changes: 21 additions & 21 deletions .package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/Alamofire/Alamofire",
"state" : {
"revision" : "b2fa556e4e48cbf06cf8c63def138c98f4b811fa",
"version" : "5.8.0"
"revision" : "3dc6a42c7727c49bf26508e29b0a0b35f9c7e1ad",
"version" : "5.8.1"
}
},
{
"identity" : "atlantis",
"kind" : "remoteSourceControl",
"location" : "https://github.com/ProxymanApp/atlantis",
"state" : {
"revision" : "cfa72085bce2600b28e47fdbbbfa2d5b96f0392b",
"version" : "1.22.0"
"revision" : "131d757cf8e6e368ad338728379174f7cfff9326",
"version" : "1.23.0"
}
},
{
"identity" : "cocoalumberjack",
"kind" : "remoteSourceControl",
"location" : "https://github.com/CocoaLumberjack/CocoaLumberjack",
"state" : {
"revision" : "67ec5818a757aba4d7c534e21a905d878d128dbf",
"version" : "3.8.1"
"revision" : "363ed23d19a931809ea834a7d722da830353806a",
"version" : "3.8.2"
}
},
{
Expand All @@ -41,25 +41,25 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/Infomaniak/ios-core",
"state" : {
"revision" : "1ec38915366434b250a50573be12568f022f255e"
"revision" : "fcb77e55122826fc21e5e73ccc2a79126a969a42"
}
},
{
"identity" : "ios-core-ui",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Infomaniak/ios-core-ui",
"state" : {
"revision" : "2ade7b9cfc5942d1126570b40a6680e4ffcff105",
"version" : "3.0.0"
"revision" : "400b26b6a266bb9206ad4eade1231a9f264e98e7",
"version" : "4.0.0"
}
},
{
"identity" : "ios-create-account",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Infomaniak/ios-create-account",
"state" : {
"revision" : "6e0c03e842b39454a8a1c17391fe81790c587f86",
"version" : "2.0.0"
"revision" : "233986fce330fdc588d3e5155500b703f9748539",
"version" : "3.0.0"
}
},
{
Expand All @@ -76,8 +76,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/Infomaniak/ios-login",
"state" : {
"revision" : "ba2e72bc84a673d4e1626f898419da82b54baefb",
"version" : "5.0.0"
"revision" : "94dee9d95d92c5fbe152476b78b04985fbddaa7c",
"version" : "6.0.1"
}
},
{
Expand Down Expand Up @@ -139,17 +139,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/johnpatrickmorgan/NavigationBackport",
"state" : {
"revision" : "fb860a404f8c0aabeca1016400e41ece5363a369",
"version" : "0.9.0"
"revision" : "b002991691b54fd7b952e1e780de252620f8ac77",
"version" : "0.9.1"
}
},
{
"identity" : "nuke",
"kind" : "remoteSourceControl",
"location" : "https://github.com/kean/Nuke",
"state" : {
"revision" : "3f666f120b63ea7de57d42e9a7c9b47f8e7a290b",
"version" : "12.1.6"
"revision" : "15fde63470d782c897816a74bdd516a907e33147",
"version" : "12.3.0"
}
},
{
Expand Down Expand Up @@ -202,8 +202,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/SnapKit/SnapKit.git",
"state" : {
"revision" : "f222cbdf325885926566172f6f5f06af95473158",
"version" : "5.6.0"
"revision" : "e74fe2a978d1216c3602b129447c7301573cc2d8",
"version" : "5.7.0"
}
},
{
Expand Down Expand Up @@ -292,8 +292,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/siteline/SwiftUI-Introspect",
"state" : {
"revision" : "3ced72fccb284235c266c1e8a32f1bf93129fc74",
"version" : "1.1.0"
"revision" : "18e965f867e37a558ad13c12d330dbe047099180",
"version" : "1.1.2"
}
},
{
Expand Down
4 changes: 2 additions & 2 deletions Mail/Helpers/PreviewHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ enum PreviewHelper {
static var mockSelectedThreadOwner = MockSelectedThreadOwner()

private class PreviewHelperRefreshTokenDelegate: RefreshTokenDelegate {
func didUpdateToken(newToken: InfomaniakCore.ApiToken, oldToken: InfomaniakCore.ApiToken) {
func didUpdateToken(newToken: ApiToken, oldToken: ApiToken) {
// No implementation
}

func didFailRefreshToken(_ token: InfomaniakCore.ApiToken) {
func didFailRefreshToken(_ token: ApiToken) {
// No implementation
}
}
Expand Down
2 changes: 0 additions & 2 deletions MailCore/API/MailApiFetcher/MailApiFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ public extension ApiFetcher {
}

public final class MailApiFetcher: ApiFetcher, MailApiFetchable {
public static let clientId = "E90BC22D-67A8-452C-BE93-28DA33588CA4"

/// All status except 401 are handled by our code, 401 status is handled by Alamofire's Authenticator code
private lazy var handledHttpStatus: Set<Int> = {
var allStatus = Set(200 ... 500)
Expand Down
8 changes: 0 additions & 8 deletions MailCore/API/SyncedAuthenticator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,6 @@ final class SyncedAuthenticator: OAuthAuthenticator {
return
}

// Maybe someone else refreshed our token
if let token = tokenStore.tokenFor(userId: credential.userId, fetchLocation: .keychain),
token.expirationDate > credential.expirationDate {
SentrySDK.addBreadcrumb(token.generateBreadcrumb(level: .info, message: "Refreshing token - Success with local"))
completion(.success(token))
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
15 changes: 2 additions & 13 deletions MailCore/Cache/AccountManager/AccountManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,6 @@ import Sentry
import SwiftUI

public extension InfomaniakNetworkLoginable {
func apiToken(username: String, applicationPassword: String) async throws -> ApiToken {
try await withCheckedThrowingContinuation { continuation in
getApiToken(username: username, applicationPassword: applicationPassword) { token, error in
if let token {
continuation.resume(returning: token)
} else {
continuation.resume(throwing: error ?? MailError.unknownError)
}
}
}
}

func apiToken(using code: String, codeVerifier: String) async throws -> ApiToken {
try await withCheckedThrowingContinuation { continuation in
getApiTokenUsing(code: code, codeVerifier: codeVerifier) { token, error in
Expand Down Expand Up @@ -218,13 +206,14 @@ public final class AccountManager: RefreshTokenDelegate, ObservableObject {
}

public func didUpdateToken(newToken: ApiToken, oldToken: ApiToken) {
SentryDebug.logTokenMigration(newToken: newToken, oldToken: oldToken)
tokenStore.addToken(newToken: newToken)
}

public func didFailRefreshToken(_ token: ApiToken) {
SentrySDK.capture(message: "Failed refreshing token") { scope in
scope.setContext(
value: ["User id": token.userId, "Expiration date": token.expirationDate.timeIntervalSince1970],
value: ["User id": token.userId, "Expiration date": token.expirationDate?.timeIntervalSince1970 ?? "infinite"],
key: "Token Infos"
)
}
Expand Down
1 change: 1 addition & 0 deletions MailCore/Cache/TokenStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import Foundation
import InfomaniakCore
import InfomaniakDI
import InfomaniakLogin

public class TokenStore {
public enum TokenStoreFetchLocation {
Expand Down
1 change: 1 addition & 0 deletions MailCore/Models/Contact/AvatarImageRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import Foundation
import InfomaniakCore
import InfomaniakLogin
import Nuke

public struct AvatarImageRequest {
Expand Down
20 changes: 20 additions & 0 deletions MailCore/Utils/SentryDebug.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

import Foundation
import InfomaniakLogin
import RealmSwift
import Sentry

Expand Down Expand Up @@ -191,6 +192,25 @@ public enum SentryDebug {
}
}

static func logTokenMigration(newToken: ApiToken, oldToken: ApiToken) {
let newTokenIsInfinite = newToken.expirationDate == nil
let oldTokenIsInfinite = oldToken.expirationDate == nil

let additionalData = ["newTokenIsInfinite": newTokenIsInfinite, "oldTokenIsInfinite": oldTokenIsInfinite]
let breadcrumb = Breadcrumb(level: .info, category: "Token")
breadcrumb.message = "Token updated"
breadcrumb.data = additionalData
SentrySDK.addBreadcrumb(breadcrumb)

// Only track migration
guard newTokenIsInfinite else { return }

SentrySDK.capture(message: "Update token infinite token") { scope in
scope.setContext(value: additionalData,
key: "Migration context")
}
}

static func addBackoffBreadcrumb(folder: Folder, index: Int) {
let breadcrumb = Breadcrumb()
breadcrumb.message = "Backoff \(index) for folder \(folder.name) - \(folder.remoteId)"
Expand Down
6 changes: 4 additions & 2 deletions MailCore/Utils/TargetAssembly.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import InfomaniakNotifications
private let realmRootPath = "mailboxes"
private let appGroupIdentifier = "group.com.infomaniak.mail"

private let loginConfig = InfomaniakLogin.Config(clientId: "E90BC22D-67A8-452C-BE93-28DA33588CA4", accessType: nil)

extension [Factory] {
func registerFactoriesInDI() {
forEach { SimpleResolver.sharedResolver.store(factory: $0) }
Expand All @@ -53,10 +55,10 @@ open class TargetAssembly {
MailboxInfosManager()
},
Factory(type: InfomaniakNetworkLoginable.self) { _, _ in
InfomaniakNetworkLogin(clientId: MailApiFetcher.clientId)
InfomaniakNetworkLogin(config: loginConfig)
},
Factory(type: InfomaniakLoginable.self) { _, _ in
InfomaniakLogin(clientId: MailApiFetcher.clientId)
InfomaniakLogin(config: loginConfig)
},
Factory(type: KeychainHelper.self) { _, _ in
KeychainHelper(accessGroup: AccountManager.accessGroup)
Expand Down
8 changes: 4 additions & 4 deletions Project.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ import ProjectDescriptionHelpers
let project = Project(name: "Mail",
packages: [
.package(url: "https://github.com/apple/swift-algorithms", .upToNextMajor(from: "1.2.0")),
.package(url: "https://github.com/Infomaniak/ios-login", .upToNextMajor(from: "5.0.0")),
.package(url: "https://github.com/Infomaniak/ios-login", .upToNextMajor(from: "6.0.0")),
.package(url: "https://github.com/Infomaniak/ios-dependency-injection", .upToNextMajor(from: "2.0.0")),
.package(url: "https://github.com/Infomaniak/swift-concurrency", .upToNextMajor(from: "0.0.4")),
.package(
url: "https://github.com/Infomaniak/ios-core",
.revision("1ec38915366434b250a50573be12568f022f255e")
.revision("fcb77e55122826fc21e5e73ccc2a79126a969a42")
),
.package(url: "https://github.com/Infomaniak/ios-core-ui", .upToNextMajor(from: "3.0.0")),
.package(url: "https://github.com/Infomaniak/ios-core-ui", .upToNextMajor(from: "4.0.0")),
.package(url: "https://github.com/Infomaniak/ios-notifications", .upToNextMajor(from: "3.0.0")),
.package(url: "https://github.com/Infomaniak/ios-create-account", .upToNextMajor(from: "2.0.0")),
.package(url: "https://github.com/Infomaniak/ios-create-account", .upToNextMajor(from: "3.0.0")),
.package(url: "https://github.com/Infomaniak/ios-bug-tracker", .upToNextMajor(from: "2.0.0")),
.package(url: "https://github.com/Infomaniak/SQRichTextEditor", .upToNextMajor(from: "1.1.1")),
.package(url: "https://github.com/Infomaniak/SwiftSoup", .upToNextMajor(from: "1.1.0")),
Expand Down

0 comments on commit d43dc2e

Please sign in to comment.