Skip to content

Commit 80e4f36

Browse files
authored
Fix ephemeral intent flags on Android (#180)
* Fix ephemeral intent flags on Android - Update androidx.browser dependency to stable 1.9.0 - Implement setEphemeralBrowsingEnabled() for ephemeral browsing - Add proper preferEphemeral option handling in AuthTab intent - Fixes issue where credentials were remembered despite ephemeral flags * fix ephemeral intent flags on Android * Revert minSdk change in example app * fix chrome crash * fix chrome * Readme + documentation update
1 parent fb660cb commit 80e4f36

5 files changed

Lines changed: 25 additions & 5 deletions

File tree

flutter_web_auth_2/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ final response = await http.post(url, body: {
8080
final accessToken = jsonDecode(response.body)['access_token'] as String;
8181
```
8282

83-
**Note:** To use multiple scopes with Google, you need to encode them as a single string, separated by spaces (`%20`). For example, `scope: 'email https://www.googleapis.com/auth/userinfo.profile'`. Here is [a list of all supported scopes](https://developers.google.com/identity/protocols/oauth2/scopes).
83+
**Note (Google multiple scopes):** To use multiple scopes with Google, you need to encode them as a single string, separated by spaces (`%20`). For example, `scope: 'email https://www.googleapis.com/auth/userinfo.profile'`. Here is [a list of all supported scopes](https://developers.google.com/identity/protocols/oauth2/scopes).
84+
85+
**Note (ephemeral auth):** Due to a [knownw Chrome bug](https://issuetracker.google.com/issues/444173718), when setting `preferEphemeral: true` on Android, the webview will crash on older Chrome version (minimum version required is [Chrome 141](https://developer.chrome.com/release-notes/141?hl=en)).
8486

8587
## Migration
8688

flutter_web_auth_2/android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ android {
4949
}
5050

5151
dependencies {
52-
implementation 'androidx.browser:browser:1.9.0-alpha01'
52+
implementation 'androidx.browser:browser:1.9.0'
5353
implementation 'androidx.activity:activity-ktx:1.10.1'
5454
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
5555
}

flutter_web_auth_2/android/src/main/kotlin/com/linusu/flutter_web_auth_2/AuthenticationManagementActivity.kt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class AuthenticationManagementActivity : ComponentActivity() {
1717
const val KEY_AUTH_URI: String = "authUri"
1818
const val KEY_AUTH_OPTION_INTENT_FLAGS: String = "authOptionsIntentFlags"
1919
const val KEY_AUTH_OPTION_TARGET_PACKAGE: String = "authOptionsTargetPackage"
20+
const val KEY_AUTH_OPTION_PREFER_EPHEMERAL: String = "authOptionsPreferEphemeral"
2021
const val KEY_AUTH_CALLBACK_SCHEME: String = "authCallbackScheme"
2122
const val KEY_AUTH_CALLBACK_HOST: String = "authCallbackHost"
2223
const val KEY_AUTH_CALLBACK_PATH: String = "authCallbackPath"
@@ -32,6 +33,7 @@ class AuthenticationManagementActivity : ComponentActivity() {
3233
private lateinit var authenticationUri: Uri
3334
private var intentFlags: Int = 0
3435
private var targetPackage: String? = null
36+
private var preferEphemeral: Boolean = false
3537
private lateinit var callbackScheme: String
3638
private var callbackHost: String? = null
3739
private var callbackPath: String? = null
@@ -78,7 +80,20 @@ class AuthenticationManagementActivity : ComponentActivity() {
7880
super.onResume()
7981

8082
if (!authStarted) {
81-
val intent = AuthTabIntent.Builder().build()
83+
val intentBuilder = AuthTabIntent.Builder()
84+
85+
// Set ephemeral browsing if requested and supported
86+
if (preferEphemeral) {
87+
try {
88+
intentBuilder.setEphemeralBrowsingEnabled(true)
89+
Log.d("flutter_web_auth_2", "Ephemeral browsing enabled")
90+
} catch (e: Exception) {
91+
Log.w("flutter_web_auth_2", "Failed to enable ephemeral browsing: ${e.message}")
92+
}
93+
}
94+
95+
val intent = intentBuilder.build()
96+
8297
intent.intent.addFlags(intentFlags)
8398

8499
if (targetPackage != null) {
@@ -112,6 +127,7 @@ class AuthenticationManagementActivity : ComponentActivity() {
112127
KEY_AUTH_OPTION_TARGET_PACKAGE,
113128
targetPackage,
114129
)
130+
outState.putBoolean(KEY_AUTH_OPTION_PREFER_EPHEMERAL, preferEphemeral)
115131
outState.putString(KEY_AUTH_CALLBACK_SCHEME, callbackScheme)
116132
outState.putString(KEY_AUTH_CALLBACK_HOST, callbackHost)
117133
outState.putString(KEY_AUTH_CALLBACK_PATH, callbackPath)
@@ -131,6 +147,7 @@ class AuthenticationManagementActivity : ComponentActivity() {
131147
} ?: throw IllegalStateException("Authentication URI is null")
132148
intentFlags = state.getInt(KEY_AUTH_OPTION_INTENT_FLAGS, 0)
133149
targetPackage = state.getString(KEY_AUTH_OPTION_TARGET_PACKAGE)
150+
preferEphemeral = state.getBoolean(KEY_AUTH_OPTION_PREFER_EPHEMERAL, false)
134151
callbackScheme = state.getString(KEY_AUTH_CALLBACK_SCHEME)!!
135152
callbackHost = state.getString(KEY_AUTH_CALLBACK_HOST)
136153
callbackPath = state.getString(KEY_AUTH_CALLBACK_PATH)

flutter_web_auth_2/android/src/main/kotlin/com/linusu/flutter_web_auth_2/FlutterWebAuth2Plugin.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class FlutterWebAuth2Plugin(
5656
putExtra(AuthenticationManagementActivity.KEY_AUTH_CALLBACK_SCHEME, callbackUrlScheme)
5757
putExtra(AuthenticationManagementActivity.KEY_AUTH_CALLBACK_HOST, options["httpsHost"] as String?)
5858
putExtra(AuthenticationManagementActivity.KEY_AUTH_CALLBACK_PATH, options["httpsPath"] as String?)
59+
putExtra(AuthenticationManagementActivity.KEY_AUTH_OPTION_PREFER_EPHEMERAL, options["preferEphemeral"] as Boolean? ?: false)
5960
})
6061
}
6162

flutter_web_auth_2/lib/src/options.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ class FlutterWebAuth2Options {
8585
customTabsPackageOrder: json['customTabsPackageOrder'],
8686
);
8787

88-
/// **Only has an effect on iOS and macOS!**
88+
/// **Only has an effect on iOS, Android and macOS!**
8989
/// If this is `true`, an ephemeral web browser session
9090
/// will be used where possible (`prefersEphemeralWebBrowserSession`).
91-
/// For Android devices, see [intentFlags].
91+
/// For Android devices (for supporting older Chrome than 141) alternatively you can see [intentFlags].
9292
final bool preferEphemeral;
9393

9494
/// **Only has an effect on Web!**

0 commit comments

Comments
 (0)