35 lines
947 B
Swift
35 lines
947 B
Swift
//
|
|
// DeviceToken.swift
|
|
//
|
|
//
|
|
// Created by NUT.Tech on 02.08.2022.
|
|
//
|
|
|
|
import Foundation
|
|
import DeviceCheck
|
|
import UIKit
|
|
|
|
public enum DeviceCheckTokenError: Error {
|
|
case notSupported
|
|
case generation
|
|
}
|
|
|
|
public struct DeviceCheckToken {
|
|
|
|
public let token: String
|
|
public let isSimulator: Bool
|
|
|
|
public static func generate() async -> Result<DeviceCheckToken, DeviceCheckTokenError> {
|
|
#if targetEnvironment(simulator)
|
|
return .success(DeviceCheckToken(token: "QWTRY", isSimulator: true))
|
|
#else
|
|
let device = DCDevice.current
|
|
guard device.isSupported else { return .failure(.notSupported) }
|
|
guard let data = try? await device.generateToken() else { return .failure(.generation) }
|
|
|
|
UIPasteboard.general.string = data.base64EncodedString()
|
|
return .success(DeviceCheckToken(token: data.base64EncodedString(), isSimulator: false))
|
|
#endif
|
|
}
|
|
}
|