-
Notifications
You must be signed in to change notification settings - Fork 3
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 #578 from digitalfabrik/482-CheckAndValidatePasswo…
…rdPolicies 482: Check and validate password policies
- Loading branch information
Showing
5 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
42 changes: 42 additions & 0 deletions
42
backend/src/main/kotlin/app/ehrenamtskarte/backend/auth/PasswordValidator.kt
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package app.ehrenamtskarte.backend.auth | ||
|
||
const val minPasswordLength = 12 | ||
const val minLowercaseChars = 1 | ||
const val minUppercaseChars = 1 | ||
const val minNumericChars = 0 | ||
const val minSpecialChars = 1 | ||
|
||
enum class PasswordValidationResult { | ||
VALID, | ||
NOT_LONG_ENOUGH, | ||
TOO_FEW_LOWERCASE_CHARS, | ||
TOO_FEW_UPPERCASE_CHARS, | ||
TOO_FEW_NUMERIC_CHARS, | ||
TOO_FEW_SPECIAL_CHARS | ||
} | ||
|
||
object PasswordValidator { | ||
|
||
fun validatePassword(password: String): PasswordValidationResult { | ||
if (password.length < minPasswordLength) { | ||
return PasswordValidationResult.NOT_LONG_ENOUGH | ||
} | ||
val numLowercaseChars = password.count { it.isLowerCase() } | ||
val numUppercaseChars = password.count { it.isUpperCase() } | ||
val numNumericChars = password.count { it.isDigit() } | ||
val numSpecialChars = password.length - numLowercaseChars - numUppercaseChars - numNumericChars | ||
if (numLowercaseChars < minLowercaseChars) { | ||
return PasswordValidationResult.TOO_FEW_LOWERCASE_CHARS | ||
} else if (numUppercaseChars < minUppercaseChars) { | ||
return PasswordValidationResult.TOO_FEW_UPPERCASE_CHARS | ||
} else if (numNumericChars < minNumericChars) { | ||
return PasswordValidationResult.TOO_FEW_NUMERIC_CHARS | ||
} else if (numSpecialChars < minSpecialChars) { | ||
return PasswordValidationResult.TOO_FEW_SPECIAL_CHARS | ||
} | ||
return PasswordValidationResult.VALID | ||
} | ||
} | ||
|
||
class InvalidPasswordException(passwordValidation: PasswordValidationResult) : | ||
Throwable(message = passwordValidation.toString()) |
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
52 changes: 52 additions & 0 deletions
52
backend/src/test/kotlin/app/ehrenamtskarte/backend/auth/PasswordValidatorTest.kt
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package app.ehrenamtskarte.backend.auth | ||
|
||
import org.junit.Test | ||
import kotlin.test.assertEquals | ||
|
||
internal class PasswordValidatorTest { | ||
|
||
@Test | ||
fun invalidatesPasswordIfTooShort() { | ||
val password = "a!Bcrf83592" | ||
assertEquals( | ||
PasswordValidator.validatePassword(password), | ||
PasswordValidationResult.NOT_LONG_ENOUGH | ||
) | ||
} | ||
|
||
@Test | ||
fun invalidatesPasswordIfTooFewLowercaseChars() { | ||
val password = "A!BCRF835921" | ||
assertEquals( | ||
PasswordValidator.validatePassword(password), | ||
PasswordValidationResult.TOO_FEW_LOWERCASE_CHARS | ||
) | ||
} | ||
|
||
@Test | ||
fun invalidatesPasswordIfTooFewUppercaseChars() { | ||
val password = "a!bcrf835921" | ||
assertEquals( | ||
PasswordValidator.validatePassword(password), | ||
PasswordValidationResult.TOO_FEW_UPPERCASE_CHARS | ||
) | ||
} | ||
|
||
@Test | ||
fun invalidatesPasswordIfTooFewSpecialChars() { | ||
val password = "a1Bcrf835921" | ||
assertEquals( | ||
PasswordValidator.validatePassword(password), | ||
PasswordValidationResult.TOO_FEW_SPECIAL_CHARS | ||
) | ||
} | ||
|
||
@Test | ||
fun validatesPassword() { | ||
val password = "a!Bcrf835921" | ||
assertEquals( | ||
PasswordValidator.validatePassword(password), | ||
PasswordValidationResult.VALID | ||
) | ||
} | ||
} |