Skip to content

Commit

Permalink
fix: com.google.android.gms.common.api.ApiException: 16 (#84)
Browse files Browse the repository at this point in the history
## Checklist

- [x] Have you added an explanation of what your changes do and why
you'd like us to include them?
- [x] Is there an existing issue for this PR?
- _link issue here_ (use keywords like `fix`, `close`, `resolve` etc. if
necessary)
- [x] Have the files been linted and formatted?
- [x] Have the docs been updated to match the changes in the PR?
- [x] Have the tests been updated to match the changes in the PR?
- [ ] Attached videos/screenshots demonstrating the fix/feature.
- [x] Have you run the tests locally to confirm they pass?

## Changes
added verification that the result of the credentialPickerRequest
request came from the SMS code confirmation picker

---------

Co-authored-by: Roman Reed <[email protected]>
  • Loading branch information
DmitrDomrachev and romreed authored Oct 8, 2024
1 parent d070e5e commit a9fc307
Showing 1 changed file with 49 additions and 26 deletions.
75 changes: 49 additions & 26 deletions android/src/main/kotlin/ru/surfstudio/otp_autofill/OTPPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ const val getAppSignatureMethod: String = "getAppSignature"
const val senderTelephoneNumber: String = "senderTelephoneNumber"

/** OtpTextEditControllerPlugin */
class OTPPlugin : FlutterPlugin, MethodCallHandler, PluginRegistry.ActivityResultListener, ActivityAware {
class OTPPlugin : FlutterPlugin, MethodCallHandler, PluginRegistry.ActivityResultListener,
ActivityAware {

private lateinit var channel: MethodChannel

Expand All @@ -60,30 +61,35 @@ class OTPPlugin : FlutterPlugin, MethodCallHandler, PluginRegistry.ActivityResul
startListenRetriever -> {
listenRetriever(result)
}

startListenUserConsent -> {
listenUserConsent(call, result)
}

getTelephoneHint -> {
showNumberHint(result)
}

stopListenForCode -> {
unRegisterBroadcastReceivers()
result.success(true)
}

getAppSignatureMethod -> {
if (activity != null) {
val signature = AppSignatureHelper(this.activity!!).getAppSignatures()[0]
result.success(signature)
} else result.success(null)
}

else -> result.notImplemented()
}
}

private fun showNumberHint(result: Result) {
lastResult = result

if(activity == null) return
if (activity == null) return

// if activity is not null will build 'show hint' intent
// on success will start showing hint
Expand All @@ -93,8 +99,10 @@ class OTPPlugin : FlutterPlugin, MethodCallHandler, PluginRegistry.ActivityResul
res.intentSender
val request = Builder(res).build()

activity!!.startIntentSenderForResult(request.intentSender, credentialPickerRequest,
null, 0, 0, 0)
activity!!.startIntentSenderForResult(
request.intentSender, credentialPickerRequest,
null, 0, 0, 0
)
}
}

Expand All @@ -117,11 +125,19 @@ class OTPPlugin : FlutterPlugin, MethodCallHandler, PluginRegistry.ActivityResul
} else {
// Consent denied. User can type OTC manually.
}

credentialPickerRequest -> if (resultCode == Activity.RESULT_OK && data != null) {
val phoneNumber =
Identity.getSignInClient(context!!).getPhoneNumberFromIntent(data)
lastResult?.success(phoneNumber)
lastResult = null
// Check if the result is for credential picker
if (data.hasExtra(SmsRetriever.EXTRA_SMS_MESSAGE)) {
// This is a result from the SMS consent picker
val phoneNumber =
Identity.getSignInClient(context!!).getPhoneNumberFromIntent(data)
lastResult?.success(phoneNumber)
lastResult = null
} else {
lastResult?.error("403", "User denied consent", null)
lastResult = null
}
}
}
return true
Expand Down Expand Up @@ -155,16 +171,22 @@ class OTPPlugin : FlutterPlugin, MethodCallHandler, PluginRegistry.ActivityResul

private fun registerSmsUserConsentBroadcastReceiver() {
smsUserConsentBroadcastReceiver = SmsUserConsentReceiver().also {
it.smsBroadcastReceiverListener = object : SmsUserConsentReceiver.SmsUserConsentBroadcastReceiverListener {
override fun onSuccess(intent: Intent?) {
intent?.let { context -> activity?.startActivityForResult(context, smsConsentRequest) }
}
it.smsBroadcastReceiverListener =
object : SmsUserConsentReceiver.SmsUserConsentBroadcastReceiverListener {
override fun onSuccess(intent: Intent?) {
intent?.let { context ->
activity?.startActivityForResult(
context,
smsConsentRequest
)
}
}

override fun onFailure() {
lastResult?.error("408", "Timeout exception", null)
lastResult = null
override fun onFailure() {
lastResult?.error("408", "Timeout exception", null)
lastResult = null
}
}
}
}

val intentFilter = IntentFilter(SmsRetriever.SMS_RETRIEVED_ACTION)
Expand All @@ -189,19 +211,20 @@ class OTPPlugin : FlutterPlugin, MethodCallHandler, PluginRegistry.ActivityResul

private fun registerSmsRetrieverBroadcastReceiver() {
smsRetrieverBroadcastReceiver = SmsRetrieverReceiver().also {
it.smsBroadcastReceiverListener = object : SmsRetrieverReceiver.SmsRetrieverBroadcastReceiverListener {
override fun onSuccess(sms: String?) {
sms?.let { it ->
lastResult?.success(it)
lastResult = null
it.smsBroadcastReceiverListener =
object : SmsRetrieverReceiver.SmsRetrieverBroadcastReceiverListener {
override fun onSuccess(sms: String?) {
sms?.let { it ->
lastResult?.success(it)
lastResult = null
}
}
}

override fun onFailure() {
lastResult?.error("408", "Timeout exception", null)
lastResult = null
override fun onFailure() {
lastResult?.error("408", "Timeout exception", null)
lastResult = null
}
}
}
}

val intentFilter = IntentFilter(SmsRetriever.SMS_RETRIEVED_ACTION)
Expand Down

0 comments on commit a9fc307

Please sign in to comment.