Merge pull request #48 from dlvoy/sms-communicator-config-fixes

Sms communicator config fixes
This commit is contained in:
Milos Kozak 2020-10-25 20:54:20 +01:00 committed by GitHub
commit 02b94e9109
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 5 deletions

View file

@ -1004,7 +1004,7 @@ class SmsCommunicatorPlugin @Inject constructor(
private fun areMoreNumbers(allowednumbers: String?): Boolean {
return allowednumbers?.let {
var countNumbers = 0
val knownNumbers = HashSet<String>()
val substrings = it.split(";").toTypedArray()
for (number in substrings) {
var cleaned = number.replace(Regex("\\s+"), "")
@ -1012,9 +1012,9 @@ class SmsCommunicatorPlugin @Inject constructor(
cleaned = cleaned.replace("+", "")
cleaned = cleaned.replace("-", "")
if (!cleaned.matches(Regex("[0-9]+"))) continue
countNumbers++
knownNumbers.add(cleaned)
}
countNumbers > 1
knownNumbers.size > 1
} ?: false
}
}

View file

@ -1,11 +1,15 @@
package info.nightscout.androidaps.plugins.general.smsCommunicator.activities
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.res.Resources
import android.graphics.Color
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.view.WindowManager
import com.google.common.primitives.Ints.min
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel
import info.nightscout.androidaps.R
@ -15,8 +19,8 @@ import info.nightscout.androidaps.plugins.general.smsCommunicator.SmsCommunicato
import info.nightscout.androidaps.plugins.general.smsCommunicator.otp.OneTimePassword
import info.nightscout.androidaps.plugins.general.smsCommunicator.otp.OneTimePasswordValidationResult
import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.alertDialogs.OKDialog
import info.nightscout.androidaps.utils.ToastUtils
import info.nightscout.androidaps.utils.alertDialogs.OKDialog
import info.nightscout.androidaps.utils.resources.ResourceHelper
import kotlinx.android.synthetic.main.activity_smscommunicator_otp.*
import net.glxn.qrgen.android.QRCode
@ -31,6 +35,7 @@ class SmsCommunicatorOtpActivity : NoSplashAppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
setContentView(R.layout.activity_smscommunicator_otp)
smscommunicator_otp_verify_edit.addTextChangedListener(object : TextWatcher {
@ -64,9 +69,23 @@ class SmsCommunicatorOtpActivity : NoSplashAppCompatActivity() {
Runnable {
otp.ensureKey(true)
updateGui()
ToastUtils.showToastInUiThread(this, R.string.smscommunicator_otp_reset_successful)
ToastUtils.Long.infoToast(this, resourceHelper.gs(R.string.smscommunicator_otp_reset_successful))
})
}
smscommunicator_otp_provisioning.setOnLongClickListener {
OKDialog.showConfirmation(this,
resourceHelper.gs(R.string.smscommunicator_otp_export_title),
resourceHelper.gs(R.string.smscommunicator_otp_export_prompt),
Runnable {
val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText("OTP Secret", otp.provisioningSecret())
clipboard.primaryClip = clip
ToastUtils.Long.infoToast(this, resourceHelper.gs(R.string.smscommunicator_otp_export_successful))
})
true
}
}
@Synchronized

View file

@ -121,4 +121,10 @@ class OneTimePassword @Inject constructor(
fun provisioningURI(): String? =
key?.let { "otpauth://totp/AndroidAPS:" + URLEncoder.encode(name(), "utf-8").replace("+", "%20") + "?secret=" + BaseEncoding.base32().encode(it.encoded).replace("=", "") + "&issuer=AndroidAPS" }
/**
* Return secret used to provision Authenticator apps, in Base32 format
*/
fun provisioningSecret(): String? =
key?.let { BaseEncoding.base32().encode(it.encoded).replace("=", "") }
}

View file

@ -1376,6 +1376,10 @@
<string name="smscommunicator_otp_reset_prompt">Are you sure to reset Authenticator key? It will render all currently configured Authenticators invalid, and you will need to set them up again.</string>
<string name="smscommunicator_otp_reset_successful">New Authenticator Key was generated! Please use updated QRCode to provision authenticators.</string>
<string name="smscommunicator_otp_export_title">Exporting OTP secret</string>
<string name="smscommunicator_otp_export_prompt">Are you sure you want to copy OTP secret to clipboard?\n\nYou may only need that if your authenticator app have issues scanning QRCode, you want to enter it manually or you want to configure hardware OTP token using dedicated app.</string>
<string name="smscommunicator_otp_export_successful">OTP secret (in Base32 format) exported and copied into clipboard. Paste it into authenticator or hardware OTP burner!</string>
<string name="smscommunicator_otp_step1_install_header">1. Install Authenticator</string>
<string name="smscommunicator_otp_step2_provisioning_header">2. Scan code to setup AndroidAPS OTP codes</string>
<string name="smscommunicator_otp_step3_test_header">3. Test One-Time-Password</string>

View file

@ -7,9 +7,13 @@ class MultiPhoneValidator(val _customErrorMessage: String?) : Validator(_customE
override fun isValid(editText: EditText): Boolean {
val substrings = editText.text.split(";").toTypedArray()
val knownNumbers = HashSet<String>()
for (number in substrings) {
if (!PatternValidator(_customErrorMessage, Patterns.PHONE).isValid(number))
return false
if (knownNumbers.contains(number))
return false
knownNumbers.add(number)
}
return substrings.isNotEmpty()
}