9
9
*/
10
10
package org.mifospay.core.ui.utils
11
11
12
+ import org.mifospay.core.common.utils.hasConsecutiveRepetitions
13
+ import org.mifospay.core.common.utils.hasSpaces
12
14
import kotlin.math.log2
13
15
import kotlin.math.pow
14
16
@@ -19,29 +21,22 @@ object PasswordChecker {
19
21
private const val MAX_PASSWORD_LENGTH = 50
20
22
21
23
fun getPasswordStrengthResult (password : String ): PasswordStrengthResult {
22
- when {
23
- password.isEmpty() -> return PasswordStrengthResult .Error (" Password cannot be empty." )
24
- password.length > MAX_PASSWORD_LENGTH -> {
25
- return PasswordStrengthResult .Error (
26
- " Password is too long. Maximum length is $MAX_PASSWORD_LENGTH characters." ,
27
- )
28
- }
29
- hasSpaceOrConsecutiveRepetitions(password) -> {
30
- return PasswordStrengthResult .Error (
31
- " Password must not contain spaces or repeating characters." ,
32
- )
33
- }
24
+ val errors = buildList {
25
+ if (password.isEmpty()) add(" - Password cannot be empty." )
26
+ if (password.length > MAX_PASSWORD_LENGTH ) add(" - Password is too long. Maximum length is $MAX_PASSWORD_LENGTH characters." )
27
+ if (password.hasSpaces()) add(" - Password must not contain spaces." )
28
+ if (password.hasConsecutiveRepetitions()) add(" - Password must not contain consecutive repetitive characters." )
29
+ }
30
+
31
+ if (errors.isNotEmpty()) {
32
+ return PasswordStrengthResult .Error (errors.joinToString(" \n " ))
34
33
}
35
34
36
35
val result = getPasswordStrength(password)
37
36
38
37
return PasswordStrengthResult .Success (result)
39
38
}
40
39
41
- fun hasSpaceOrConsecutiveRepetitions (password : String ): Boolean {
42
- return Regex (" (.)\\ 1" ).containsMatchIn(password) || password.contains(" " )
43
- }
44
-
45
40
private fun getPasswordStrength (password : String ): PasswordStrength {
46
41
val length = password.length
47
42
val hasUpperCase = password.any { it.isUpperCase() }
@@ -91,10 +86,10 @@ object PasswordChecker {
91
86
if (password.length < STRONG_PASSWORD_LENGTH ) {
92
87
feedback.add(" For a stronger password, use at least $STRONG_PASSWORD_LENGTH characters." )
93
88
}
94
- if (Regex ( " (.) \\ 1 " ).containsMatchIn(password )) {
89
+ if (password.hasConsecutiveRepetitions( )) {
95
90
feedback.add(" Remove consecutive repeating characters." )
96
91
}
97
- if (password.contains( " " )) {
92
+ if (password.hasSpaces( )) {
98
93
feedback.add(" Remove spaces." )
99
94
}
100
95
0 commit comments