8000 [PM-12695] Add hidden field changes to password history by andrebispo5 · Pull Request #1012 · bitwarden/ios · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[PM-12695] Add hidden field changes to password history #1012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Oct 24, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,22 @@ extension Fido2CredentialView {
}
}

extension BitwardenSdk.FieldView {
static func fixture(
name: String? = "Name",
value: String? = "1",
type: BitwardenSdk.FieldType = BitwardenSdk.FieldType.hidden,
linkedId: BitwardenSdk.LinkedIdType? = nil
) -> BitwardenSdk.FieldView {
BitwardenSdk.FieldView(
name: name,
value: value,
type: type,
linkedId: linkedId
)
}
}

extension BitwardenSdk.Login {
static func fixture(
autofillOnPageLoad: Bool? = nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ extension CipherView {

// Update the password updated date and the password history if the password has changed.
var passwordHistory = passwordHistory
let lastUsedDate = timeProvider.presentTime
if addEditState.type == .login,
let previousPassword = login?.password,
addEditState.loginState.password != previousPassword {
let lastUsedDate = timeProvider.presentTime
loginState.passwordUpdatedDate = lastUsedDate

// Update the password history list.
Expand All @@ -158,11 +158,21 @@ extension CipherView {
} else {
passwordHistory!.append(newPasswordHistoryView)
}

// Cap the size of the password history list to 5.
passwordHistory = passwordHistory?.suffix(5)
}

// Map new fields
let newFields = mapToCustomFieldViews(from: addEditState.customFieldsState.customFields)

passwordHistory = updatePasswordHistoryWithHiddenFields(
passwordHistory: passwordHistory,
oldFields: fields,
newFields: newFields,
lastUsedDate: lastUsedDate
)

// Cap the size of the password history list to 5.
passwordHistory = passwordHistory?.suffix(5)

// Return the updated cipher.
return CipherView(
id: id,
Expand All @@ -184,21 +194,52 @@ extension CipherView {
viewPassword: viewPassword,
localData: localData,
attachments: attachments,
fields: addEditState.customFieldsState.customFields.isEmpty ?
nil : addEditState.customFieldsState.customFields.map { customField in
FieldView(
name: customField.name,
value: customField.value,
type: .init(fieldType: customField.type),
linkedId: customField.linkedIdType?.rawValue
)
},
fields: newFields,
passwordHistory: passwordHistory,
creationDate: creationDate,
deletedDate: deletedDate,
revisionDate: revisionDate
)
}

private func updatePasswordHistoryWithHiddenFields(
passwordHistory: [PasswordHistoryView]?,
oldFields: [FieldView]?,
newFields: [FieldView]?,
lastUsedDate: Date
) -> [PasswordHistoryView]? {
guard let oldFields else {
return passwordHistory
}
let newPasswordHistoryFields: [PasswordHistoryView] = oldFields
.filter { field in
FieldType(fieldType: field.type) == FieldType.hidden &&
!field.name.isEmptyOrNil &&
!field.value.isEmptyOrNil &&
!(newFields?.contains(field) ?? false)
}.compactMap { hiddenField in
PasswordHistoryView(
password: "\(hiddenField.name ?? ""): \(hiddenField.value ?? "")",
lastUsedDate: lastUsedDate
)
}
guard !newPasswordHistoryFields.isEmpty else {
return passwordHistory
}
guard let passwordHistory else {
return newPasswordHistoryFields
}
return passwordHistory + newPasswordHistoryFields
}

/// Maps the array of `CustomFieldState` into the an array of `FieldView`.
private func mapToCustomFieldViews(from customFields: [CustomFieldState]) -> [FieldView]? {
guard !customFields.isEmpty else {
return nil
}

return customFields.map { FieldView(customFieldState: $0) }
}
}

extension CipherView {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ final class CipherViewUpdateTests: BitwardenTestCase { // swiftlint:disable:this

/// Tests that the update succeeds with new properties.
func test_update_login_edits_succeeds() {
subject = CipherView.loginFixture()
cipherItemState.type = .login
cipherItemState.notes = "I have a note"
cipherItemState.loginState.username = "PASTA"
Expand Down Expand Up @@ -234,6 +235,86 @@ final class CipherViewUpdateTests: BitwardenTestCase { // swiftlint:disable:this
XCTAssertEqual(newerPasswordHistory?.last?.password, "New password")
}

/// Tests that the update succeeds when a hidden field change modifies the password history.
func test_update_login_passwordHistory_hiddenField_succeeds() {
subject = CipherView.loginFixture(fields: [
FieldView(
name: "Name",
value: "1",
type: BitwardenSdk.FieldType.hidden,
linkedId: nil
),
])
cipherItemState.customFieldsState.customFields = [
CustomFieldState(fieldView: .fixture(value: "2")),
]

let comparison = subject.updatedView(with: cipherItemState)
let newPasswordHistory = comparison.passwordHistory

XCTAssertEqual(newPasswordHistory?.last?.password, "Name: 1")

cipherItemState.customFieldsState.customFields = [
CustomFieldState(fieldView: .fixture(value: "3")),
]

let secondComparison = comparison.updatedView(with: cipherItemState)
let newerPasswordHistory = secondComparison.passwordHistory

XCTAssertEqual(newerPasswordHistory?.last?.password, "Name: 2")
}

/// Tests that the update succeeds when a hidden field is deleted modifies the password history..
func test_update_login_passwordHistory_deleteHiddenField_succeeds() {
subject = CipherView.loginFixture(fields: [
FieldView(
name: "Name",
value: "1",
type: BitwardenSdk.FieldType.hidden,
linkedId: nil
),
])
cipherItemState.customFieldsState.customFields = [
CustomFieldState(fieldView: .fixture()),
CustomFieldState(fieldView: .fixture(name: "NewField", value: "1")),
]

let comparison = subject.updatedView(with: cipherItemState)
let newPasswordHistory = comparison.passwordHistory

XCTAssertNil(newPasswordHistory)

cipherItemState.customFieldsState.customFields = [
CustomFieldState(fieldView: .fixture()),
]

let secondComparison = comparison.updatedView(with: cipherItemState)
let newerPasswordHistory = secondComparison.passwordHistory

XCTAssertEqual(newerPasswordHistory?.last?.password, "NewField: 1")
}

/// Tests a new hidden field doesn't update the password history.
func test_update_login_passwordHistory_newHiddenField_succeeds() {
subject = CipherView.loginFixture(fields: [
FieldView(
name: "Name",
value: "1",
type: BitwardenSdk.FieldType.hidden,
linkedId: nil
),
])
cipherItemState.customFieldsState.customFields = [
CustomFieldState(fieldView: .fixture()),
CustomFieldState(fieldView: .fixture(name: "NewField", value: "1")),
]

let comparison = subject.updatedView(with: cipherItemState)
let newPasswordHistory = comparison.passwordHistory

XCTAssertNil(newPasswordHistory)
}

/// Tests that the update succeeds with a new password updating the password revision date.
func test_update_login_passwordRevisionDate_succeeds() throws {
subject = CipherView.loginFixture(
Expand Down Expand Up @@ -318,4 +399,4 @@ final class CipherViewUpdateTests: BitwardenTestCase { // swiftlint:disable:this
XCTAssertNil(identity.postalCode)
XCTAssertNil(identity.country)
}
}
} // swiftlint:disable:this file_length
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import BitwardenSdk // swiftlint:disable:this file_name

// MARK: - FieldView+Update

extension BitwardenSdk.FieldView {
/// initializes a new FieldView with updated properties
///
/// - Parameter customFieldState: The `CustomFieldState` used to create or update the field view.
///
init(customFieldState: CustomFieldState) {
self.init(
name: customFieldState.name,
value: customFieldState.value,
type: BitwardenSdk.FieldType(fieldType: customFieldState.type),
linkedId: customFieldState.linkedIdType?.rawValue
)
}
}
Loading
0