Last active
April 5, 2022 09:18
-
-
Save ExNDY/bd35c7ece62ed04b4b57472e8762273f to your computer and use it in GitHub Desktop.
GitHub authentication validation (login, authToken)
This file contains hidden or 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
object Validator { | |
private const val LOGIN_PATTERN = "^[a-zA-Z0-9_-]{0,38}$" | |
private const val AUTHORIZATION_TOKEN_PATTERN = "^gh[pousr]_[a-zA-Z0-9]{0,40}$" | |
fun validateLogin(login: String): Validation { | |
if (login.isEmpty()) return FieldValidation.Empty | |
val matcher = Pattern.compile(LOGIN_PATTERN).matcher(login) | |
return if (matcher.matches()) Validation.Correct else Validation.Incorrect | |
} | |
fun validateAuthorisationToken(authToken: String): Validation { | |
if (authToken.isEmpty()) return FieldValidation.Empty | |
val matcher = Pattern.compile(AUTHORIZATION_TOKEN_PATTERN).matcher(authToken) | |
return if (matcher.matches()) Validation.Correct else Validation.Incorrect | |
} | |
} | |
sealed interface Validation { | |
object Correct : Validation | |
object Incorrect : Validation | |
object Empty : Validation | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment