Skip to content

Instantly share code, notes, and snippets.

@ExNDY
Last active April 5, 2022 09:18
Show Gist options
  • Save ExNDY/bd35c7ece62ed04b4b57472e8762273f to your computer and use it in GitHub Desktop.
Save ExNDY/bd35c7ece62ed04b4b57472e8762273f to your computer and use it in GitHub Desktop.
GitHub authentication validation (login, authToken)
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