Last active
January 10, 2025 02:27
-
-
Save KunalKumarSwift/9c829856f5d0c5419c72bddd7ac466f2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import DeviceCheck | |
import CryptoKit | |
class AppAttestationService { | |
private let dcAppAttestService = DCAppAttestService.shared | |
// Update to your local machine's IP address - don't use localhost since iOS simulator/device | |
// needs to reach your computer's actual IP | |
private let serverURL = URL(string: "http://192.168.1.X:3000/verify-attestation")! | |
func performAttestation() async throws { | |
// For development, we'll add additional logging | |
print("Starting attestation process...") | |
guard dcAppAttestService.isSupported else { | |
print("⚠️ App attestation not supported on this device") | |
throw NSError(domain: "AppAttestation", code: 1, | |
userInfo: [NSLocalizedDescriptionKey: "App attestation not supported"]) | |
} | |
print("Generating new key pair...") | |
let keyId = try await dcAppAttestService.generateKey() | |
print("Generated keyId: \(keyId)") | |
print("Generating attestation...") | |
let attestationData = try await dcAppAttestService.attestKey(keyId) | |
print("Attestation data generated successfully") | |
// Create request body | |
let requestBody = AttestationRequest( | |
keyId: keyId, | |
attestationData: attestationData.base64EncodedString() | |
) | |
print("Sending attestation to local server...") | |
// Allow local HTTP connections by adding an exception in Info.plist | |
var request = URLRequest(url: serverURL) | |
request.httpMethod = "POST" | |
request.setValue("application/json", forHTTPHeaderField: "Content-Type") | |
request.httpBody = try JSONEncoder().encode(requestBody) | |
let (data, response) = try await URLSession.shared.data(for: request) | |
guard let httpResponse = response as? HTTPURLResponse else { | |
throw NSError(domain: "AppAttestation", code: 2, | |
userInfo: [NSLocalizedDescriptionKey: "Invalid server response"]) | |
} | |
print("Server response status code: \(httpResponse.statusCode)") | |
guard httpResponse.statusCode == 200 else { | |
throw NSError(domain: "AppAttestation", code: 3, | |
userInfo: [NSLocalizedDescriptionKey: "Server verification failed"]) | |
} | |
// Store keyId for future assertions | |
UserDefaults.standard.set(keyId, forKey: "attestationKeyId") | |
print("Attestation process completed successfully!") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment