2020-06-30 19:46:10 +00:00
|
|
|
/*
|
|
|
|
Copyright 2020 New Vector Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
2021-02-05 17:31:34 +00:00
|
|
|
enum CrossSigningServiceError: Int, Error {
|
|
|
|
case authenticationRequired
|
|
|
|
case unknown
|
|
|
|
}
|
|
|
|
|
|
|
|
extension CrossSigningServiceError: CustomNSError {
|
|
|
|
public static let errorDomain = "CrossSigningService"
|
|
|
|
|
|
|
|
public var errorCode: Int {
|
|
|
|
return Int(rawValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
public var errorUserInfo: [String: Any] {
|
|
|
|
return [:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-30 19:46:10 +00:00
|
|
|
@objcMembers
|
|
|
|
final class CrossSigningService: NSObject {
|
|
|
|
|
2021-02-03 10:11:21 +00:00
|
|
|
// MARK - Properties
|
|
|
|
|
2020-06-30 19:46:10 +00:00
|
|
|
private var supportSetupKeyVerificationByUser: [String: Bool] = [:] // Cached server response
|
2021-02-10 13:57:00 +00:00
|
|
|
private var userInteractiveAuthenticationService: UserInteractiveAuthenticationService?
|
2021-02-03 10:11:21 +00:00
|
|
|
|
|
|
|
// MARK - Public
|
2020-06-30 19:46:10 +00:00
|
|
|
|
|
|
|
@discardableResult
|
|
|
|
func canSetupCrossSigning(for session: MXSession, success: @escaping ((Bool) -> Void), failure: @escaping ((Error) -> Void)) -> MXHTTPOperation? {
|
|
|
|
|
|
|
|
guard let crossSigning = session.crypto?.crossSigning, crossSigning.state == .notBootstrapped else {
|
|
|
|
// Cross-signing already setup
|
|
|
|
success(false)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let userId: String = session.myUserId
|
|
|
|
|
|
|
|
if let supportSetupKeyVerification = self.supportSetupKeyVerificationByUser[userId] {
|
|
|
|
// Return cached response
|
|
|
|
success(supportSetupKeyVerification)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-10 13:57:00 +00:00
|
|
|
let userInteractiveAuthenticationService = UserInteractiveAuthenticationService(session: session)
|
2020-06-30 19:46:10 +00:00
|
|
|
|
2021-02-10 13:57:00 +00:00
|
|
|
self.userInteractiveAuthenticationService = userInteractiveAuthenticationService
|
2021-02-03 10:11:21 +00:00
|
|
|
|
2021-02-10 13:57:00 +00:00
|
|
|
let request = self.setupCrossSigningRequest()
|
2020-06-30 19:46:10 +00:00
|
|
|
|
2021-02-10 13:57:00 +00:00
|
|
|
return userInteractiveAuthenticationService.canAuthenticate(with: request) { (result) in
|
2021-02-03 10:11:21 +00:00
|
|
|
switch result {
|
|
|
|
case .success(let succeeded):
|
|
|
|
success(succeeded)
|
|
|
|
case .failure(let error):
|
2020-06-30 19:46:10 +00:00
|
|
|
failure(error)
|
2021-02-03 10:11:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-10 13:57:00 +00:00
|
|
|
func setupCrossSigningRequest() -> AuthenticatedEndpointRequest {
|
2021-02-03 10:11:21 +00:00
|
|
|
let path = "\(kMXAPIPrefixPathUnstable)/keys/device_signing/upload"
|
2021-02-10 13:57:00 +00:00
|
|
|
return AuthenticatedEndpointRequest(path: path, httpMethod: "POST")
|
2020-06-30 19:46:10 +00:00
|
|
|
}
|
2021-02-05 17:31:34 +00:00
|
|
|
|
|
|
|
/// Setup cross-signing without authentication. Useful when a grace period is enabled.
|
|
|
|
@discardableResult
|
|
|
|
func setupCrossSigningWithoutAuthentication(for session: MXSession, success: @escaping (() -> Void), failure: @escaping ((Error) -> Void)) -> MXHTTPOperation? {
|
|
|
|
|
|
|
|
guard let crossSigning = session.crypto.crossSigning else {
|
|
|
|
failure(CrossSigningServiceError.unknown)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-10 13:57:00 +00:00
|
|
|
let userInteractiveAuthenticationService = UserInteractiveAuthenticationService(session: session)
|
|
|
|
self.userInteractiveAuthenticationService = userInteractiveAuthenticationService
|
2021-02-05 17:31:34 +00:00
|
|
|
|
2021-02-10 13:57:00 +00:00
|
|
|
let request = self.setupCrossSigningRequest()
|
2021-02-05 17:31:34 +00:00
|
|
|
|
2021-02-10 13:57:00 +00:00
|
|
|
return userInteractiveAuthenticationService.authenticatedEndpointStatus(for: request) { result in
|
2021-02-05 17:31:34 +00:00
|
|
|
switch result {
|
2021-02-10 13:57:00 +00:00
|
|
|
case .success(let authenticatedEnpointStatus):
|
|
|
|
switch authenticatedEnpointStatus {
|
2021-02-05 17:31:34 +00:00
|
|
|
case .authenticationNeeded:
|
|
|
|
failure(CrossSigningServiceError.authenticationRequired)
|
|
|
|
case .authenticationNotNeeded:
|
|
|
|
crossSigning.setup(withAuthParams: [:]) {
|
|
|
|
success()
|
|
|
|
} failure: { error in
|
|
|
|
failure(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case .failure(let error):
|
|
|
|
failure(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-30 19:46:10 +00:00
|
|
|
}
|