Skip to content

Commit

Permalink
Fix brave/brave-ios#6093: Update wallet password policy (brave/brave-…
Browse files Browse the repository at this point in the history
…ios#7182)

Update wallet password policy, add unit tests (no longer using `KeyringService` function).
  • Loading branch information
StephenHeaps authored Apr 3, 2023
1 parent dfd814f commit f7cb31e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 31 deletions.
10 changes: 9 additions & 1 deletion Sources/BraveWallet/Crypto/Stores/KeyringStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,15 @@ public class KeyringStore: ObservableObject {
}

func isStrongPassword(_ password: String, completion: @escaping (Bool) -> Void) {
keyringService.isStrongPassword(password, completion: completion)
completion(password.count >= 8)
}

@MainActor func isStrongPassword(_ password: String) async -> Bool {
await withCheckedContinuation { continuation in
isStrongPassword(password) { isStrong in
continuation.resume(returning: isStrong)
}
}
}

func createWallet(password: String, completion: ((String) -> Void)? = nil) {
Expand Down
30 changes: 1 addition & 29 deletions Sources/BraveWallet/Preview Content/MockKeyringService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -263,35 +263,7 @@ class MockKeyringService: BraveWalletKeyringService {
}

func isStrongPassword(_ password: String, completion: @escaping (Bool) -> Void) {
do {
// Should match shared logic for testing, however this may not always be the case
let range = NSRange(location: 0, length: password.count)
if password.count < 7 {
completion(false)
return
}
// Has at least one letter
if (try NSRegularExpression(pattern: "[a-zA-Z]", options: []))
.numberOfMatches(in: password, options: [], range: range) < 1 {
completion(false)
return
}
// Has at least one number
if (try NSRegularExpression(pattern: "[0-9]", options: []))
.numberOfMatches(in: password, options: [], range: range) < 1 {
completion(false)
return
}
// Has at least one non-alphanumeric
if (try NSRegularExpression(pattern: "[^0-9a-zA-Z]", options: []))
.numberOfMatches(in: password, options: [], range: range) < 1 {
completion(false)
return
}
completion(true)
} catch {
completion(false)
}
completion(password.count >= 8)
}

func checksumEthAddress(_ address: String, completion: @escaping (String) -> Void) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/BraveWallet/WalletStrings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ extension Strings {
"wallet.passwordDoesNotMeetRequirementsError",
tableName: "BraveWallet",
bundle: .module,
value: "Passwords must be at least 7 characters, and contain at least one letter, one number, and one special character.",
value: "Passwords must be at least 8 characters.",
comment: "The error message displayed when a user enters a password that does not meet the requirements"
)
public static let passwordsDontMatchError = NSLocalizedString(
Expand Down
31 changes: 31 additions & 0 deletions Tests/BraveWalletTests/KeyringStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,35 @@ class KeyringStoreTests: XCTestCase {
XCTAssertNil(error)
}
}

@MainActor func testIsStrongPassword() async {
let (keyringService, rpcService, walletService) = setupServices()
let store = KeyringStore(
keyringService: keyringService,
walletService: walletService,
rpcService: rpcService
)

let invalidPassword1 = ""
var isStrongPassword = await store.isStrongPassword(invalidPassword1)
XCTAssertFalse(isStrongPassword)

let invalidPassword2 = "1234"
isStrongPassword = await store.isStrongPassword(invalidPassword2)
XCTAssertFalse(isStrongPassword)

let validPassword = "12345678"
isStrongPassword = await store.isStrongPassword(validPassword)
XCTAssertTrue(isStrongPassword)

let uuid = UUID().uuidString
// first 30 characters of uuid
let validPassword2 = String(uuid[uuid.startIndex..<uuid.index(uuid.startIndex, offsetBy: 30)])
isStrongPassword = await store.isStrongPassword(validPassword2)
XCTAssertTrue(isStrongPassword)

let strongPassword = "LDKH66BJbLsHQPEAK@4_zak*"
isStrongPassword = await store.isStrongPassword(strongPassword)
XCTAssertTrue(isStrongPassword)
}
}

0 comments on commit f7cb31e

Please sign in to comment.