diff --git a/CovidSafe.xcodeproj/project.pbxproj b/CovidSafe.xcodeproj/project.pbxproj index 75ad59e..bf1e0ee 100644 --- a/CovidSafe.xcodeproj/project.pbxproj +++ b/CovidSafe.xcodeproj/project.pbxproj @@ -333,7 +333,6 @@ 0BA617CD242E09B200E6C631 /* FeedbackViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedbackViewController.swift; sourceTree = ""; }; 0BC141AB24305D9C00399FA8 /* NSMutableString + Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSMutableString + Extensions.swift"; sourceTree = ""; }; 0BC141AD2430685800399FA8 /* UIColor + Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIColor + Extensions.swift"; sourceTree = ""; }; - 122AF4E79D17C983066C1CEB /* Pods-CovidSafe-staging.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CovidSafe-staging.release.xcconfig"; path = "Target Support Files/Pods-CovidSafe-staging/Pods-CovidSafe-staging.release.xcconfig"; sourceTree = ""; }; 1925EA5F4413AD52AC198894 /* Pods-CovidSafe.covid-production.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CovidSafe.covid-production.xcconfig"; path = "Target Support Files/Pods-CovidSafe/Pods-CovidSafe.covid-production.xcconfig"; sourceTree = ""; }; 1B86119024303EF200EA4B6B /* Question1ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Question1ViewController.swift; sourceTree = ""; }; 1B86119224303F4A00EA4B6B /* Question2ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Question2ViewController.swift; sourceTree = ""; }; @@ -342,7 +341,6 @@ 1B86119824303F9600EA4B6B /* Question2ErrorViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Question2ErrorViewController.swift; sourceTree = ""; }; 1B86119A24303FA200EA4B6B /* Question3ErrorViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Question3ErrorViewController.swift; sourceTree = ""; }; 1B86119C24303FC000EA4B6B /* QuestionUploadDataViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QuestionUploadDataViewController.swift; sourceTree = ""; }; - 20A0AADA27329C83CFAB5A7C /* Pods-CovidSafe.covid-staging.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CovidSafe.covid-staging.xcconfig"; path = "Target Support Files/Pods-CovidSafe/Pods-CovidSafe.covid-staging.xcconfig"; sourceTree = ""; }; 2B916D8946F8A94E32E569C7 /* Pods-CovidSafe.covid-staging.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CovidSafe.covid-staging.xcconfig"; path = "Target Support Files/Pods-CovidSafe/Pods-CovidSafe.covid-staging.xcconfig"; sourceTree = ""; }; 30BE1CA923F108BA005DCE4F /* UploadFileData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UploadFileData.swift; sourceTree = ""; }; 30BE1CAE23F1349F005DCE4F /* EncounterRecord.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EncounterRecord.swift; sourceTree = ""; }; @@ -350,12 +348,9 @@ 30BE1CB423F15D47005DCE4F /* OTPViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OTPViewController.swift; sourceTree = ""; }; 30E91BE823EFE514002D592A /* UploadDataVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UploadDataVC.swift; sourceTree = ""; }; 30E91BEA23EFEA0B002D592A /* CodeInputView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CodeInputView.swift; sourceTree = ""; }; - 342FF717762F714E56F4412D /* Pods-CovidSafe.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CovidSafe.release.xcconfig"; path = "Target Support Files/Pods-CovidSafe/Pods-CovidSafe.release.xcconfig"; sourceTree = ""; }; 34E9EB96EA6E42A571E73C8E /* Pods_CovidSafe.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_CovidSafe.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 39E21BD3842669F051A5D6D8 /* Pods-CovidSafe.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CovidSafe.debug.xcconfig"; path = "Target Support Files/Pods-CovidSafe/Pods-CovidSafe.debug.xcconfig"; sourceTree = ""; }; - 3A403F07F3B7C5A94CBFC43D /* Pods-CovidSafe-staging.covid-staging.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CovidSafe-staging.covid-staging.xcconfig"; path = "Target Support Files/Pods-CovidSafe-staging/Pods-CovidSafe-staging.covid-staging.xcconfig"; sourceTree = ""; }; 46A5730925DA6B664DFE9546 /* Pods-CovidSafe-staging.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CovidSafe-staging.debug.xcconfig"; path = "Target Support Files/Pods-CovidSafe-staging/Pods-CovidSafe-staging.debug.xcconfig"; sourceTree = ""; }; - 46D0C3F015CA753BB4D4787D /* Pods-CovidSafe-staging.covid-production.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CovidSafe-staging.covid-production.xcconfig"; path = "Target Support Files/Pods-CovidSafe-staging/Pods-CovidSafe-staging.covid-production.xcconfig"; sourceTree = ""; }; 49A22E09D113DF058C94C6E6 /* Pods-CovidSafe-staging.covid-staging.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CovidSafe-staging.covid-staging.xcconfig"; path = "Target Support Files/Pods-CovidSafe-staging/Pods-CovidSafe-staging.covid-staging.xcconfig"; sourceTree = ""; }; 5904A5C12462471A008C8012 /* EncounterV1toV2.xcmappingmodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcmappingmodel; path = EncounterV1toV2.xcmappingmodel; sourceTree = ""; }; 5909E4AA245043C400D41C26 /* CovidPersistentContainer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CovidPersistentContainer.swift; sourceTree = ""; }; @@ -474,8 +469,6 @@ 5DD41D5223DD4CA400FD4AB0 /* PeripheralController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PeripheralController.swift; sourceTree = ""; }; 5DD41D7823DE141700FD4AB0 /* Model.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = Model.xcdatamodel; sourceTree = ""; }; 5DDB85EA23EE39C000B186BC /* Project Bluetrace.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = "Project Bluetrace.entitlements"; sourceTree = ""; }; - 679DE00E3E364DA756795844 /* Pods-CovidSafe.covid-production.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CovidSafe.covid-production.xcconfig"; path = "Target Support Files/Pods-CovidSafe/Pods-CovidSafe.covid-production.xcconfig"; sourceTree = ""; }; - 6EBCA1AD6D10F8DF7BBDC660 /* Pods-CovidSafe.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CovidSafe.debug.xcconfig"; path = "Target Support Files/Pods-CovidSafe/Pods-CovidSafe.debug.xcconfig"; sourceTree = ""; }; 7F19B4DC23F565850071A11E /* PogoInstructionsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PogoInstructionsViewController.swift; sourceTree = ""; }; 7F2F0BA123EFFF75006D7404 /* OnboardingStep2ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingStep2ViewController.swift; sourceTree = ""; }; 7F36305E23F7F81400CC6E1D /* PushNotificationConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PushNotificationConstants.swift; sourceTree = ""; }; @@ -522,7 +515,6 @@ D8EB201A23FA722D001C60EC /* HelpNavController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HelpNavController.swift; sourceTree = ""; }; D8EB201C23FBE216001C60EC /* help_center_article_style.css */ = {isa = PBXFileReference; lastKnownFileType = text.css; path = help_center_article_style.css; sourceTree = ""; }; DC24373E23F51531007BDBDF /* covid-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "covid-Bridging-Header.h"; sourceTree = ""; }; - E3B9CCF78D93EFF0EF2A0ADB /* Pods-CovidSafe-staging.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CovidSafe-staging.debug.xcconfig"; path = "Target Support Files/Pods-CovidSafe-staging/Pods-CovidSafe-staging.debug.xcconfig"; sourceTree = ""; }; FB12C4C0242F047F007E893B /* RespondToAuthChallengeAPI.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RespondToAuthChallengeAPI.swift; sourceTree = ""; }; FB12C4C2242F0FE9007E893B /* GetTempIdAPI.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GetTempIdAPI.swift; sourceTree = ""; }; FB12C4C424304AF0007E893B /* OnboardingStep1aViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OnboardingStep1aViewController.swift; sourceTree = ""; }; @@ -954,14 +946,6 @@ C1FDFBCD3FA29CDE38625AB0 /* Pods */ = { isa = PBXGroup; children = ( - 20A0AADA27329C83CFAB5A7C /* Pods-CovidSafe.covid-staging.xcconfig */, - 679DE00E3E364DA756795844 /* Pods-CovidSafe.covid-production.xcconfig */, - 3A403F07F3B7C5A94CBFC43D /* Pods-CovidSafe-staging.covid-staging.xcconfig */, - 46D0C3F015CA753BB4D4787D /* Pods-CovidSafe-staging.covid-production.xcconfig */, - 6EBCA1AD6D10F8DF7BBDC660 /* Pods-CovidSafe.debug.xcconfig */, - 342FF717762F714E56F4412D /* Pods-CovidSafe.release.xcconfig */, - E3B9CCF78D93EFF0EF2A0ADB /* Pods-CovidSafe-staging.debug.xcconfig */, - 122AF4E79D17C983066C1CEB /* Pods-CovidSafe-staging.release.xcconfig */, 39E21BD3842669F051A5D6D8 /* Pods-CovidSafe.debug.xcconfig */, 2B916D8946F8A94E32E569C7 /* Pods-CovidSafe.covid-staging.xcconfig */, 03E2D9045555F5F013130375 /* Pods-CovidSafe.release.xcconfig */, @@ -1643,7 +1627,7 @@ CODE_SIGN_ENTITLEMENTS = "CovidSafe/Project Bluetrace.entitlements"; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 80; + CURRENT_PROJECT_VERSION = 83; DEVELOPMENT_TEAM = 45792XH5L8; INFOPLIST_FILE = "$(SRCROOT)/CovidSafe/Info.plist"; IPHONEOS_DEPLOYMENT_TARGET = 10.0; @@ -1651,7 +1635,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.13; + MARKETING_VERSION = 1.14; ONLY_ACTIVE_ARCH = YES; PRODUCT_BUNDLE_IDENTIFIER = au.gov.health.covidsafe; PRODUCT_NAME = COVIDSafe; @@ -1727,7 +1711,7 @@ CODE_SIGN_ENTITLEMENTS = "CovidSafe/Project Bluetrace.entitlements"; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 80; + CURRENT_PROJECT_VERSION = 83; DEVELOPMENT_TEAM = 45792XH5L8; INFOPLIST_FILE = "$(SRCROOT)/CovidSafe/Info.plist"; IPHONEOS_DEPLOYMENT_TARGET = 10.0; @@ -1735,7 +1719,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.13; + MARKETING_VERSION = 1.14; PRODUCT_BUNDLE_IDENTIFIER = au.gov.health.covidsafe; PRODUCT_NAME = COVIDSafe; PROVISIONING_PROFILE_SPECIFIER = ""; @@ -1755,7 +1739,7 @@ CODE_SIGN_ENTITLEMENTS = "CovidSafe/Project Bluetrace.entitlements"; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 80; + CURRENT_PROJECT_VERSION = 83; DEVELOPMENT_TEAM = 45792XH5L8; INFOPLIST_FILE = "$(SRCROOT)/CovidSafe/Info.plist"; IPHONEOS_DEPLOYMENT_TARGET = 10.0; @@ -1763,7 +1747,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.13; + MARKETING_VERSION = 1.14; ONLY_ACTIVE_ARCH = YES; OTHER_SWIFT_FLAGS = "$(inherited) -D COCOAPODS -D DEBUG"; PRODUCT_BUNDLE_IDENTIFIER = au.gov.health.covidsafe; @@ -1785,7 +1769,7 @@ CODE_SIGN_ENTITLEMENTS = "CovidSafe/Project Bluetrace.entitlements"; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 80; + CURRENT_PROJECT_VERSION = 83; DEVELOPMENT_TEAM = 45792XH5L8; INFOPLIST_FILE = "$(SRCROOT)/CovidSafe/Info.plist"; IPHONEOS_DEPLOYMENT_TARGET = 10.0; @@ -1793,7 +1777,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.13; + MARKETING_VERSION = 1.14; ONLY_ACTIVE_ARCH = YES; OTHER_SWIFT_FLAGS = "$(inherited) -D COCOAPODS -D DEBUG"; PRODUCT_BUNDLE_IDENTIFIER = au.gov.health.covidsafe; @@ -1815,7 +1799,7 @@ CODE_SIGN_ENTITLEMENTS = "CovidSafe/Project Bluetrace.entitlements"; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 80; + CURRENT_PROJECT_VERSION = 83; DEVELOPMENT_TEAM = 45792XH5L8; INFOPLIST_FILE = "$(SRCROOT)/CovidSafe/Info.plist"; IPHONEOS_DEPLOYMENT_TARGET = 10.0; @@ -1823,7 +1807,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.13; + MARKETING_VERSION = 1.14; OTHER_SWIFT_FLAGS = "$(inherited) -D COCOAPODS -D DEBUG"; PRODUCT_BUNDLE_IDENTIFIER = au.gov.health.covidsafe; PRODUCT_MODULE_NAME = COVIDSafe; @@ -1845,7 +1829,7 @@ CODE_SIGN_ENTITLEMENTS = "CovidSafe/Project Bluetrace.entitlements"; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 80; + CURRENT_PROJECT_VERSION = 83; DEVELOPMENT_TEAM = 45792XH5L8; INFOPLIST_FILE = "$(SRCROOT)/CovidSafe/Info.plist"; IPHONEOS_DEPLOYMENT_TARGET = 10.0; @@ -1853,7 +1837,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.13; + MARKETING_VERSION = 1.14; OTHER_SWIFT_FLAGS = "$(inherited) -D COCOAPODS -D DEBUG"; PRODUCT_BUNDLE_IDENTIFIER = au.gov.health.covidsafe; PRODUCT_MODULE_NAME = COVIDSafe; @@ -1993,7 +1977,7 @@ CODE_SIGN_ENTITLEMENTS = "CovidSafe/Project Bluetrace.entitlements"; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 80; + CURRENT_PROJECT_VERSION = 83; DEVELOPMENT_TEAM = 45792XH5L8; INFOPLIST_FILE = "$(SRCROOT)/CovidSafe/Info.plist"; IPHONEOS_DEPLOYMENT_TARGET = 10.0; @@ -2001,7 +1985,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.13; + MARKETING_VERSION = 1.14; ONLY_ACTIVE_ARCH = YES; PRODUCT_BUNDLE_IDENTIFIER = au.gov.health.covidsafe; PRODUCT_NAME = COVIDSafe; @@ -2021,7 +2005,7 @@ CODE_SIGN_ENTITLEMENTS = "CovidSafe/Project Bluetrace.entitlements"; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 80; + CURRENT_PROJECT_VERSION = 83; DEVELOPMENT_TEAM = 45792XH5L8; INFOPLIST_FILE = "$(SRCROOT)/CovidSafe/Info.plist"; IPHONEOS_DEPLOYMENT_TARGET = 10.0; @@ -2029,7 +2013,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.13; + MARKETING_VERSION = 1.14; PRODUCT_BUNDLE_IDENTIFIER = au.gov.health.covidsafe; PRODUCT_NAME = COVIDSafe; PROVISIONING_PROFILE_SPECIFIER = ""; diff --git a/CovidSafe/API/CovidNetworking.swift b/CovidSafe/API/CovidNetworking.swift index bf34088..06c760d 100644 --- a/CovidSafe/API/CovidNetworking.swift +++ b/CovidSafe/API/CovidNetworking.swift @@ -7,6 +7,7 @@ import Foundation import Alamofire +import KeychainSwift final class CovidServerTrustManager: ServerTrustManager { override func serverTrustEvaluator(forHost host: String) throws -> ServerTrustEvaluating? { @@ -45,3 +46,44 @@ enum APIError: Error { case ExpireSession case ServerError } + +struct CovidSafeErrorResponse: Decodable { + let message: String? +} + +enum CovidSafeAPIError: Error { + case RequestError + case ResponseError + case ServerError + case TokenExpiredError + case UnknownError +} + +class CovidSafeAuthenticatedAPI { + + static func authenticatedHeaders() throws -> HTTPHeaders? { + let keychain = KeychainSwift() + + guard let token = keychain.get("JWT_TOKEN") else { + throw CovidSafeAPIError.TokenExpiredError + } + let headers: HTTPHeaders = [ + "Authorization": "Bearer \(token)" + ] + return headers + } + + static func processUnauthorizedError(_ data: Data) -> CovidSafeAPIError { + var errorType = CovidSafeAPIError.RequestError + do { + let errorResponse = try JSONDecoder().decode(CovidSafeErrorResponse.self, from: data) + if errorResponse.message == "Unauthorized" { + errorType = .TokenExpiredError + } + } catch { + // unable to parse response + errorType = .ResponseError + } + return errorType + } +} diff --git a/CovidSafe/API/GetTempIdAPI.swift b/CovidSafe/API/GetTempIdAPI.swift index 4c2fb92..c058abe 100644 --- a/CovidSafe/API/GetTempIdAPI.swift +++ b/CovidSafe/API/GetTempIdAPI.swift @@ -7,25 +7,20 @@ import Foundation import Alamofire -import KeychainSwift -class GetTempIdAPI { +class GetTempIdAPI: CovidSafeAuthenticatedAPI { private static let apiVersion = 2 - static func getTempId(completion: @escaping (String?, Int?, Swift.Error?) -> Void) { - let keychain = KeychainSwift() + static func getTempId(completion: @escaping (String?, Int?, Swift.Error?, CovidSafeAPIError?) -> Void) { guard let apiHost = PlistHelper.getvalueFromInfoPlist(withKey: "API_Host", plistName: "CovidSafe-config") else { return } - guard let token = keychain.get("JWT_TOKEN") else { - completion(nil, nil, nil) + guard let headers = try? authenticatedHeaders() else { + completion(nil, nil, nil, .TokenExpiredError) return } - let headers: HTTPHeaders = [ - "Authorization": "Bearer \(token)" - ] let params = [ "version" : apiVersion ] @@ -37,9 +32,17 @@ class GetTempIdAPI { switch response.result { case .success: guard let tempIdResponse = response.value else { return } - completion(tempIdResponse.tempId, tempIdResponse.expiryTime, nil) + completion(tempIdResponse.tempId, tempIdResponse.expiryTime, nil, nil) case let .failure(error): - completion(nil, nil, error) + guard let statusCode = response.response?.statusCode else { + completion(nil, nil, error, .UnknownError) + return + } + if statusCode == 401, let respData = response.data { + completion(nil, nil, error, processUnauthorizedError(respData)) + return + } + completion(nil, nil, error, .ServerError) } } } diff --git a/CovidSafe/API/MessageAPI.swift b/CovidSafe/API/MessageAPI.swift index 8b813ad..cd0974d 100644 --- a/CovidSafe/API/MessageAPI.swift +++ b/CovidSafe/API/MessageAPI.swift @@ -7,20 +7,19 @@ import Foundation import Alamofire -import KeychainSwift -class MessageAPI { +class MessageAPI: CovidSafeAuthenticatedAPI { static let keyLastApiUpdate = "keyLastApiUpdate" static let keyLastVersionChecked = "keyLastVersionChecked" - static func getMessagesIfNeeded(completion: @escaping (MessageResponse?, MessageAPIError?) -> Void) { + static func getMessagesIfNeeded(completion: @escaping (MessageResponse?, CovidSafeAPIError?) -> Void) { if shouldGetMessages() { getMessages(completion: completion) } } - static func getMessages(completion: @escaping (MessageResponse?, MessageAPIError?) -> Void) { + static func getMessages(completion: @escaping (MessageResponse?, CovidSafeAPIError?) -> Void) { guard let token = UserDefaults.standard.string(forKey: "deviceTokenForAPN") else { completion(nil, .RequestError) return @@ -82,20 +81,16 @@ class MessageAPI { } private static func getMessages(msgRequest: MessageRequest, - completion: @escaping (MessageResponse?, MessageAPIError?) -> Void) { - let keychain = KeychainSwift() + completion: @escaping (MessageResponse?, CovidSafeAPIError?) -> Void) { guard let apiHost = PlistHelper.getvalueFromInfoPlist(withKey: "API_Host", plistName: "CovidSafe-config") else { completion(nil, .RequestError) return } - guard let token = keychain.get("JWT_TOKEN") else { - completion(nil, .RequestError) + guard let headers = try? authenticatedHeaders() else { + completion(nil, .TokenExpiredError) return } - let headers: HTTPHeaders = [ - "Authorization": "Bearer \(token)" - ] let preferredLanguages = Locale.preferredLanguages.count > 5 ? Locale.preferredLanguages[0...5].joined(separator: ",") : Locale.preferredLanguages.joined(separator: ",") @@ -138,11 +133,19 @@ class MessageAPI { completion(nil, .UnknownError) return } + if (statusCode == 200) { completion(nil, .ResponseError) + return + } + + if statusCode == 401, let respData = response.data { + completion(nil, processUnauthorizedError(respData)) + return } if (statusCode >= 400 && statusCode < 500) { completion(nil, .RequestError) + return } completion(nil, .ServerError) } @@ -183,10 +186,3 @@ struct Message: Decodable { case destination } } - -enum MessageAPIError: Error { - case RequestError - case ResponseError - case ServerError - case UnknownError -} diff --git a/CovidSafe/API/PhoneValidationAPI.swift b/CovidSafe/API/PhoneValidationAPI.swift index 6d0b376..97a45da 100644 --- a/CovidSafe/API/PhoneValidationAPI.swift +++ b/CovidSafe/API/PhoneValidationAPI.swift @@ -62,4 +62,5 @@ struct AuthResponse: Decodable { protocol RegistrationHandler { var registrationInfo: RegistrationRequest? { get set } + var reauthenticating: Bool { get set } } diff --git a/CovidSafe/API/StatisticsAPI.swift b/CovidSafe/API/StatisticsAPI.swift index e89084e..57f3bf5 100644 --- a/CovidSafe/API/StatisticsAPI.swift +++ b/CovidSafe/API/StatisticsAPI.swift @@ -7,26 +7,21 @@ import Foundation import Alamofire -import KeychainSwift -class StatisticsAPI { +class StatisticsAPI: CovidSafeAuthenticatedAPI { static let keyCovidStatistics = "keyCovidStatistics" - static func getStatistics(completion: @escaping (StatisticsResponse?, MessageAPIError?) -> Void) { - let keychain = KeychainSwift() + static func getStatistics(completion: @escaping (StatisticsResponse?, CovidSafeAPIError?) -> Void) { guard let apiHost = PlistHelper.getvalueFromInfoPlist(withKey: "API_Host", plistName: "CovidSafe-config") else { completion(nil, .RequestError) return } - guard let token = keychain.get("JWT_TOKEN") else { - completion(nil, .RequestError) + guard let headers = try? authenticatedHeaders() else { + completion(nil, .TokenExpiredError) return } - let headers: HTTPHeaders = [ - "Authorization": "Bearer \(token)" - ] CovidNetworking.shared.session.request("\(apiHost)/statistics", method: .get, @@ -50,9 +45,17 @@ class StatisticsAPI { } if (statusCode == 200) { completion(lastStats, .ResponseError) + return } + + if statusCode == 401, let respData = response.data { + completion(nil, processUnauthorizedError(respData)) + return + } + if (statusCode >= 400 && statusCode < 500) { completion(lastStats, .RequestError) + return } completion(lastStats, .ServerError) } diff --git a/CovidSafe/AppDelegate.swift b/CovidSafe/AppDelegate.swift index dba7447..e5b39e5 100644 --- a/CovidSafe/AppDelegate.swift +++ b/CovidSafe/AppDelegate.swift @@ -72,17 +72,15 @@ class AppDelegate: UIResponder, UIApplicationDelegate { @objc func jwtExpired(_ notification: Notification) { DispatchQueue.main.async { - guard let regVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "onboardingStep3") as? PhoneNumberViewController else { + guard let regVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "personalDetails") as? PersonalDetailsViewController else { return } regVC.reauthenticating = true - regVC.modalPresentationStyle = .overFullScreen - regVC.modalTransitionStyle = .coverVertical let navigationController = UINavigationController(rootViewController: regVC) navigationController.setToolbarHidden(true, animated: false) - if #available(iOS 13.0, *) { - navigationController.isModalInPresentation = true - } + navigationController.isNavigationBarHidden = true + navigationController.modalPresentationStyle = .overFullScreen + navigationController.modalTransitionStyle = .coverVertical self.window?.topmostPresentedViewController?.present(navigationController, animated: true, completion: nil) } } diff --git a/CovidSafe/Base.lproj/Main.storyboard b/CovidSafe/Base.lproj/Main.storyboard index 34e51e8..2a6007f 100644 --- a/CovidSafe/Base.lproj/Main.storyboard +++ b/CovidSafe/Base.lproj/Main.storyboard @@ -37,9 +37,6 @@ - - -