Files
app-check/AppCheckCore/Sources/AppAttestProvider/API/AppCheckCoreAppAttestAttestationResponse.swift
T
2026-09-23 18:22:08 -04:00

71 lines
2.2 KiB
Swift

// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import Foundation
private let kResponseFieldAppCheckTokenDict = "appCheckToken"
private let kResponseFieldArtifact = "artifact"
class AppCheckCoreAppAttestAttestationResponse: NSObject {
let artifact: Data
let token: AppCheckCoreToken
init(artifact: Data, token: AppCheckCoreToken) {
self.artifact = artifact
self.token = token
super.init()
}
init(responseData: Data, requestDate: Date) throws {
if responseData.isEmpty {
throw AppCheckCoreErrorUtil
.error(
withFailureReason: "Failed to parse the initial handshake response. Empty server response body."
)
}
let responseDict = try JSONSerialization
.jsonObject(with: responseData, options: []) as? [String: Any]
guard let responseDict = responseDict else {
throw AppCheckCoreErrorUtil.jsonSerializationError(NSError(
domain: NSCocoaErrorDomain,
code: 0,
userInfo: nil
))
}
guard let artifactBase64String = responseDict[kResponseFieldArtifact] as? String,
let artifactData = Data(base64Encoded: artifactBase64String) else {
throw AppCheckCoreErrorUtil
.appAttestAttestationResponseError(withMissingField: kResponseFieldArtifact)
}
guard let appCheckTokenDict = responseDict[kResponseFieldAppCheckTokenDict] as? [String: Any]
else {
throw AppCheckCoreErrorUtil
.appAttestAttestationResponseError(withMissingField: kResponseFieldAppCheckTokenDict)
}
let appCheckToken = try AppCheckCoreToken(
responseDict: appCheckTokenDict,
requestDate: requestDate
)
artifact = artifactData
token = appCheckToken
super.init()
}
}