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.
Merge pull request #59 from nodes-vapor/feature/vapor-2-fix-password-…
…validator Fix StrongPassword validation and refactor it (+readability)
- Loading branch information
Showing
2 changed files
with
37 additions
and
10 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,51 @@ | ||
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] | ||
internal var errorDescription: String? | ||
|
||
public init(minLength: Int = 6) { | ||
public init( | ||
minLength: Int = 6, | ||
regexes: [String] = standardRegexes, | ||
minMatchingRegexes: Int = 3, | ||
errorDescription: String? = nil) | ||
{ | ||
self.minLength = minLength | ||
self.regexes = regexes | ||
self.minMatchingRegexes = minMatchingRegexes | ||
self.errorDescription = errorDescription | ||
} | ||
|
||
public func validate(_ input: String) throws { | ||
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.") | ||
|
||
let matchingRegexesCount = regexes | ||
.reduce(0, { | ||
$0 + ((input.range(of: $1, options: .regularExpression) == nil) ? 0 : 1) | ||
}) | ||
|
||
guard input.count >= minLength, matchingRegexesCount >= minMatchingRegexes else { | ||
throw error(errorDescription ?? | ||
"Password is not strong enough. It must be be at least \(minLength) characters long and contain \(minMatchingRegexes) of these: a number, a capital letter, a small letter or a special character.") | ||
} | ||
} | ||
} |
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