2024-09-23 13:02:26 +00:00
|
|
|
//
|
|
|
|
// OpenVPNView+Credentials.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 9/1/24.
|
|
|
|
// Copyright (c) 2024 Davide De Rosa. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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/>.
|
|
|
|
//
|
|
|
|
|
2024-11-03 12:16:13 +00:00
|
|
|
import CommonLibrary
|
2024-11-02 09:11:59 +00:00
|
|
|
import CommonUtils
|
2024-09-23 13:02:26 +00:00
|
|
|
import PassepartoutKit
|
|
|
|
import SwiftUI
|
|
|
|
|
2024-11-01 22:32:35 +00:00
|
|
|
public struct OpenVPNCredentialsView: View {
|
2024-11-03 07:17:19 +00:00
|
|
|
private enum Field: Hashable {
|
|
|
|
case username
|
|
|
|
|
|
|
|
case password
|
|
|
|
|
|
|
|
case otp
|
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-11-01 22:32:35 +00:00
|
|
|
@EnvironmentObject
|
|
|
|
private var iapManager: IAPManager
|
2024-10-02 14:05:40 +00:00
|
|
|
|
2024-11-29 14:25:22 +00:00
|
|
|
@EnvironmentObject
|
|
|
|
private var providerManager: ProviderManager
|
|
|
|
|
|
|
|
private let providerId: ProviderID?
|
|
|
|
|
2024-11-01 22:32:35 +00:00
|
|
|
@Binding
|
|
|
|
private var isInteractive: Bool
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-11-01 22:32:35 +00:00
|
|
|
@Binding
|
|
|
|
private var credentials: OpenVPN.Credentials?
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-11-03 10:27:12 +00:00
|
|
|
private let isAuthenticating: Bool
|
|
|
|
|
|
|
|
private let onSubmit: (() -> Void)?
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-11-01 22:32:35 +00:00
|
|
|
@State
|
|
|
|
private var builder = OpenVPN.Credentials.Builder()
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-12-01 23:01:35 +00:00
|
|
|
@State
|
|
|
|
private var providerConfiguration: OpenVPN.Credentials.ProviderConfiguration?
|
|
|
|
|
2024-11-01 22:32:35 +00:00
|
|
|
@State
|
|
|
|
private var paywallReason: PaywallReason?
|
|
|
|
|
2024-11-03 07:17:19 +00:00
|
|
|
@FocusState
|
|
|
|
private var focusedField: Field?
|
|
|
|
|
2024-11-01 22:32:35 +00:00
|
|
|
public init(
|
2024-11-29 14:25:22 +00:00
|
|
|
providerId: ProviderID?,
|
2024-11-01 22:32:35 +00:00
|
|
|
isInteractive: Binding<Bool>,
|
|
|
|
credentials: Binding<OpenVPN.Credentials?>,
|
2024-11-03 10:27:12 +00:00
|
|
|
isAuthenticating: Bool = false,
|
|
|
|
onSubmit: (() -> Void)? = nil
|
2024-11-01 22:32:35 +00:00
|
|
|
) {
|
2024-11-29 14:25:22 +00:00
|
|
|
self.providerId = providerId
|
2024-11-01 22:32:35 +00:00
|
|
|
_isInteractive = isInteractive
|
|
|
|
_credentials = credentials
|
|
|
|
self.isAuthenticating = isAuthenticating
|
2024-11-03 10:27:12 +00:00
|
|
|
self.onSubmit = onSubmit
|
2024-11-01 22:32:35 +00:00
|
|
|
}
|
2024-10-02 14:05:40 +00:00
|
|
|
|
2024-11-01 22:32:35 +00:00
|
|
|
public var body: some View {
|
2024-11-29 18:55:22 +00:00
|
|
|
debugChanges()
|
|
|
|
return Group {
|
2024-11-18 16:43:01 +00:00
|
|
|
if !isAuthenticating {
|
|
|
|
interactiveSection
|
|
|
|
}
|
2024-11-01 22:32:35 +00:00
|
|
|
inputSection
|
2024-11-29 14:25:22 +00:00
|
|
|
guidanceSection
|
2024-11-01 22:32:35 +00:00
|
|
|
}
|
|
|
|
.themeManualInput()
|
2024-12-01 23:11:52 +00:00
|
|
|
.themeAnimation(on: isInteractive, category: .modules)
|
|
|
|
.themeAnimation(on: builder, category: .modules)
|
2024-11-05 12:27:05 +00:00
|
|
|
.modifier(PaywallModifier(reason: $paywallReason))
|
2024-12-01 23:01:35 +00:00
|
|
|
.onLoad(perform: onLoad)
|
|
|
|
.onChange(of: builder, perform: onChange)
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-01 22:32:35 +00:00
|
|
|
private extension OpenVPNCredentialsView {
|
2024-09-23 13:02:26 +00:00
|
|
|
var interactiveSection: some View {
|
|
|
|
Group {
|
2024-11-18 16:43:01 +00:00
|
|
|
Toggle(isOn: $isInteractive) {
|
|
|
|
HStack {
|
|
|
|
Text(Strings.Modules.Openvpn.Credentials.interactive)
|
|
|
|
PurchaseRequiredButton(features: requiredFeatures, paywallReason: $paywallReason)
|
|
|
|
}
|
|
|
|
}
|
2024-12-01 22:23:51 +00:00
|
|
|
.themeRowWithSubtitle(interactiveFooter)
|
2024-09-23 13:02:26 +00:00
|
|
|
|
|
|
|
if isInteractive {
|
|
|
|
Picker(Strings.Unlocalized.otp, selection: $builder.otpMethod) {
|
|
|
|
ForEach(otpMethods, id: \.self) {
|
|
|
|
Text($0.localizedDescription(style: .entity))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-10-03 15:03:53 +00:00
|
|
|
.themeSection(footer: interactiveFooter)
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var interactiveFooter: String? {
|
|
|
|
if isInteractive {
|
|
|
|
return [
|
|
|
|
Strings.Modules.Openvpn.Credentials.Interactive.footer,
|
|
|
|
builder.otpMethod.localizedDescription(style: .approachDescription)
|
|
|
|
].joined(separator: " ")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var inputSection: some View {
|
|
|
|
Group {
|
2024-11-02 14:24:41 +00:00
|
|
|
if !isAuthenticating || builder.otpMethod == .none {
|
2024-11-03 10:27:12 +00:00
|
|
|
usernameField
|
2024-12-01 23:01:35 +00:00
|
|
|
if !ignoresPassword {
|
|
|
|
passwordField
|
|
|
|
}
|
2024-11-02 14:24:41 +00:00
|
|
|
}
|
2024-10-30 16:07:59 +00:00
|
|
|
if isEligibleForInteractiveLogin, isAuthenticating, builder.otpMethod != .none {
|
2024-11-03 10:27:12 +00:00
|
|
|
otpField
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
2024-12-01 22:23:51 +00:00
|
|
|
.themeSection(footer: inputFooter, forcesFooter: true)
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
2024-11-29 14:25:22 +00:00
|
|
|
@ViewBuilder
|
|
|
|
var guidanceSection: some View {
|
2024-12-01 23:01:35 +00:00
|
|
|
if let url = providerConfiguration?.url {
|
2024-11-29 14:25:22 +00:00
|
|
|
Link(Strings.Modules.Openvpn.Credentials.Guidance.link, destination: url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
var inputFooter: String? {
|
|
|
|
if isAuthenticating {
|
|
|
|
return builder.otpMethod.localizedDescription(style: .approachDescription)
|
2024-10-30 16:07:59 +00:00
|
|
|
.nilIfEmpty
|
2024-12-01 23:01:35 +00:00
|
|
|
} else if let providerConfiguration {
|
|
|
|
switch providerConfiguration.purpose {
|
2024-11-29 14:25:22 +00:00
|
|
|
case .web:
|
|
|
|
return Strings.Modules.Openvpn.Credentials.Guidance.web
|
|
|
|
case .specific:
|
|
|
|
return Strings.Modules.Openvpn.Credentials.Guidance.specific
|
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-11-03 10:27:12 +00:00
|
|
|
|
|
|
|
var usernameField: some View {
|
2024-11-23 19:31:22 +00:00
|
|
|
ThemeTextField(Strings.Global.Nouns.username, text: $builder.username, placeholder: Strings.Placeholders.username)
|
2024-11-03 10:27:12 +00:00
|
|
|
.textContentType(.username)
|
|
|
|
.focused($focusedField, equals: .username)
|
|
|
|
}
|
|
|
|
|
|
|
|
var passwordField: some View {
|
2024-11-23 19:31:22 +00:00
|
|
|
ThemeSecureField(title: Strings.Global.Nouns.password, text: $builder.password, placeholder: Strings.Placeholders.secret)
|
2024-11-03 10:27:12 +00:00
|
|
|
.textContentType(.password)
|
|
|
|
.focused($focusedField, equals: .password)
|
|
|
|
.onSubmit {
|
|
|
|
if builder.otpMethod == .none {
|
|
|
|
onSubmit?()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var otpField: some View {
|
|
|
|
ThemeSecureField(
|
|
|
|
title: Strings.Unlocalized.otp,
|
|
|
|
text: $builder.otp ?? "",
|
|
|
|
placeholder: Strings.Placeholders.secret
|
|
|
|
)
|
|
|
|
.textContentType(.oneTimeCode)
|
|
|
|
.focused($focusedField, equals: .otp)
|
|
|
|
.onSubmit {
|
|
|
|
if builder.otpMethod != .none {
|
|
|
|
onSubmit?()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
2024-11-29 14:25:22 +00:00
|
|
|
private extension OpenVPNCredentialsView {
|
|
|
|
var isEligibleForInteractiveLogin: Bool {
|
|
|
|
iapManager.isEligible(for: .interactiveLogin)
|
|
|
|
}
|
|
|
|
|
|
|
|
var requiredFeatures: Set<AppFeature>? {
|
|
|
|
isInteractive ? [.interactiveLogin] : nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var otpMethods: [OpenVPN.Credentials.OTPMethod] {
|
|
|
|
[.none, .append, .encode]
|
|
|
|
}
|
|
|
|
|
2024-12-01 23:01:35 +00:00
|
|
|
var ignoresPassword: Bool {
|
|
|
|
providerConfiguration?.options?.contains(.noPassword) ?? false
|
|
|
|
}
|
|
|
|
|
|
|
|
func onLoad() {
|
|
|
|
if let providerId, let metadata = providerManager.provider(withId: providerId) {
|
|
|
|
providerConfiguration = OpenVPN.Credentials.configuration(forProvider: metadata)
|
|
|
|
}
|
|
|
|
builder = credentials?.builder() ?? OpenVPN.Credentials.Builder()
|
|
|
|
if ignoresPassword {
|
|
|
|
builder.password = ""
|
|
|
|
}
|
|
|
|
builder.otp = nil
|
|
|
|
if isAuthenticating {
|
|
|
|
switch builder.otpMethod {
|
|
|
|
case .none:
|
|
|
|
focusedField = .username
|
|
|
|
|
|
|
|
default:
|
|
|
|
focusedField = .otp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func onChange(_ builder: OpenVPN.Credentials.Builder) {
|
|
|
|
var copy = builder
|
|
|
|
if isEligibleForInteractiveLogin {
|
|
|
|
copy.otp = copy.otp ?? ""
|
|
|
|
} else {
|
|
|
|
copy.otpMethod = .none
|
|
|
|
copy.otp = nil
|
2024-11-29 14:25:22 +00:00
|
|
|
}
|
2024-12-01 23:01:35 +00:00
|
|
|
credentials = copy.build()
|
2024-11-29 14:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Previews
|
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
#Preview {
|
2024-11-13 11:07:30 +00:00
|
|
|
struct ContentView: View {
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-11-13 11:07:30 +00:00
|
|
|
@State
|
|
|
|
private var credentials: OpenVPN.Credentials?
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-11-13 11:07:30 +00:00
|
|
|
@State
|
|
|
|
private var isInteractive = true
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-11-13 11:07:30 +00:00
|
|
|
var body: some View {
|
|
|
|
NavigationStack {
|
|
|
|
OpenVPNCredentialsView(
|
2024-11-29 14:25:22 +00:00
|
|
|
providerId: nil,
|
2024-11-13 11:07:30 +00:00
|
|
|
isInteractive: $isInteractive,
|
|
|
|
credentials: $credentials
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
2024-11-13 11:07:30 +00:00
|
|
|
|
|
|
|
return ContentView()
|
|
|
|
.withMockEnvironment()
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|