TF-2384 Handle refresh token when token expired in NSE

Signed-off-by: dab246 <tdvu@linagora.com>
(cherry picked from commit c8a39bb42792770c001ac90d6301d53126225d95)
This commit is contained in:
dab246
2023-12-24 21:45:42 +07:00
committed by Dat H. Pham
parent 5bf1b97de3
commit b1b2bb90a5
19 changed files with 416 additions and 37 deletions
+37 -17
View File
@@ -3,13 +3,13 @@ import Alamofire
class JmapClient {
static let shared: JmapClient = JmapClient()
private let jmapHeader = HTTPHeader(name: "Accept", value: "application/json;jmapVersion=rfc-8621")
private func getBasicAuthorization(basicAuth: String) -> String {
return "Basic \(basicAuth)"
}
private var authentication: Authentication?
private var tokenRefreshManager: TokenRefreshManager?
private var authenticationInterceptor: AuthenticationInterceptor?
func getNewEmails(
apiUrl: String,
accountId: String,
@@ -17,27 +17,47 @@ class JmapClient {
authenticationType: AuthenticationType,
tokenOidc: TokenOidc?,
basicAuth: String?,
tokenEndpointUrl: String?,
oidcScopes: [String]?,
onSuccess: @escaping ([Email]) -> Void,
onFailure: @escaping (Error) -> Void
) {
let authenticationValue = authenticationType == AuthenticationType.basic
? getBasicAuthorization(basicAuth: basicAuth ?? "")
: tokenOidc?.getAuthorization() ?? ""
let headers = HTTPHeaders([
jmapHeader,
HTTPHeader(name: "Authorization", value: authenticationValue)
])
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: headers,
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)