TF-2871 Fix validate expire time in swift code

This commit is contained in:
dab246
2024-06-04 16:57:05 +07:00
committed by Dat H. Pham
parent ef159c78c5
commit d2ee12c2ae
9 changed files with 142 additions and 16 deletions
@@ -4,4 +4,11 @@ extension Date {
func isBefore(_ otherDate: Date) -> Bool {
return self < otherDate
}
func convertDateToISO8601String() -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = CoreUtils.ISO8601_DATE_FORMAT
dateFormatter.locale = Locale(identifier: CoreUtils.EN_US_POSIX_LOCALE)
return dateFormatter.string(from: self)
}
}
@@ -0,0 +1,11 @@
import Foundation
extension Int {
func convertMillisecondsToDate() -> Date {
return Date(timeIntervalSince1970: TimeInterval(self) / 1000)
}
func convertMillisecondsToISO8601String() -> String {
return convertMillisecondsToDate().convertDateToISO8601String()
}
}
@@ -3,8 +3,8 @@ import Foundation
extension String {
func convertISO8601StringToDate() -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = CoreUtils.ISO8601_DATE_FORMAT
dateFormatter.locale = Locale(identifier: CoreUtils.EN_US_POSIX_LOCALE)
return dateFormatter.date(from: self)
}
@@ -69,7 +69,7 @@ class AuthenticationInterceptor: RequestInterceptor {
private func validateToRefreshToken(response: HTTPURLResponse, authenticationSSO: AuthenticationSSO) -> Bool {
return response.statusCode == 401 &&
!authenticationSSO.refreshToken.isEmpty &&
authenticationSSO.isExpiredTime()
authenticationSSO.isExpiredTime(currentDate: CoreUtils.shared.getCurrentDate())
}
// MARK: - Handle refresh token to get new token
@@ -18,23 +18,15 @@ struct AuthenticationSSO: Authentication {
return "Bearer \(accessToken)"
}
func isExpiredTime() -> Bool {
func isExpiredTime(currentDate: Date) -> Bool {
guard let expireTime else {
return false
}
if #available(iOSApplicationExtension 15, *) {
guard let expireDate = expireTime.convertISO8601StringToDate(),
expireDate.isBefore(Date.now) else {
return true
}
if let expireDate = expireTime.convertISO8601StringToDate() {
return expireDate.isBefore(currentDate)
} else {
guard let expireDate = expireTime.convertISO8601StringToDate(),
expireDate.isBefore(Date()) else {
return true
}
return false
}
return false
}
}
+17
View File
@@ -0,0 +1,17 @@
import Foundation
class CoreUtils {
static let shared: CoreUtils = CoreUtils()
static let ISO8601_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS"
static let EN_US_POSIX_LOCALE = "en_US_POSIX"
func getCurrentDate() -> Date {
if #available(iOS 15, *) {
return Date.now
} else {
return Date()
}
}
}