passepartout-apple/Passepartout/Sources/Model/ConnectionProfile.swift

89 lines
2.5 KiB
Swift
Raw Normal View History

2018-10-11 07:13:19 +00:00
//
// ConnectionProfile.swift
// Passepartout
//
// Created by Davide De Rosa on 9/2/18.
2019-03-09 10:44:44 +00:00
// Copyright (c) 2019 Davide De Rosa. All rights reserved.
2018-10-11 07:13:19 +00:00
//
2018-11-03 21:33:30 +00:00
// https://github.com/passepartoutvpn
2018-10-11 07:13:19 +00:00
//
// This file is part of Passepartout.
//
// Passepartout is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Passepartout is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
//
import Foundation
import TunnelKit
import NetworkExtension
public enum Context: String, Codable {
case provider
case host
}
public protocol ConnectionProfile: class, EndpointDataSource, CustomStringConvertible {
var context: Context { get }
2018-10-11 07:13:19 +00:00
var id: String { get }
var username: String? { get set }
var requiresCredentials: Bool { get }
2018-10-11 07:13:19 +00:00
func generate(from configuration: TunnelKitProvider.Configuration, preferences: Preferences) throws -> TunnelKitProvider.Configuration
2018-11-02 13:44:29 +00:00
func with(newId: String) -> ConnectionProfile
2018-10-11 07:13:19 +00:00
}
public extension ConnectionProfile {
2018-10-11 07:13:19 +00:00
var passwordKey: String? {
guard let username = username else {
return nil
}
return "\(Bundle.main.bundleIdentifier!).\(context.rawValue).\(id).\(username)"
2018-10-11 07:13:19 +00:00
}
func password(in keychain: Keychain) -> String? {
guard let key = passwordKey else {
return nil
}
return try? keychain.password(for: key)
}
func setPassword(_ password: String?, in keychain: Keychain) throws {
guard let key = passwordKey else {
return
}
guard let password = password else {
keychain.removePassword(for: key)
return
}
try keychain.set(password: password, for: key, label: key)
}
func removePassword(in keychain: Keychain) {
guard let key = passwordKey else {
return
}
keychain.removePassword(for: key)
}
2018-10-11 07:13:19 +00:00
}
public extension ConnectionProfile {
var description: String {
2019-01-04 13:19:56 +00:00
return "(\(context):\(id))"
}
}