Files
workavia-mail-front/ios/TwakeCore/Jmap/JmapClient.swift
T
dab246 b1b2bb90a5 TF-2384 Handle refresh token when token expired in NSE
Signed-off-by: dab246 <tdvu@linagora.com>
(cherry picked from commit c8a39bb42792770c001ac90d6301d53126225d95)
2024-01-08 14:25:26 +01:00

72 lines
2.5 KiB
Swift

import Foundation
import Alamofire
class JmapClient {
static let shared: JmapClient = JmapClient()
private let jmapHeader = HTTPHeader(name: "Accept", value: "application/json;jmapVersion=rfc-8621")
private var authentication: Authentication?
private var tokenRefreshManager: TokenRefreshManager?
private var authenticationInterceptor: AuthenticationInterceptor?
func getNewEmails(
apiUrl: String,
accountId: String,
sinceState: String,
authenticationType: AuthenticationType,
tokenOidc: TokenOidc?,
basicAuth: String?,
tokenEndpointUrl: String?,
oidcScopes: [String]?,
onSuccess: @escaping ([Email]) -> Void,
onFailure: @escaping (Error) -> Void
) {
if (authenticationType == AuthenticationType.basic) {
authentication = AuthenticationCredential(
type: AuthenticationType.basic,
basicAuth: basicAuth ?? ""
)
} else {
authentication = AuthenticationSSO(
type: AuthenticationType.oidc,
accessToken: tokenOidc?.token ?? "",
refreshToken: tokenOidc?.refreshToken ?? "",
expireTime: tokenOidc?.expiredTime
)
tokenRefreshManager = TokenRefreshManager(
refreshToken: tokenOidc?.refreshToken ?? "",
tokenEndpoint: tokenEndpointUrl ?? "",
scopes: oidcScopes
)
}
authenticationInterceptor = AuthenticationInterceptor(
authentication: authentication,
accountId: accountId,
tokenRefreshManager: tokenRefreshManager
)
let jmapRequestObject = JmapRequestGenerator.shared.createEmailChangesRequest(
accountId: accountId,
sinceState: sinceState
)
AlamofireService.shared.post(
url: apiUrl,
payloadData: jmapRequestObject?.toData(),
headers: HTTPHeaders([jmapHeader]),
interceptor: authenticationInterceptor,
onSuccess: { (data: JmapResponseObject<Email>) in
if let listEmail = data.parsing(methodName: JmapConstants.EMAIL_GET_METHOD_NAME, methodCallId: "c1"), !listEmail.isEmpty {
onSuccess(listEmail)
} else {
onFailure(JmapExceptions.notFoundNewEmails)
}
},
onFailure: onFailure
)
}
}