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/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
import PassepartoutKit
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
extension OpenVPNView {
|
|
|
|
struct CredentialsView: View {
|
|
|
|
|
2024-10-02 14:05:40 +00:00
|
|
|
@EnvironmentObject
|
|
|
|
private var iapManager: IAPManager
|
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
@Binding
|
|
|
|
var isInteractive: Bool
|
|
|
|
|
|
|
|
@Binding
|
|
|
|
var credentials: OpenVPN.Credentials?
|
|
|
|
|
|
|
|
var isAuthenticating = false
|
|
|
|
|
|
|
|
@State
|
|
|
|
private var builder = OpenVPN.Credentials.Builder()
|
|
|
|
|
|
|
|
@State
|
|
|
|
private var otp = ""
|
|
|
|
|
2024-10-02 14:05:40 +00:00
|
|
|
@State
|
|
|
|
private var paywallReason: PaywallReason?
|
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
var body: some View {
|
|
|
|
Form {
|
2024-10-02 14:05:40 +00:00
|
|
|
restrictedArea
|
2024-09-23 13:02:26 +00:00
|
|
|
inputSection
|
|
|
|
}
|
|
|
|
.themeAnimation(on: isInteractive, category: .modules)
|
|
|
|
.themeManualInput()
|
|
|
|
.themeForm()
|
|
|
|
.navigationTitle(Strings.Modules.Openvpn.credentials)
|
|
|
|
.onLoad {
|
|
|
|
builder = credentials?.builder() ?? OpenVPN.Credentials.Builder()
|
|
|
|
}
|
|
|
|
.onChange(of: builder) {
|
2024-10-02 14:05:40 +00:00
|
|
|
if isEligibleForInteractiveLogin, isAuthenticating {
|
2024-09-23 13:02:26 +00:00
|
|
|
credentials = $0.buildForAuthentication(otp: otp)
|
|
|
|
} else {
|
|
|
|
credentials = $0.build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.onChange(of: otp) {
|
|
|
|
credentials = builder.buildForAuthentication(otp: $0)
|
|
|
|
}
|
2024-10-02 14:05:40 +00:00
|
|
|
.modifier(PaywallModifier(reason: $paywallReason))
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension OpenVPNView.CredentialsView {
|
2024-10-02 14:05:40 +00:00
|
|
|
var isEligibleForInteractiveLogin: Bool {
|
|
|
|
iapManager.isEligible(for: .interactiveLogin)
|
|
|
|
}
|
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
var otpMethods: [OpenVPN.Credentials.OTPMethod] {
|
|
|
|
[.none, .append, .encode]
|
|
|
|
}
|
|
|
|
|
2024-10-02 14:05:40 +00:00
|
|
|
@ViewBuilder
|
|
|
|
var restrictedArea: some View {
|
|
|
|
switch iapManager.paywallReason(forFeature: .interactiveLogin) {
|
|
|
|
case .purchase(let appFeature):
|
|
|
|
Button(Strings.Modules.Openvpn.Purchase.interactive) {
|
|
|
|
paywallReason = .purchase(appFeature)
|
|
|
|
}
|
|
|
|
|
|
|
|
case .restricted:
|
|
|
|
EmptyView()
|
|
|
|
|
|
|
|
default:
|
|
|
|
if !isAuthenticating {
|
|
|
|
interactiveSection
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
var interactiveSection: some View {
|
|
|
|
Group {
|
|
|
|
Toggle(Strings.Modules.Openvpn.Credentials.interactive, isOn: $isInteractive)
|
|
|
|
|
|
|
|
if isInteractive {
|
|
|
|
Picker(Strings.Unlocalized.otp, selection: $builder.otpMethod) {
|
|
|
|
ForEach(otpMethods, id: \.self) {
|
|
|
|
Text($0.localizedDescription(style: .entity))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.themeSectionWithFooter(interactiveFooter)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
ThemeTextField(Strings.Global.username, text: $builder.username, placeholder: Strings.Placeholders.username)
|
|
|
|
.textContentType(.username)
|
|
|
|
ThemeSecureField(title: Strings.Global.password, text: $builder.password, placeholder: Strings.Placeholders.secret)
|
|
|
|
.textContentType(.password)
|
|
|
|
|
2024-10-02 14:05:40 +00:00
|
|
|
if isEligibleForInteractiveLogin, isAuthenticating && builder.otpMethod != .none {
|
2024-09-23 13:02:26 +00:00
|
|
|
ThemeSecureField(title: Strings.Unlocalized.otp, text: $otp, placeholder: Strings.Placeholders.secret)
|
|
|
|
.textContentType(.oneTimeCode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.themeSectionWithFooter(inputFooter)
|
|
|
|
}
|
|
|
|
|
|
|
|
var inputFooter: String? {
|
|
|
|
if isAuthenticating {
|
|
|
|
return builder.otpMethod.localizedDescription(style: .approachDescription)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#Preview {
|
|
|
|
|
|
|
|
@State
|
|
|
|
var credentials: OpenVPN.Credentials?
|
|
|
|
|
|
|
|
@State
|
|
|
|
var isInteractive = false
|
|
|
|
|
|
|
|
return NavigationStack {
|
|
|
|
OpenVPNView.CredentialsView(
|
|
|
|
isInteractive: $isInteractive,
|
|
|
|
credentials: $credentials
|
|
|
|
)
|
2024-10-02 14:05:40 +00:00
|
|
|
.withMockEnvironment()
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
}
|