mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2025-12-04 15:49:33 +01:00
Add min and max values for OTP
This commit is contained in:
@@ -6,7 +6,10 @@ import android.text.Editable
|
||||
import android.text.TextWatcher
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.*
|
||||
import android.widget.AdapterView
|
||||
import android.widget.ArrayAdapter
|
||||
import android.widget.EditText
|
||||
import android.widget.Spinner
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.fragment.app.DialogFragment
|
||||
import com.google.android.material.textfield.TextInputLayout
|
||||
@@ -14,6 +17,12 @@ import com.kunzisoft.keepass.BuildConfig
|
||||
import com.kunzisoft.keepass.R
|
||||
import com.kunzisoft.keepass.model.OtpModel
|
||||
import com.kunzisoft.keepass.otp.OtpElement
|
||||
import com.kunzisoft.keepass.otp.OtpElement.Companion.MAX_HOTP_COUNTER
|
||||
import com.kunzisoft.keepass.otp.OtpElement.Companion.MAX_OTP_DIGITS
|
||||
import com.kunzisoft.keepass.otp.OtpElement.Companion.MAX_TOTP_PERIOD
|
||||
import com.kunzisoft.keepass.otp.OtpElement.Companion.MIN_HOTP_COUNTER
|
||||
import com.kunzisoft.keepass.otp.OtpElement.Companion.MIN_OTP_DIGITS
|
||||
import com.kunzisoft.keepass.otp.OtpElement.Companion.MIN_TOTP_PERIOD
|
||||
import com.kunzisoft.keepass.otp.OtpTokenType
|
||||
import com.kunzisoft.keepass.otp.OtpType
|
||||
import com.kunzisoft.keepass.otp.TokenCalculator
|
||||
@@ -190,7 +199,7 @@ class SetOTPDialogFragment : DialogFragment() {
|
||||
otpCounterContainer?.error = null
|
||||
} catch (exception: Exception) {
|
||||
otpCounterContainer?.error = getString(R.string.error_otp_counter,
|
||||
0, Long.MAX_VALUE)
|
||||
MIN_HOTP_COUNTER, MAX_HOTP_COUNTER)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -207,7 +216,7 @@ class SetOTPDialogFragment : DialogFragment() {
|
||||
otpPeriodContainer?.error = null
|
||||
} catch (exception: Exception) {
|
||||
otpPeriodContainer?.error = getString(R.string.error_otp_period,
|
||||
0, 60)
|
||||
MIN_TOTP_PERIOD, MAX_TOTP_PERIOD)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -224,7 +233,7 @@ class SetOTPDialogFragment : DialogFragment() {
|
||||
otpDigitsContainer?.error = null
|
||||
} catch (exception: Exception) {
|
||||
otpDigitsContainer?.error = getString(R.string.error_otp_digits,
|
||||
6, 10)
|
||||
MIN_OTP_DIGITS, MAX_OTP_DIGITS)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ data class OtpElement(var otpModel: OtpModel = OtpModel()) {
|
||||
var counter
|
||||
get() = otpModel.counter
|
||||
set(value) {
|
||||
otpModel.counter = if (value < 0 && value > Long.MAX_VALUE) {
|
||||
otpModel.counter = if (value < MIN_HOTP_COUNTER || value > MAX_HOTP_COUNTER) {
|
||||
TokenCalculator.HOTP_INITIAL_COUNTER
|
||||
throw NumberFormatException()
|
||||
} else value
|
||||
@@ -78,7 +78,7 @@ data class OtpElement(var otpModel: OtpModel = OtpModel()) {
|
||||
var period
|
||||
get() = otpModel.period
|
||||
set(value) {
|
||||
otpModel.period = if (value <= 0 || value > 60) {
|
||||
otpModel.period = if (value < MIN_TOTP_PERIOD || value > MAX_TOTP_PERIOD) {
|
||||
TokenCalculator.TOTP_DEFAULT_PERIOD
|
||||
throw NumberFormatException()
|
||||
} else value
|
||||
@@ -87,7 +87,7 @@ data class OtpElement(var otpModel: OtpModel = OtpModel()) {
|
||||
var digits
|
||||
get() = otpModel.digits
|
||||
set(value) {
|
||||
otpModel.digits = if (value <= 0|| value > 10) {
|
||||
otpModel.digits = if (value < MIN_OTP_DIGITS|| value > MAX_OTP_DIGITS) {
|
||||
TokenCalculator.OTP_DEFAULT_DIGITS
|
||||
throw NumberFormatException()
|
||||
} else value
|
||||
@@ -154,6 +154,15 @@ data class OtpElement(var otpModel: OtpModel = OtpModel()) {
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val MIN_HOTP_COUNTER = 1
|
||||
const val MAX_HOTP_COUNTER = Long.MAX_VALUE
|
||||
|
||||
const val MIN_TOTP_PERIOD = 1
|
||||
const val MAX_TOTP_PERIOD = 60
|
||||
|
||||
const val MIN_OTP_DIGITS = 4
|
||||
const val MAX_OTP_DIGITS = 18
|
||||
|
||||
fun checkBase32Secret(secret: String): Boolean {
|
||||
return (Pattern.matches("^(?:[A-Z2-7]{8})*(?:[A-Z2-7]{2}={6}|[A-Z2-7]{4}={4}|[A-Z2-7]{5}={3}|[A-Z2-7]{7}=)?$", secret))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user