Files
bundesmessenger-ios/Riot/Model/HomeserverConfiguration/HomeserverConfigurationBuilder.swift
JanNiklas Grabowski ccdbd400d2 Merge commit 'f6b85b8f9a0b4ce162616e79045fb015a21b27da' into feature/5004_basis_update_element
* commit 'f6b85b8f9a0b4ce162616e79045fb015a21b27da': (40 commits)
  finish version++
  version++
  changelog.d: Upgrade MatrixSDK version ([v0.27.1](https://github.com/matrix-org/matrix-ios-sdk/releases/tag/v0.27.1)).
  completed
  code improvement
  fix 7646
  opening the safari web view externally so that it will be able to share the cookies
  web view opened on tap + changelog
  added the cell, now I just need to implement the navigation to the web view
  completed
  Hide deactivate account if the auth property is present on the WK.
  Add changelogs
  Prevent mention crashes when room members are missing display names (objc interop)
  Prevent pill crashes when room members are missing display names (objc interop)
  Update introspect to the latest version, remove now duplicate `introspectCollectionView`
  Prepare for new sprint
  finish version++
  Add missing changelog entry.
  version++
  changelog.d: Upgrade MatrixSDK version ([v0.27.0](https://github.com/matrix-org/matrix-ios-sdk/releases/tag/v0.27.0)).
  ...

# Conflicts:
#	Config/AppVersion.xcconfig
#	Podfile
#	Riot.xcodeproj/xcshareddata/xcschemes/Riot.xcscheme
#	Riot/Modules/SecureBackup/Setup/SecureBackupSetupCoordinator.swift
#	Riot/Modules/Settings/SettingsViewController.m
#	Riot/target.yml
2023-08-29 17:00:19 +02:00

157 lines
7.6 KiB
Swift

//
// 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
/// `HomeserverConfigurationBuilder` build `HomeserverConfiguration` objects according to injected inputs
@objcMembers
final class HomeserverConfigurationBuilder: NSObject {
// MARK: - Properties
private let vectorWellKnownParser = VectorWellKnownParser()
// MARK: - Public
/// Create an `HomeserverConfiguration` from an HS Well-Known when possible otherwise it takes hardcoded values from BuildSettings by default.
func build(from wellKnown: MXWellKnown?) -> HomeserverConfiguration {
var vectorWellKnownEncryptionConfiguration: VectorWellKnownEncryptionConfiguration?
var vectorWellKnownJitsiConfiguration: VectorWellKnownJitsiConfiguration?
if let wellKnown = wellKnown, let vectorWellKnown = self.vectorWellKnownParser.parse(jsonDictionary: wellKnown.jsonDictionary()) {
vectorWellKnownEncryptionConfiguration = self.getEncryptionConfiguration(from: vectorWellKnown)
vectorWellKnownJitsiConfiguration = self.getJitsiConfiguration(from: vectorWellKnown)
}
// bwi: (#4218) the well-known defaults for bwi are different than the element defaults. E2EE decryption is true instead of false, presharingmode is .whenEnteringRoom instead of .whenTyping and our backup method is only .passphrase instead of all
// Encryption configuration
// Enable E2EE by default when there is no value
let isE2EEByDefaultEnabled = vectorWellKnownEncryptionConfiguration?.isE2EEByDefaultEnabled ?? true
// Disable mandatory secure backup when there is no value
let isSecureBackupRequired = vectorWellKnownEncryptionConfiguration?.isSecureBackupRequired ?? true
// Default to `MXKKeyPreSharingWhenTyping` when there is no value
let outboundKeysPreSharingMode = vectorWellKnownEncryptionConfiguration?.outboundKeysPreSharingMode ?? .whenEnteringRoom
// Defaults to all secure backup methods available when there is no value
let secureBackupSetupMethods: [VectorWellKnownBackupSetupMethod]
if let backupSetupMethods = vectorWellKnownEncryptionConfiguration?.secureBackupSetupMethods {
secureBackupSetupMethods = backupSetupMethods.isEmpty ? [.passphrase] : backupSetupMethods
} else {
secureBackupSetupMethods = [.passphrase]
}
let deviceDehydrationEnabled = wellKnown?.jsonDictionary()["org.matrix.msc3814"] as? Bool == true
let encryptionConfiguration = HomeserverEncryptionConfiguration(isE2EEByDefaultEnabled: isE2EEByDefaultEnabled,
isSecureBackupRequired: isSecureBackupRequired,
secureBackupSetupMethods: secureBackupSetupMethods,
outboundKeysPreSharingMode: outboundKeysPreSharingMode,
deviceDehydrationEnabled: deviceDehydrationEnabled)
// Jitsi configuration
let jitsiPreferredDomain: String?
let jitsiServerURL: URL?
let hardcodedJitsiServerURL: URL? = BWIBuildSettings.shared.jitsiServerUrl
if let preferredDomain = vectorWellKnownJitsiConfiguration?.preferredDomain {
jitsiPreferredDomain = preferredDomain
jitsiServerURL = self.jitsiServerURL(from: preferredDomain) ?? hardcodedJitsiServerURL
} else {
jitsiPreferredDomain = hardcodedJitsiServerURL?.host
jitsiServerURL = hardcodedJitsiServerURL
}
// Tile server configuration
var tileServerMapStyleURL: URL = URL(string: BWIBuildSettings.shared.serverConfigDefaultMapstyleURLString)!
if let mapStyleURLString = wellKnown?.tileServer?.mapStyleURLString,
let mapStyleURL = URL(string: mapStyleURLString) {
if verifyUrl(url: mapStyleURL) {
tileServerMapStyleURL = mapStyleURL
}
}
let tileServerConfiguration = HomeserverTileServerConfiguration(mapStyleURL: tileServerMapStyleURL)
// Create HomeserverConfiguration
let jitsiConfiguration = HomeserverJitsiConfiguration(serverDomain: jitsiPreferredDomain,
serverURL: jitsiServerURL)
return HomeserverConfiguration(jitsi: jitsiConfiguration,
encryption: encryptionConfiguration,
tileServer: tileServerConfiguration)
}
// MARK: - Private
private func getJitsiConfiguration(from vectorWellKnown: VectorWellKnown) -> VectorWellKnownJitsiConfiguration? {
let jitsiConfiguration: VectorWellKnownJitsiConfiguration?
if let lastJitsiConfiguration = vectorWellKnown.jitsi {
jitsiConfiguration = lastJitsiConfiguration
} else if let deprecatedJitsiConfiguration = vectorWellKnown.deprecatedJitsi {
MXLog.debug("[HomeserverConfigurationBuilder] getJitsiConfiguration - Use deprecated configuration")
jitsiConfiguration = deprecatedJitsiConfiguration
} else {
MXLog.debug("[HomeserverConfigurationBuilder] getJitsiConfiguration - No configuration found")
jitsiConfiguration = nil
}
return jitsiConfiguration
}
private func getEncryptionConfiguration(from vectorWellKnown: VectorWellKnown) -> VectorWellKnownEncryptionConfiguration? {
let encryptionConfiguration: VectorWellKnownEncryptionConfiguration?
if let lastEncryptionConfiguration = vectorWellKnown.encryption {
encryptionConfiguration = lastEncryptionConfiguration
} else if let deprecatedEncryptionConfiguration = vectorWellKnown.deprecatedEncryption {
MXLog.debug("[HomeserverConfigurationBuilder] getEncryptionConfiguration - Use deprecated configuration")
encryptionConfiguration = deprecatedEncryptionConfiguration
} else {
MXLog.debug("[HomeserverConfigurationBuilder] getEncryptionConfiguration - No configuration found")
encryptionConfiguration = nil
}
return encryptionConfiguration
}
private func jitsiServerURL(from jitsiServerDomain: String) -> URL? {
let jitsiStringURL: String
if jitsiServerDomain.starts(with: "http") {
jitsiStringURL = jitsiServerDomain
} else {
jitsiStringURL = "https://\(jitsiServerDomain)"
}
guard let jitsiServerURL = URL(string: jitsiStringURL) else {
MXLog.debug("[HomeserverConfigurationBuilder] Jitsi server URL is not valid")
return nil
}
return jitsiServerURL
}
private func verifyUrl (url: URL) -> Bool {
return UIApplication.shared.canOpenURL(url)
}
}