Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//
// DeviceCheckPlugin.swift
// Runner
//
// Created by Avo Sukiasyan on 26.12.21.
//
import UIKit
import DeviceCheck
import Flutter
import CryptoKit
class DeviceCheckPlugin {
let curDevice = DCDevice.current
var token = ""
var channelMethod:FlutterMethodChannel?
init(channelMethod: FlutterMethodChannel) {
self.channelMethod = channelMethod
self.setup()
}
func setup() {
channelMethod?.setMethodCallHandler { call, result in
switch call.method {
case "attest":
let args = call.arguments as? [String: Any]
if #available(iOS 14, *) {
let service = DCAppAttestService.shared
service.generateKey { (keyIdentifier, error) in
guard error == nil else {
result(nil)
return
}
let legitimationId = args?["nonce"] as? String ?? ""
let challenge = legitimationId.data(using: .utf8)!
let hash = Data(SHA256.hash(data: challenge))
service.attestKey(keyIdentifier!, clientDataHash: hash) { attestation, error in
guard error == nil else {
result(nil)
return
}
let attestationString = attestation?.base64EncodedString()
result(["attestation":attestationString])
}
}
} else {
self.curDevice.generateToken { data, error in
if let data = data {
self.token = data.base64EncodedString(options: [])
result(["attestation":self.token])
}
}
}
case "isSupported":
result(["isSupported":self.curDevice.isSupported])
default:
break
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment