feat: add password timeout

This commit is contained in:
Andries Smit 2022-03-05 23:33:56 +01:00
parent a8694bb468
commit f50881c1fc
4 changed files with 49 additions and 13 deletions

View file

@ -168,6 +168,7 @@ class MainActivity : NoSplashAppCompatActivity() {
override fun onResume() {
super.onResume()
protectionCheck.resetAuthorization()
if (!isProtectionCheckActive) {
isProtectionCheckActive = true
protectionCheck.queryProtection(this, ProtectionCheck.Protection.APPLICATION, UIRunnable { isProtectionCheckActive = false },

View file

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:validate="http://schemas.android.com/apk/res-auto">
<PreferenceCategory
android:key="@string/key_configbuilder_general_settings"
@ -24,9 +25,8 @@
<EditTextPreference
android:inputType="textPersonName"
android:key="@string/key_patient_name"
android:title="@string/patient_name"
android:summary="@string/patient_name_summary"
/>
android:title="@string/patient_name" />
<PreferenceCategory
android:key="@string/key_protection_settings"
@ -35,8 +35,7 @@
<Preference
android:inputType="textPassword"
android:key="@string/key_master_password"
android:title="@string/master_password"
/>
android:title="@string/master_password" />
<ListPreference
android:defaultValue="0"
@ -83,6 +82,15 @@
android:key="@string/key_bolus_pin"
android:title="@string/bolus_pin" />
<EditTextPreference
android:inputType="number"
android:key="@string/key_protection_timeout"
android:title="@string/protection_timeout_title"
app:defaultValue="0"
validate:maxNumber="180"
validate:minNumber="0"
validate:testType="numeric" />
</PreferenceCategory>
<info.nightscout.androidaps.skins.SkinListPreference

View file

@ -2,16 +2,22 @@ package info.nightscout.androidaps.utils.protection
import androidx.fragment.app.FragmentActivity
import info.nightscout.androidaps.core.R
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.shared.sharedPreferences.SP
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class ProtectionCheck @Inject constructor(
val sp: SP,
val passwordCheck: PasswordCheck
val passwordCheck: PasswordCheck,
val dateUtil: DateUtil
) {
private var lastAuthorization = mutableListOf(0L, 0L, 0L)
private val timeout = TimeUnit.SECONDS.toMillis(sp.getInt(R.string.key_protection_timeout, 0).toLong())
enum class Protection {
PREFERENCES,
APPLICATION,
@ -61,19 +67,38 @@ class ProtectionCheck @Inject constructor(
}
}
fun queryProtection(activity: FragmentActivity, protection: Protection,
ok: Runnable?, cancel: Runnable? = null, fail: Runnable? = null) {
fun resetAuthorization() {
lastAuthorization = mutableListOf(0L, 0L, 0L)
}
private fun activeSession(protection: Protection): Boolean {
val last = lastAuthorization[protection.ordinal]
val diff = dateUtil.now() - last
return diff < timeout
}
private fun onOk(protection: Protection) {
lastAuthorization[protection.ordinal] = dateUtil.now()
}
fun queryProtection(activity: FragmentActivity, protection: Protection, ok: Runnable?, cancel: Runnable? = null, fail: Runnable? = null) {
if (activeSession(protection)) {
onOk(protection)
ok?.run()
return
}
when (ProtectionType.values()[sp.getInt(protectionTypeResourceIDs[protection.ordinal], ProtectionType.NONE.ordinal)]) {
ProtectionType.NONE ->
ok?.run()
ProtectionType.BIOMETRIC ->
BiometricCheck.biometricPrompt(activity, titlePassResourceIDs[protection.ordinal], ok, cancel, fail, passwordCheck)
BiometricCheck.biometricPrompt(activity, titlePassResourceIDs[protection.ordinal], { onOk(protection); ok?.run() }, cancel, fail, passwordCheck)
ProtectionType.MASTER_PASSWORD ->
passwordCheck.queryPassword(activity, R.string.master_password, R.string.key_master_password, { ok?.run() }, { cancel?.run() }, { fail?.run() })
passwordCheck.queryPassword(activity, R.string.master_password, R.string.key_master_password, { onOk(protection); ok?.run() }, { cancel?.run() }, { fail?.run() })
ProtectionType.CUSTOM_PASSWORD ->
passwordCheck.queryPassword(activity, titlePassResourceIDs[protection.ordinal], passwordsResourceIDs[protection.ordinal], { ok?.run() }, { cancel?.run() }, { fail?.run() })
passwordCheck.queryPassword(activity, titlePassResourceIDs[protection.ordinal], passwordsResourceIDs[protection.ordinal], { onOk(protection); ok?.run() }, { cancel?.run() }, { fail?.run() })
ProtectionType.CUSTOM_PIN ->
passwordCheck.queryPassword(activity, titlePinResourceIDs[protection.ordinal], pinsResourceIDs[protection.ordinal], { ok?.run() }, { cancel?.run() }, { fail?.run() }, true)
passwordCheck.queryPassword(activity, titlePinResourceIDs[protection.ordinal], pinsResourceIDs[protection.ordinal], { onOk(protection); ok?.run() }, { cancel?.run() }, { fail?.run() }, true)
}
}
}

View file

@ -12,6 +12,7 @@
<string name="application_pin">Application PIN</string>
<string name="bolus_password">Bolus password</string>
<string name="bolus_pin">Bolus PIN</string>
<string name="protection_timeout_title">Time before Bolus and Settings password / PIN expires (seconds)</string>
<string name="unlock_settings">Unlock settings</string>
<string name="biometric">Biometric</string>
<string name="custom_password">Custom password</string>
@ -43,4 +44,5 @@
<string name="key_settings_protection" translatable="false">settings_protection</string>
<string name="key_application_protection" translatable="false">application_protection</string>
<string name="key_bolus_protection" translatable="false">bolus_protection</string>
<string name="key_protection_timeout" translatable="false">protection_timeout</string>
</resources>