This repository has been archived by the owner on Apr 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix StrongPassword validation and refactor it (+readability)
Addresses #48
- Loading branch information
1 parent
a5d6de8
commit 3f02395
Showing
2 changed files
with
32 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,47 @@ | ||
import Vapor | ||
import Validation | ||
|
||
private let regex = "^.*(?=.{3,})(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[\\d\\X])(?=.*[^a-zA-Z\\d\\s:]).*$" | ||
|
||
/*The password contains characters from at least three of the following four categories: | ||
|
||
English uppercase characters (A – Z) | ||
English lowercase characters (a – z) | ||
Base 10 digits (0 – 9) | ||
Non-alphanumeric (For example: !, $, #, or %) | ||
Unicode characters | ||
|
||
*/ | ||
|
||
public struct StrongPassword: Validator { | ||
|
||
static let lowerCaseRegex = "^.*[a-z].*$" | ||
static let upperCaseRegex = "^.*[A-Z].*$" | ||
static let digitsRegex = "^.*[0-9].*$" | ||
static let specialCharRegex = "^.*[^a-zA-Z0-9].*$" | ||
|
||
static let standardRegexes = [lowerCaseRegex, upperCaseRegex, digitsRegex, specialCharRegex] | ||
|
||
internal let minLength: Int | ||
internal let minMatchingRegexes: Int | ||
internal let regexes: [String] | ||
|
||
public init(minLength: Int = 6, | ||
regexes: [String] = standardRegexes, | ||
minMatchingRegexes: Int = 3) { | ||
|
||
public init(minLength: Int = 6) { | ||
self.minLength = minLength | ||
self.regexes = regexes | ||
self.minMatchingRegexes = minMatchingRegexes | ||
} | ||
|
||
public func validate(_ input: String) throws { | ||
|
||
let matchingRegexesCount = regexes | ||
.reduce(0, {$0 + ((input.range(of: $1, options: .regularExpression) == nil) ? 0 : 1) }) | ||
|
||
guard | ||
input.range(of: regex, options: .regularExpression) != nil | ||
&& input.count >= minLength | ||
else { | ||
throw error("Password is not strong enough. It must be be at least \(minLength) characters long and contain a number, a capital letter and a small letter.") | ||
input.count >= minLength, | ||
matchingRegexesCount >= minMatchingRegexes | ||
else { | ||
throw error("Password is not strong enough. It must be be at least \(minLength) characters long and contain a number, a capital letter and a small letter.") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters