Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed currentServerDate in favour of powerAuthSDK timeService #148

Merged
merged 5 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 7 additions & 40 deletions WultraMobileTokenSDK/Operations/Service/WMTOperationsImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,12 @@ class WMTOperationsImpl<T: WMTUserOperation>: WMTOperations, WMTService {
set { networking.acceptLanguage = newValue }
}

/// Calculated difference between server and device time
private var serverDateShiftInSeconds: TimeInterval?
var currentServerDate: Date? {
if let serverDateShiftInSeconds {
return Date().addingTimeInterval(serverDateShiftInSeconds)
private var currentDate: Date {
let timeService = powerAuth.timeSynchronizationService
if timeService.isTimeSynchronized {
return Date(timeIntervalSince1970: timeService.currentTime())
}
return nil
return Date()
}

private var tasks = [GetOperationsTask]() // Task that are waiting for operation fetch
Expand Down Expand Up @@ -282,8 +281,7 @@ class WMTOperationsImpl<T: WMTUserOperation>: WMTOperations, WMTService {
guard validateActivation(completion) else {
return nil
}

let data = WMTAuthorizationData(operation: operation, timestampSent: currentServerDate ?? Date())
let data = WMTAuthorizationData(operation: operation, timestampSent: currentDate)

return networking.post(data: .init(data), signedWith: authentication, to: WMTOperationEndpoints.Authorize.endpoint) { response, error in
self.processResult(response: response, error: error) { result in
Expand Down Expand Up @@ -403,11 +401,7 @@ class WMTOperationsImpl<T: WMTUserOperation>: WMTOperations, WMTService {
networking.post(data: .init(), signedWith: .possession(), to: WMTOperationEndpoints.List<T>.endpoint) { response, error in

assert(Thread.isMainThread)

if let response {
self.processServerTime(response: response, requestStarted: requestStartDate)
}


// if all tasks were canceled, just ignore the result.
guard self.tasks.contains(where: { $0.isCanceled == false }) else {
completion(.failure(WMTError(reason: .unknown)))
Expand All @@ -430,33 +424,6 @@ class WMTOperationsImpl<T: WMTUserOperation>: WMTOperations, WMTService {
}
}

private func processServerTime(response: WMTOperationListResponse<T>, requestStarted: Date) {

let now = Date()
let requestDelay = now.timeIntervalSince1970 - requestStarted.timeIntervalSince1970

// server does not support this feature
guard var serverTime = response.currentTimestamp else {
return
}

// Reject the value if the request took too long and we already have a server date.
// This is to avoid volatility of the value
if currentServerDate != nil && requestDelay > 1 {
return
}

// We're adding half of the time that the request took to compensate for the network delay
serverTime = serverTime.addingTimeInterval(requestDelay/2)

// If the difference is under 0.3 seconds, we ignore the new value to avoid unnecesary changes that might be due to network delay.
if let currentServerDate, abs(currentServerDate.timeIntervalSince1970 - serverTime.timeIntervalSince1970) < 0.3 {
return
}

serverDateShiftInSeconds = serverTime.timeIntervalSince1970 - now.timeIntervalSince1970
}

/// If request for operation fails at known error code, then this private function adjusts description of given AuthError.
private func adjustOperationError(_ error: WMTError, auth: Bool) -> WMTError {

Expand Down
12 changes: 0 additions & 12 deletions WultraMobileTokenSDK/Operations/WMTOperations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,6 @@ public protocol WMTOperations: AnyObject {
/// Last cached operation result for easy access.
var lastFetchResult: GetOperationsResult? { get }

/// Current server date
///
/// This is calculated property based on the difference between phone date
/// and date on the server.
///
/// This property is available after the first successful operation list request.
/// It might be nil if the server doesn't provide such a feature.
///
/// Note that this value might be incorrent when the user decide to
/// change the system time during the runtime of the application.
var currentServerDate: Date? { get }

/// If operation loading is currently in progress.
var isLoadingOperations: Bool { get }

Expand Down
20 changes: 6 additions & 14 deletions WultraMobileTokenSDKTests/IntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,24 +171,16 @@ class IntegrationTests: XCTestCase {
waitForExpectations(timeout: 20, handler: nil)
}

/// `currentServerDate` is nil by default and after ops fetch, it should be set
/// `currentServerDate` was removed from WMTOperations in favor of more precise powerAuth timeService
func testCurrentServerDate() {
let exp = expectation(description: "Server date should be set after operation fetch")
var synchronizedServerDate: Date? = nil

XCTAssertNil(self.ops.currentServerDate)

_ = ops.getOperations { result in

switch result {
case .success:
XCTAssertNotNil(self.ops.currentServerDate)
case .failure(let err):
XCTFail(err.description)
}
exp.fulfill()
let timeService = pa.timeSynchronizationService
if timeService.isTimeSynchronized {
synchronizedServerDate = Date(timeIntervalSince1970: timeService.currentTime())
}

waitForExpectations(timeout: 20, handler: nil)
XCTAssertNotNil(synchronizedServerDate)
}

/// Test of Login operation approval (1FA)
Expand Down
Loading