Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion FlowCrypt.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
214A023A26A3029700C24066 /* EmailKeyManagerApi.swift in Sources */ = {isa = PBXBuildFile; fileRef = 214A023926A3029700C24066 /* EmailKeyManagerApi.swift */; };
215002A32690B1DD00980DDD /* client_configuraion_with_unknown_flag.json in Resources */ = {isa = PBXBuildFile; fileRef = 215002A22690B1DD00980DDD /* client_configuraion_with_unknown_flag.json */; };
215897E8267A553300423694 /* FilesManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 215897E7267A553200423694 /* FilesManager.swift */; };
21750D7D26C6AFA6007E6A6F /* SetupEKMKeyViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21750D7C26C6AFA6007E6A6F /* SetupEKMKeyViewController.swift */; };
21750D7F26C6C1E3007E6A6F /* SetupCreatePassphraseAbstractViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21750D7E26C6C1E3007E6A6F /* SetupCreatePassphraseAbstractViewController.swift */; };
2196A2202684B9BE001B9E00 /* URLExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2196A21F2684B9BE001B9E00 /* URLExtension.swift */; };
21B15C9426AA00A400D8522B /* ClientConfigurationServiceResults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21B15C9326AA00A400D8522B /* ClientConfigurationServiceResults.swift */; };
21C7DEFC26669A3700C44800 /* CalendarExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21C7DEFB26669A3700C44800 /* CalendarExtension.swift */; };
Expand Down Expand Up @@ -392,6 +394,8 @@
214A023926A3029700C24066 /* EmailKeyManagerApi.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmailKeyManagerApi.swift; sourceTree = "<group>"; };
215002A22690B1DD00980DDD /* client_configuraion_with_unknown_flag.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = client_configuraion_with_unknown_flag.json; sourceTree = "<group>"; };
215897E7267A553200423694 /* FilesManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FilesManager.swift; sourceTree = "<group>"; };
21750D7C26C6AFA6007E6A6F /* SetupEKMKeyViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SetupEKMKeyViewController.swift; sourceTree = "<group>"; };
21750D7E26C6C1E3007E6A6F /* SetupCreatePassphraseAbstractViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SetupCreatePassphraseAbstractViewController.swift; sourceTree = "<group>"; };
2196A21F2684B9BE001B9E00 /* URLExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLExtension.swift; sourceTree = "<group>"; };
21B15C9326AA00A400D8522B /* ClientConfigurationServiceResults.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClientConfigurationServiceResults.swift; sourceTree = "<group>"; };
21C7DEFB26669A3700C44800 /* CalendarExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CalendarExtension.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1041,6 +1045,7 @@
9F1B49E02624E19D00420472 /* Realm Models */ = {
isa = PBXGroup;
children = (
9FC7EAB2266A404D00F3BF5D /* PassPhraseObject.swift */,
D2F41372243CC7990066AFB5 /* UserObject.swift */,
D27B911C24EFE806002DF0A1 /* ContactObject.swift */,
D2E26F6924F25AB800612AF1 /* KeyAlgoObject.swift */,
Expand Down Expand Up @@ -1588,7 +1593,9 @@
isa = PBXGroup;
children = (
9F4163B7265ED61C00106194 /* SetupInitialViewController.swift */,
21750D7E26C6C1E3007E6A6F /* SetupCreatePassphraseAbstractViewController.swift */,
9FA405C6265AEBA40084D133 /* SetupGenerateKeyViewController.swift */,
21750D7C26C6AFA6007E6A6F /* SetupEKMKeyViewController.swift */,
C192421E1EC48B6900C3D251 /* SetupBackupsViewController.swift */,
9F268890237DC55600428A94 /* SetupManuallyImportKeyViewController.swift */,
9FD364852381EFCB00657302 /* SetupManuallyEnterPassPhraseViewController.swift */,
Expand Down Expand Up @@ -1628,7 +1635,6 @@
D212D36324C1AC4800035991 /* KeyId.swift */,
D212D35C24C1AACF00035991 /* PrvKeyInfo.swift */,
D2E26F6B24F25B1F00612AF1 /* KeyAlgo.swift */,
9FC7EAB2266A404D00F3BF5D /* PassPhraseObject.swift */,
2133D51726A1E45300CC686F /* DecryptedPrivateKey.swift */,
);
path = Models;
Expand Down Expand Up @@ -2565,6 +2571,7 @@
5A39F437239ECC23001F4607 /* KeySettingsViewController.swift in Sources */,
9FF0673325520DE400FCC9E6 /* GmailService+send.swift in Sources */,
D20D3C892520B67C00D4AA9A /* BackupOptionsViewController.swift in Sources */,
21750D7D26C6AFA6007E6A6F /* SetupEKMKeyViewController.swift in Sources */,
211392A5266511E6009202EC /* PubLookup.swift in Sources */,
D20D3C672520AB1000D4AA9A /* BackupViewController.swift in Sources */,
D2E26F6324F1698100612AF1 /* ContactsListViewController.swift in Sources */,
Expand Down Expand Up @@ -2617,6 +2624,7 @@
9F0C3C102316DD5B00299985 /* GoogleUserService.swift in Sources */,
D24F4C2223E2359B00C5EEE4 /* BootstrapViewController.swift in Sources */,
D211CE7623FC36BC00D1CE38 /* UIColorExtension.swift in Sources */,
21750D7F26C6C1E3007E6A6F /* SetupCreatePassphraseAbstractViewController.swift in Sources */,
9FC7EBAA266EBD3700F3BF5D /* InMemoryPassPhraseStorage.swift in Sources */,
9FA9C83C264C2D75005A9670 /* MessageService.swift in Sources */,
D2FF6966243115EC007182F0 /* SetupImapViewController.swift in Sources */,
Expand Down
6 changes: 3 additions & 3 deletions FlowCrypt/Controllers/Setup/PassPraseSaveable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import FlowCryptUI

protocol PassPhraseSaveable {
var shouldSaveLocally: Bool { get set }
var shouldStorePassPhrase: Bool { get set }
var passPhraseIndexes: [IndexPath] { get }
var saveLocallyNode: CellNode { get }
var saveInMemoryNode: CellNode { get }
Expand All @@ -26,11 +26,11 @@ extension PassPhraseSaveable where Self: TableNodeViewController {
}

var saveLocallyNode: CellNode {
CheckBoxTextNode(input: .passPhraseLocally(isSelected: self.shouldSaveLocally))
CheckBoxTextNode(input: .passPhraseLocally(isSelected: self.shouldStorePassPhrase))
}

var saveInMemoryNode: CellNode {
CheckBoxTextNode(input: .passPhraseMemory(isSelected: !self.shouldSaveLocally))
CheckBoxTextNode(input: .passPhraseMemory(isSelected: !self.shouldStorePassPhrase))
}

func showPassPhraseErrorAlert() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class SetupBackupsViewController: TableNodeViewController, PassPhraseSavea

private var passPhrase: String?

var shouldSaveLocally = true {
var shouldStorePassPhrase = true {
didSet {
handleSelectedPassPhraseOption()
}
Expand Down Expand Up @@ -150,7 +150,7 @@ extension SetupBackupsViewController {
PassPhrase(value: passPhrase, fingerprints: $0.fingerprints)
}
.forEach {
passPhraseService.savePassPhrase(with: $0, inStorage: shouldSaveLocally)
passPhraseService.savePassPhrase(with: $0, inStorage: shouldStorePassPhrase)
}

// save keys
Expand Down Expand Up @@ -256,9 +256,9 @@ extension SetupBackupsViewController: ASTableDelegate, ASTableDataSource {

switch part {
case .saveLocally:
shouldSaveLocally = true
shouldStorePassPhrase = true
case .saveInMemory:
shouldSaveLocally = false
shouldStorePassPhrase = false
default:
break
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
//
// SetupCreatePassphraseAbstractViewController.swift
// FlowCrypt
//
// Created by Yevhen Kyivskyi on 13.08.2021.
// Copyright © 2021 FlowCrypt Limited. All rights reserved.
//

import AsyncDisplayKit
import FlowCryptCommon
import FlowCryptUI
import Promises

/**
* Controller which decalres a base logic for passphrase setup
* - Has not to have an instance!
*/

class SetupCreatePassphraseAbstractViewController: TableNodeViewController, PassPhraseSaveable {
enum Parts: Int, CaseIterable {
case title, description, passPhrase, divider, saveLocally, saveInMemory, action, subtitle
}

var parts: [Parts] {
Parts.allCases
}

let decorator: SetupViewDecorator
let core: Core
let router: GlobalRouterType
let user: UserId
let storage: DataServiceType
let keyStorage: KeyStorageType
let passPhraseService: PassPhraseServiceType

var shouldStorePassPhrase = true {
didSet {
handleSelectedPassPhraseOption()
}
}

var passPhraseIndexes: [IndexPath] {
[Parts.saveLocally, Parts.saveInMemory]
.map { IndexPath(row: $0.rawValue, section: 0) }
}

private var passPhrase: String?

private lazy var logger = Logger.nested(in: Self.self, with: .setup)

init(
user: UserId,
core: Core = .shared,
router: GlobalRouterType = GlobalRouter(),
decorator: SetupViewDecorator = SetupViewDecorator(),
storage: DataServiceType = DataService.shared,
keyStorage: KeyStorageType = KeyDataStorage(),
passPhraseService: PassPhraseServiceType = PassPhraseService()
) {
self.user = user
self.core = core
self.router = router
self.decorator = decorator
self.storage = storage
self.keyStorage = keyStorage
self.passPhraseService = passPhraseService
super.init(node: TableNode())
}

@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func viewDidLoad() {
super.viewDidLoad()
setupUI()
}

func setupAccount(with passphrase: String) {
fatalError("This method has to be overriden")
}
}

// MARK: - UI

extension SetupCreatePassphraseAbstractViewController {
func setupUI() {
node.delegate = self
node.dataSource = self

title = decorator.sceneTitle(for: .createKey)
observeKeyboardNotifications()
}

// TODO: - Ticket? - Unify this logic for all controllers
// swiftlint:disable discarded_notification_center_observer
private func observeKeyboardNotifications() {
NotificationCenter.default.addObserver(
forName: UIResponder.keyboardWillShowNotification,
object: nil,
queue: .main
) { [weak self] notification in
guard let self = self else { return }
self.adjustForKeyboard(height: self.keyboardHeight(from: notification))
}

NotificationCenter.default.addObserver(
forName: UIResponder.keyboardWillHideNotification,
object: nil,
queue: .main
) { [weak self] _ in
self?.adjustForKeyboard(height: 0)
}
}

private func adjustForKeyboard(height: CGFloat) {
let insets = UIEdgeInsets(top: 0, left: 0, bottom: height + 5, right: 0)
node.contentInset = insets
node.scrollToRow(at: IndexPath(item: Parts.passPhrase.rawValue, section: 0), at: .middle, animated: true)
}
}

// MARK: - Setup

extension SetupCreatePassphraseAbstractViewController {

func validateAndConfirmNewPassPhraseOrReject(passPhrase: String) -> Promise<Void> {
Promise { [weak self] in
guard let self = self else { throw AppErr.nilSelf }

let strength = try self.core.zxcvbnStrengthBar(passPhrase: passPhrase)

guard strength.word.pass else {
throw CreateKeyError.weakPassPhrase(strength)
}

let confirmPassPhrase = try awaitPromise(self.awaitUserPassPhraseEntry())

guard confirmPassPhrase != nil else {
throw CreateKeyError.conformingPassPhraseError
}

guard confirmPassPhrase == passPhrase else {
throw CreateKeyError.doesntMatch
}
}
}

private func awaitUserPassPhraseEntry() -> Promise<String?> {
Promise<String?>(on: .main) { [weak self] resolve, _ in
guard let self = self else { throw AppErr.nilSelf }
let alert = UIAlertController(
title: "Pass Phrase",
message: "Confirm Pass Phrase",
preferredStyle: .alert
)

alert.addTextField { textField in
textField.isSecureTextEntry = true
textField.accessibilityLabel = "textField"
}

alert.addAction(UIAlertAction(title: "cancel".localized, style: .default) { _ in
resolve(nil)
})

alert.addAction(UIAlertAction(title: "ok".localized, style: .default) { [weak alert] _ in
resolve(alert?.textFields?[0].text)
})

self.present(alert, animated: true, completion: nil)
}
}
}

extension SetupCreatePassphraseAbstractViewController {
func moveToMainFlow() {
router.proceed()
}

private func showChoosingOptions() {
showToast("Not implemented yet")
}

private func handleButtonAction() {
view.endEditing(true)
guard let passPhrase = passPhrase, passPhrase.isNotEmpty else {
showAlert(message: "setup_wrong_pass_phrase_retry".localized)
return
}
logger.logInfo("Setup account with \(passPhrase)")
setupAccount(with: passPhrase)
}
}

// MARK: - ASTableDelegate, ASTableDataSource

extension SetupCreatePassphraseAbstractViewController: ASTableDelegate, ASTableDataSource {
func tableNode(_: ASTableNode, numberOfRowsInSection _: Int) -> Int {
parts.count
}

func tableNode(_: ASTableNode, nodeBlockForRowAt indexPath: IndexPath) -> ASCellNodeBlock {
return { [weak self] in
guard let self = self else { return ASCellNode() }
let part = self.parts[indexPath.row]
switch part {
case .title:
return SetupTitleNode(
SetupTitleNode.Input(
title: self.decorator.title(for: .setup),
insets: self.decorator.insets.titleInset,
backgroundColor: .backgroundColor
)
)
case .description:
return SetupTitleNode(
SetupTitleNode.Input(
title: self.decorator.subtitle(for: .choosingPassPhrase),
insets: self.decorator.insets.subTitleInset,
backgroundColor: .backgroundColor
)
)
case .passPhrase:
return TextFieldCellNode(input: .passPhraseTextFieldStyle) { [weak self] action in
guard case let .didEndEditing(value) = action else { return }
self?.passPhrase = value
}
.onShouldReturn { [weak self] _ in
self?.view.endEditing(true)
self?.handleButtonAction()
return true
}
.then {
$0.becomeFirstResponder()
}
case .action:
let input = ButtonCellNode.Input(
title: self.decorator.buttonTitle(for: .setPassPhrase),
insets: self.decorator.insets.buttonInsets
)
return ButtonCellNode(input: input) { [weak self] in
self?.handleButtonAction()
}
case .subtitle:
return SetupTitleNode(
SetupTitleNode.Input(
title: self.decorator.passPhraseLostDescription,
insets: .side(8),
backgroundColor: .backgroundColor
)
)
case .divider:
return DividerCellNode(inset: self.decorator.insets.dividerInsets)
case .saveLocally:
return self.saveLocallyNode
case .saveInMemory:
return self.saveInMemoryNode
}
}
}

func tableNode(_ tableNode: ASTableNode, didSelectRowAt indexPath: IndexPath) {
let part = parts[indexPath.row]

switch part {
case .description:
showChoosingOptions()
case .saveLocally:
shouldStorePassPhrase = true
case .saveInMemory:
shouldStorePassPhrase = false
default:
break
}
}
}
Loading