Skip to content

Instantly share code, notes, and snippets.

@schirrmacher
Last active March 9, 2020 13:25
Show Gist options
  • Save schirrmacher/eeee5919d6023347161595bec3d9dbc8 to your computer and use it in GitHub Desktop.
Save schirrmacher/eeee5919d6023347161595bec3d9dbc8 to your computer and use it in GitHub Desktop.
DeviceCheck Example Implementation for Validating Device Authenticity on iOS
import Foundation
import RxSwift
import DeviceCheck
public struct Client {
private static func retrieveDeviceToken() -> Observable<String?> {
if #available(iOS 11.0, *) {
return Observable.create({ observer -> Disposable in
guard DCDevice.current.isSupported else {
observer.onNext(nil)
observer.onCompleted()
return Disposables.create()
}
DCDevice.current.generateToken { (data, error) in
if error != nil {
observer.onNext(nil)
observer.onCompleted()
}
guard let data = data else {
observer.onNext(nil)
observer.onCompleted()
return
}
observer.onNext(data.base64EncodedString())
observer.onCompleted()
}
return Disposables.create()
})
}
return Observable.just(nil)
}
public static func executeSomeRequest() -> Observable<ResponseModel> {
return retrieveDeviceToken().flatMap { deviceToken -> Observable<ResponseModel> in
return createRequest(with: deviceToken)
}
}
public static func createRequest(with deviceToken: String?) -> Observable<ResponseModel> {
// to be implemented
// e.g. set token as header such as "Device-Check-Token"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment