Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
- Added `const` constructor to `SmartAuthSms` and the `@immutable` annotation to `SmartAuthResult`
- Documentation: redesigned README, documented `getAppSignature`, added a 4.x migration note and the version requirements

**Fixes**

- `removeSmsRetrieverApiListener` and `removeUserConsentApiListener` now finish a waiting SMS call as canceled instead of leaving its Future pending forever
- Starting a new SMS call now finishes the previous waiting one as canceled instead of leaving it pending forever
- `getSmsWithRetrieverApi` can now return `SmartAuthResult.canceled`
- `requestPhoneNumberHint` and the SMS calls no longer share a result, so an SMS can't be returned as the phone number

**Android**

- Migrated Android to built-in Kotlin and removed the Kotlin Gradle Plugin classpath from the plugin's `build.gradle` (AGP 9 support)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ if (prepared.hasData) {
await smartAuth.clearCredentialState();
```

- Saving a password or creating a passkey needs a credential provider on the device, such as Google Password Manager with a signed-in Google account. Without one the call fails with `TYPE_NO_CREATE_OPTIONS`, which is common on emulators. Treat it as "not saved", not as a failed sign-in.
- `prepareGetCredential` and `getPreparedCredential` need Android 14 or higher. Below that they fail with `UNSUPPORTED`.
- Only privileged apps can see which credentials `prepareGetCredential` found. The `has*` fields of `SmartAuthPreparedCredential` are `null` for other apps. It still makes the next `getPreparedCredential` faster.

Expand Down
151 changes: 100 additions & 51 deletions android/src/main/kotlin/dev/tornike/smart_auth/SmartAuthPlugin.kt

Large diffs are not rendered by default.

130 changes: 105 additions & 25 deletions example/integration_test/run_sms_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,49 @@ package="dev.tornike.smart_auth_example"
# healthy emulator needs a few seconds.
startup_timeout="${STARTUP_TIMEOUT:-120}"
attempts="${ATTEMPTS:-3}"
sender="5551234"
other_sender="5559876"

# The console treats a newline as a new command, so keep messages on one line.
send_sms() {
adb -s "$device" emu sms send "$1" "$2" </dev/null
}

# adb reads stdin, which here is the test output: without this it would swallow
# the lines the loop below is waiting for.
device_shell() {
adb -s "$device" shell "$@" </dev/null
}

# Taps a button of the consent dialog by its label.
tap_dialog_button() {
device_shell uiautomator dump --windows /sdcard/ui.xml >/dev/null
local coords
coords=$(device_shell cat /sdcard/ui.xml \
| grep -o "text=\"$1\"[^>]*bounds=\"\[[0-9,]*\]\[[0-9,]*\]\"" \
| grep -o '\[[0-9]*,[0-9]*\]\[[0-9]*,[0-9]*\]' | head -n 1 \
| sed 's/\]\[/,/; s/[][]//g' | tr ',' ' ' \
| awk '{print int(($1+$3)/2), int(($2+$4)/2)}' || true)
if [ -n "$coords" ]; then
device_shell input tap $coords
else
echo "Dialog button not found: $1"
fi
}

# Dismisses the phone number hint dialog. Only presses Back while the dialog
# has focus: it can die on its own on CI emulators, and a stray Back would
# then send the app to the background and stall the rest of the run.
dismiss_hint() {
for _ in 1 2 3 4 5; do
if device_shell dumpsys window 2>/dev/null \
| grep -E 'mCurrentFocus|mFocusedWindow' | grep -q PhoneNumberHintActivity; then
device_shell input keyevent KEYCODE_BACK
break
fi
sleep 2
done
}

run_attempt() {
local flag="$1"
Expand Down Expand Up @@ -53,37 +96,74 @@ run_attempt() {
signature="${line##*SMS_SIGNATURE=}"
# Give the retriever time to start listening.
sleep 4
# The console treats a newline as a new command, so keep it on one line.
adb -s "$device" emu sms send 5551234 "<#> Your code is 123456 $signature"
send_sms "$sender" "<#> Your code is 123456 $signature"
;;
*SMS_UNSIGNED_CHECK=*)
signature="${line##*SMS_UNSIGNED_CHECK=}"
sleep 4
send_sms "$sender" "<#> Your code is 111111"
sleep 3
send_sms "$sender" "<#> Your code is 222222 $signature"
;;
*HINT_READY*)
# Dismiss the phone number hint dialog. Only press Back while the dialog
# has focus: it can die on its own on CI emulators, and a stray Back would
# then send the app to the background and stall the rest of the run.
sleep 4
for _ in 1 2 3 4 5; do
if adb -s "$device" shell dumpsys window 2>/dev/null \
| grep -E 'mCurrentFocus|mFocusedWindow' | grep -q PhoneNumberHintActivity; then
adb -s "$device" shell input keyevent KEYCODE_BACK
break
fi
sleep 2
done
dismiss_hint
;;
*HINT_SMS_READY=*)
signature="${line##*HINT_SMS_READY=}"
sleep 4
send_sms "$sender" "<#> Your code is 888888 $signature"
sleep 2
dismiss_hint
;;
*SMS_CONSENT_READY*)
sleep 2
adb -s "$device" emu sms send 5551234 "Your code is 654321"
# Accept the consent dialog.
send_sms "$sender" "Your code is 654321"
sleep 3
tap_dialog_button Allow
;;
*SMS_CONSENT_DENY_READY*)
sleep 2
send_sms "$sender" "Your code is 333333"
sleep 3
tap_dialog_button Deny
;;
*SMS_CONSENT_OTHER_SENDER_READY*)
sleep 2
send_sms "$other_sender" "Your code is 111111"
sleep 3
send_sms "$sender" "Your code is 444444"
sleep 3
tap_dialog_button Allow
;;
*SMS_CONSENT_ANY_SENDER_READY*)
sleep 2
send_sms "$other_sender" "Your code is 555555"
sleep 3
tap_dialog_button Allow
;;
*SMS_CONSENT_ROTATE_READY*)
sleep 2
send_sms "$sender" "Your code is 666666"
sleep 3
device_shell settings put system accelerometer_rotation 0
device_shell settings put system user_rotation 1
sleep 3
# Finish rotating before tapping: each rotation recreates the dialog,
# which can lose a tap that lands around the same time.
device_shell settings put system user_rotation 0
sleep 3
tap_dialog_button Allow
;;
*SMS_CONSENT_BACKGROUND_READY*)
sleep 2
send_sms "$sender" "Your code is 777777"
sleep 3
device_shell input keyevent KEYCODE_HOME
sleep 2
device_shell monkey -p "$package" -c android.intent.category.LAUNCHER 1 >/dev/null
sleep 3
adb -s "$device" shell uiautomator dump --windows /sdcard/ui.xml >/dev/null
coords=$(adb -s "$device" shell cat /sdcard/ui.xml \
| grep -o 'text="Allow"[^>]*bounds="\[[0-9,]*\]\[[0-9,]*\]"' \
| grep -o '\[[0-9]*,[0-9]*\]\[[0-9]*,[0-9]*\]' | head -n 1 \
| sed 's/\]\[/,/; s/[][]//g' | tr ',' ' ' \
| awk '{print int(($1+$3)/2), int(($2+$4)/2)}' || true)
if [ -n "$coords" ]; then
adb -s "$device" shell input tap $coords
fi
tap_dialog_button Allow
;;
esac
done
Expand All @@ -102,7 +182,7 @@ for attempt in $(seq 1 "$attempts"); do
fi
rm -f "$hung"
echo "Attempt $attempt of $attempts: the app did not start, trying again."
adb -s "$device" shell am force-stop "$package" >/dev/null 2>&1 || true
device_shell am force-stop "$package" >/dev/null 2>&1 || true
done
echo "The app did not start after $attempts attempts."
exit 1
147 changes: 147 additions & 0 deletions example/integration_test/smart_auth_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,19 @@ void main() {
expect(result.requireData.code, '123456');
expect(result.requireData.sms, contains(signature));
}, skip: !smsTests);

testWidgets('ignores a message without the app signature', (tester) async {
final signature = (await smartAuth.getAppSignature()).requireData;
// ignore: avoid_print for testing
print('SMS_UNSIGNED_CHECK=$signature');

final result = await smartAuth.getSmsWithRetrieverApi().timeout(
const Duration(seconds: 60),
);

expect(result.hasData, isTrue, reason: '${result.error}');
expect(result.requireData.code, '222222');
}, skip: !smsTests);
});

group('SMS User Consent API', () {
Expand All @@ -273,5 +286,139 @@ void main() {
expect(result.hasData, isTrue, reason: '${result.error}');
expect(result.requireData.code, '654321');
}, skip: !smsTests);

testWidgets('is reported as canceled when the user denies', (tester) async {
// ignore: avoid_print for testing
print('SMS_CONSENT_DENY_READY');
final result = await smartAuth
.getSmsWithUserConsentApi(senderPhoneNumber: _sender)
.timeout(const Duration(seconds: 60));

expect(result.isCanceled, isTrue, reason: '${result.error}');
}, skip: !smsTests);

testWidgets('ignores a message from another sender', (tester) async {
// ignore: avoid_print for testing
print('SMS_CONSENT_OTHER_SENDER_READY');
final result = await smartAuth
.getSmsWithUserConsentApi(senderPhoneNumber: _sender)
.timeout(const Duration(seconds: 60));

expect(result.hasData, isTrue, reason: '${result.error}');
expect(result.requireData.code, '444444');
}, skip: !smsTests);

testWidgets('accepts any sender when none is given', (tester) async {
// ignore: avoid_print for testing
print('SMS_CONSENT_ANY_SENDER_READY');
final result = await smartAuth.getSmsWithUserConsentApi().timeout(
const Duration(seconds: 60),
);

expect(result.hasData, isTrue, reason: '${result.error}');
expect(result.requireData.code, '555555');
}, skip: !smsTests);

testWidgets('survives a rotation while the dialog is open', (tester) async {
// ignore: avoid_print for testing
print('SMS_CONSENT_ROTATE_READY');
final result = await smartAuth
.getSmsWithUserConsentApi(senderPhoneNumber: _sender)
.timeout(const Duration(seconds: 60));

expect(result.hasData, isTrue, reason: '${result.error}');
expect(result.requireData.code, '666666');
}, skip: !smsTests);

testWidgets('survives the app going to the background', (tester) async {
// ignore: avoid_print for testing
print('SMS_CONSENT_BACKGROUND_READY');
final result = await smartAuth
.getSmsWithUserConsentApi(senderPhoneNumber: _sender)
.timeout(const Duration(seconds: 60));

expect(result.hasData, isTrue, reason: '${result.error}');
expect(result.requireData.code, '777777');
}, skip: !smsTests);
});

group('Phone Number Hint with an SMS listener', () {
testWidgets('keeps the two results apart', (tester) async {
final signature = (await smartAuth.getAppSignature()).requireData;
final sms = smartAuth.getSmsWithRetrieverApi();
// ignore: avoid_print for testing
print('HINT_SMS_READY=$signature');
// See the Phone Number Hint group for why this uses runAsync.
final hint = (await tester.runAsync(
() => smartAuth.requestPhoneNumberHint().timeout(
const Duration(seconds: 60),
),
))!;

expect(hint.hasData, isFalse, reason: 'hint got: ${hint.data}');

final result = await sms.timeout(const Duration(seconds: 30));

expect(result.hasData, isTrue, reason: '${result.error}');
expect(result.requireData.code, '888888');
}, skip: !hintTests || !smsTests);
});

// No SMS is sent here. These stay last: a request that is never answered
// stays registered with Play services for 5 minutes, and a stale consent
// request would open a second dialog in the tests above.
group('pending SMS calls', () {
const timeout = Duration(seconds: 10);

tearDown(() async {
await smartAuth.removeSmsRetrieverApiListener();
await smartAuth.removeUserConsentApiListener();
});

testWidgets('removeSmsRetrieverApiListener finishes the call', (
tester,
) async {
final pending = smartAuth.getSmsWithRetrieverApi();
await smartAuth.removeSmsRetrieverApiListener();

final result = await pending.timeout(timeout);

expect(result.isCanceled, isTrue, reason: '${result.error}');
}, skip: !smsTests);

testWidgets('removeUserConsentApiListener finishes the call', (
tester,
) async {
final pending = smartAuth.getSmsWithUserConsentApi();
await smartAuth.removeUserConsentApiListener();

final result = await pending.timeout(timeout);

expect(result.isCanceled, isTrue, reason: '${result.error}');
}, skip: !smsTests);

testWidgets('a second retriever call finishes the first', (tester) async {
final first = smartAuth.getSmsWithRetrieverApi();
final second = smartAuth.getSmsWithRetrieverApi();

final result = await first.timeout(timeout);

expect(result.isCanceled, isTrue, reason: '${result.error}');
await smartAuth.removeSmsRetrieverApiListener();
await second.timeout(timeout);
}, skip: !smsTests);

testWidgets('a consent call finishes a pending retriever call', (
tester,
) async {
final first = smartAuth.getSmsWithRetrieverApi();
final second = smartAuth.getSmsWithUserConsentApi();

final result = await first.timeout(timeout);

expect(result.isCanceled, isTrue, reason: '${result.error}');
await smartAuth.removeUserConsentApiListener();
await second.timeout(timeout);
}, skip: !smsTests);
});
}
Loading
Loading