Created
April 27, 2022 07:04
-
-
Save RobertApikyan/5c5073e567f1bb757df1833dc07d0b62 to your computer and use it in GitHub Desktop.
This file contains 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
// | |
// 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