Move Context to ConnectionProfile

Fix an id conflict in credentials.
This commit is contained in:
Davide De Rosa 2018-10-26 18:36:41 +02:00
parent 18c7de140e
commit b5347e04b2
5 changed files with 19 additions and 21 deletions

View File

@ -409,7 +409,7 @@ extension OrganizerViewController {
private func profile(at indexPath: IndexPath) -> ConnectionProfile { private func profile(at indexPath: IndexPath) -> ConnectionProfile {
let id = sectionProfiles(at: indexPath)[indexPath.row] let id = sectionProfiles(at: indexPath)[indexPath.row]
let section = model.section(for: indexPath.section) let section = model.section(for: indexPath.section)
let context: ConnectionService.ProfileKey.Context let context: Context
switch section { switch section {
case .providers: case .providers:
context = .provider context = .provider

View File

@ -27,7 +27,15 @@ import Foundation
import TunnelKit import TunnelKit
import NetworkExtension import NetworkExtension
enum Context: String, Codable {
case provider
case host
}
protocol ConnectionProfile: class, EndpointDataSource { protocol ConnectionProfile: class, EndpointDataSource {
var context: Context { get }
var id: String { get } var id: String { get }
var username: String? { get set } var username: String? { get set }
@ -42,7 +50,7 @@ extension ConnectionProfile {
guard let username = username else { guard let username = username else {
return nil return nil
} }
return "\(Bundle.main.bundleIdentifier!).\(id).\(username)" return "\(Bundle.main.bundleIdentifier!).\(context.rawValue).\(id).\(username)"
} }
func password(in keychain: Keychain) -> String? { func password(in keychain: Keychain) -> String? {

View File

@ -50,12 +50,6 @@ class ConnectionService: Codable {
} }
struct ProfileKey: RawRepresentable, Hashable, Codable { struct ProfileKey: RawRepresentable, Hashable, Codable {
enum Context: String {
case provider
case host
}
let context: Context let context: Context
let id: String let id: String
@ -66,15 +60,7 @@ class ConnectionService: Codable {
} }
init(_ profile: ConnectionProfile) { init(_ profile: ConnectionProfile) {
if let _ = profile as? ProviderConnectionProfile { context = profile.context
context = .provider
} else if let _ = profile as? HostConnectionProfile {
context = .host
} else if let placeholder = profile as? PlaceholderConnectionProfile {
context = placeholder.context
} else {
fatalError("Unexpected profile type: \(type(of: profile))")
}
id = profile.id id = profile.id
} }
@ -288,7 +274,7 @@ class ConnectionService: Codable {
} }
} }
func profile(withContext context: ProfileKey.Context, id: String) -> ConnectionProfile? { func profile(withContext context: Context, id: String) -> ConnectionProfile? {
let key = ProfileKey(context, id) let key = ProfileKey(context, id)
var profile = cache[key] var profile = cache[key]
if let _ = profile as? PlaceholderConnectionProfile { if let _ = profile as? PlaceholderConnectionProfile {
@ -311,7 +297,7 @@ class ConnectionService: Codable {
return profile return profile
} }
func ids(forContext context: ProfileKey.Context) -> [String] { func ids(forContext context: Context) -> [String] {
return cache.keys.filter { $0.context == context }.map { $0.id } return cache.keys.filter { $0.context == context }.map { $0.id }
} }
@ -485,6 +471,8 @@ class ConnectionService: Codable {
} }
private class PlaceholderConnectionProfile: ConnectionProfile { private class PlaceholderConnectionProfile: ConnectionProfile {
let context: Context
let id: String let id: String
var username: String? var username: String?
@ -507,8 +495,6 @@ private class PlaceholderConnectionProfile: ConnectionProfile {
var customProtocol: TunnelKitProvider.EndpointProtocol? var customProtocol: TunnelKitProvider.EndpointProtocol?
let context: ConnectionService.ProfileKey.Context
init(_ key: ConnectionService.ProfileKey) { init(_ key: ConnectionService.ProfileKey) {
self.context = key.context self.context = key.context
self.id = key.id self.id = key.id

View File

@ -42,6 +42,8 @@ class HostConnectionProfile: ConnectionProfile, Codable, Equatable {
// MARK: ConnectionProfile // MARK: ConnectionProfile
let context: Context = .host
var id: String { var id: String {
return title return title
} }

View File

@ -92,6 +92,8 @@ class ProviderConnectionProfile: ConnectionProfile, Codable, Equatable {
// MARK: ConnectionProfile // MARK: ConnectionProfile
let context: Context = .provider
var id: String { var id: String {
return name.rawValue return name.rawValue
} }