COVIDSafe code from version 1.14 (#27)

This commit is contained in:
COVIDSafe Support 2020-11-09 16:51:00 -08:00 committed by GitHub
parent 3ea83834f5
commit cf93ea43c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
29 changed files with 370 additions and 141 deletions

View file

@ -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
}
}