mirror of
https://github.com/AU-COVIDSafe/mobile-ios.git
synced 2025-04-19 13:05:21 +00:00
COVIDSafe code from version 1.14 (#27)
This commit is contained in:
parent
3ea83834f5
commit
cf93ea43c0
29 changed files with 370 additions and 141 deletions
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue