2022-04-12 13:09:14 +00:00
|
|
|
//
|
|
|
|
// AccountView.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 2/11/22.
|
2023-03-17 15:56:19 +00:00
|
|
|
// Copyright (c) 2023 Davide De Rosa. All rights reserved.
|
2022-04-12 13:09:14 +00:00
|
|
|
//
|
|
|
|
// https://github.com/passepartoutvpn
|
|
|
|
//
|
|
|
|
// 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/>.
|
|
|
|
//
|
|
|
|
|
2022-06-23 21:31:01 +00:00
|
|
|
import PassepartoutLibrary
|
2023-04-04 07:50:45 +00:00
|
|
|
import SwiftUI
|
2022-04-12 13:09:14 +00:00
|
|
|
|
|
|
|
struct AccountView: View {
|
2022-08-28 07:19:15 +00:00
|
|
|
@ObservedObject private var providerManager: ProviderManager
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
private let providerName: ProviderName?
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
private let vpnProtocol: VPNProtocolType
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
@Binding private var account: Profile.Account
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
private let saveAnyway: Bool
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
private let onSave: (() -> Void)?
|
|
|
|
|
|
|
|
@State private var liveAccount = Profile.Account()
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
init(
|
|
|
|
providerName: ProviderName?,
|
|
|
|
vpnProtocol: VPNProtocolType,
|
|
|
|
account: Binding<Profile.Account>,
|
|
|
|
saveAnyway: Bool = false,
|
|
|
|
onSave: (() -> Void)? = nil
|
|
|
|
) {
|
|
|
|
providerManager = .shared
|
|
|
|
self.providerName = providerName
|
|
|
|
self.vpnProtocol = vpnProtocol
|
|
|
|
_account = account
|
|
|
|
self.saveAnyway = saveAnyway
|
|
|
|
self.onSave = onSave
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
var body: some View {
|
|
|
|
List {
|
2023-03-20 14:06:56 +00:00
|
|
|
// TODO: interactive, re-enable after fixing
|
|
|
|
// Section {
|
|
|
|
// // TODO: interactive, l10n
|
2023-04-04 15:07:41 +00:00
|
|
|
// themeTextPicker(L10n.Global.Strings.authentication, selection: $liveAccount.authenticationMethod ?? .persistent, values: [
|
2023-03-20 14:06:56 +00:00
|
|
|
// .persistent,
|
|
|
|
// .interactive
|
|
|
|
//// .totp // TODO: interactive, support OTP-based authentication
|
|
|
|
// ], description: \.localizedDescription)
|
|
|
|
// }
|
2022-05-01 17:43:33 +00:00
|
|
|
Section {
|
2022-04-12 13:09:14 +00:00
|
|
|
TextField(usernamePlaceholder ?? L10n.Account.Items.Username.placeholder, text: $liveAccount.username)
|
|
|
|
.textContentType(.username)
|
|
|
|
.keyboardType(.emailAddress)
|
2022-05-24 06:25:10 +00:00
|
|
|
.themeRawTextStyle()
|
2022-04-12 13:09:14 +00:00
|
|
|
.withLeadingText(L10n.Account.Items.Username.caption)
|
|
|
|
|
2023-03-17 15:49:35 +00:00
|
|
|
switch liveAccount.authenticationMethod {
|
|
|
|
case nil, .persistent, .interactive:
|
|
|
|
if liveAccount.authenticationMethod == .interactive {
|
|
|
|
EmptyView()
|
|
|
|
} else {
|
2023-03-20 10:00:01 +00:00
|
|
|
themeSecureField(L10n.Account.Items.Password.placeholder, text: $liveAccount.password)
|
2023-03-17 15:49:35 +00:00
|
|
|
.withLeadingText(L10n.Account.Items.Password.caption)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: interactive, scan QR code
|
|
|
|
case .totp:
|
2023-03-20 10:00:01 +00:00
|
|
|
themeSecureField(L10n.Account.Items.Password.placeholder, text: $liveAccount.password, contentType: .oneTimeCode)
|
2023-03-17 15:49:35 +00:00
|
|
|
.withLeadingText(L10n.Account.Items.Seed.caption)
|
|
|
|
}
|
2022-05-01 17:43:33 +00:00
|
|
|
} footer: {
|
|
|
|
metadata?.localizedGuidanceString.map {
|
|
|
|
Text($0)
|
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
if vpnProtocol == .openVPN {
|
|
|
|
metadata?.openVPNGuidanceURL.map { guidanceURL in
|
|
|
|
Section {
|
|
|
|
Button(L10n.Account.Items.OpenGuide.caption) {
|
|
|
|
openGuidanceURL(guidanceURL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.navigationTitle(L10n.Account.title)
|
|
|
|
.toolbar {
|
|
|
|
CopySavingButton(
|
|
|
|
original: $account,
|
|
|
|
copy: $liveAccount,
|
|
|
|
mapping: \.stripped,
|
|
|
|
label: themeSaveButtonLabel,
|
|
|
|
saveAnyway: saveAnyway,
|
|
|
|
onSave: onSave
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
private func openGuidanceURL(_ url: URL) {
|
2023-04-04 07:50:45 +00:00
|
|
|
URL.open(url)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Provider
|
|
|
|
|
|
|
|
extension AccountView {
|
|
|
|
private var usernamePlaceholder: String? {
|
|
|
|
guard let name = providerName else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return providerManager.defaultUsername(name, vpnProtocol: vpnProtocol)
|
|
|
|
}
|
|
|
|
|
|
|
|
private var metadata: ProviderMetadata? {
|
|
|
|
guard let name = providerName else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return providerManager.provider(withName: name)
|
|
|
|
}
|
|
|
|
}
|
2023-03-17 15:49:35 +00:00
|
|
|
|
|
|
|
private extension Profile.Account.AuthenticationMethod {
|
|
|
|
var localizedDescription: String {
|
|
|
|
switch self {
|
|
|
|
case .persistent:
|
|
|
|
return L10n.Account.Items.AuthenticationMethod.persistent
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-03-17 15:49:35 +00:00
|
|
|
case .interactive:
|
|
|
|
return L10n.Account.Items.AuthenticationMethod.interactive
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-03-17 15:49:35 +00:00
|
|
|
case .totp:
|
|
|
|
return Unlocalized.Other.totp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|