From cc9983e31034097ce4080070f9bbff9f0777b180 Mon Sep 17 00:00:00 2001 From: Prince Mathew Date: Mon, 8 Jun 2026 12:14:15 +0530 Subject: [PATCH 1/8] fix : Handling DPoP enabled WebAuth flow after process death --- .../provider/AuthenticationActivity.kt | 2 +- .../auth0/android/provider/OAuthManager.kt | 15 +++- .../android/provider/OAuthManagerState.kt | 9 +- .../auth0/android/provider/WebAuthProvider.kt | 5 +- .../android/provider/OAuthManagerStateTest.kt | 84 +++++++++++++++++++ .../android/provider/WebAuthProviderTest.kt | 22 +++++ 6 files changed, 126 insertions(+), 11 deletions(-) diff --git a/auth0/src/main/java/com/auth0/android/provider/AuthenticationActivity.kt b/auth0/src/main/java/com/auth0/android/provider/AuthenticationActivity.kt index 5f3c3f4e6..b0f413cc6 100644 --- a/auth0/src/main/java/com/auth0/android/provider/AuthenticationActivity.kt +++ b/auth0/src/main/java/com/auth0/android/provider/AuthenticationActivity.kt @@ -40,7 +40,7 @@ public open class AuthenticationActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) if (savedInstanceState != null) { - WebAuthProvider.onRestoreInstanceState(savedInstanceState) + WebAuthProvider.onRestoreInstanceState(savedInstanceState, this) intentLaunched = savedInstanceState.getBoolean(EXTRA_INTENT_LAUNCHED, false) } } diff --git a/auth0/src/main/java/com/auth0/android/provider/OAuthManager.kt b/auth0/src/main/java/com/auth0/android/provider/OAuthManager.kt index acc2b2864..718d3a788 100644 --- a/auth0/src/main/java/com/auth0/android/provider/OAuthManager.kt +++ b/auth0/src/main/java/com/auth0/android/provider/OAuthManager.kt @@ -29,6 +29,7 @@ internal class OAuthManager( @get:VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) internal val dPoP: DPoP? = null ) : ResumableManager() { + private val parameters: MutableMap private val headers: MutableMap private val ctOptions: CustomTabsOptions @@ -211,7 +212,8 @@ internal class OAuthManager( auth0 = account, idTokenVerificationIssuer = idTokenVerificationIssuer, idTokenVerificationLeeway = idTokenVerificationLeeway, - customAuthorizeUrl = this.customAuthorizeUrl + customAuthorizeUrl = this.customAuthorizeUrl, + dPoPEnabled = dPoP != null ) } @@ -387,14 +389,21 @@ internal class OAuthManager( internal fun OAuthManager.Companion.fromState( state: OAuthManagerState, - callback: Callback + callback: Callback, + context: Context ): OAuthManager { + // Enable DPoP on the restored PKCE's AuthenticationAPIClient so that + // the token exchange request includes the DPoP proof after process restore. + if (state.dPoPEnabled && state.pkce != null) { + state.pkce.apiClient.useDPoP(context) + } return OAuthManager( account = state.auth0, ctOptions = state.ctOptions, parameters = state.parameters, callback = callback, - customAuthorizeUrl = state.customAuthorizeUrl + customAuthorizeUrl = state.customAuthorizeUrl, + dPoP = if (state.dPoPEnabled ) DPoP(context) else null ).apply { setHeaders( state.headers diff --git a/auth0/src/main/java/com/auth0/android/provider/OAuthManagerState.kt b/auth0/src/main/java/com/auth0/android/provider/OAuthManagerState.kt index ab677af6c..06d0c6b0b 100644 --- a/auth0/src/main/java/com/auth0/android/provider/OAuthManagerState.kt +++ b/auth0/src/main/java/com/auth0/android/provider/OAuthManagerState.kt @@ -6,7 +6,6 @@ import android.util.Base64 import androidx.core.os.ParcelCompat import com.auth0.android.Auth0 import com.auth0.android.authentication.AuthenticationAPIClient -import com.auth0.android.dpop.DPoP import com.auth0.android.request.internal.GsonProvider import com.google.gson.Gson @@ -20,7 +19,7 @@ internal data class OAuthManagerState( val idTokenVerificationLeeway: Int?, val idTokenVerificationIssuer: String?, val customAuthorizeUrl: String? = null, - val dPoP: DPoP? = null + val dPoPEnabled: Boolean = false ) { private class OAuthManagerJson( @@ -37,7 +36,7 @@ internal data class OAuthManagerState( val idTokenVerificationLeeway: Int?, val idTokenVerificationIssuer: String?, val customAuthorizeUrl: String? = null, - val dPoP: DPoP? = null + val dPoPEnabled: Boolean ) fun serializeToJson( @@ -62,7 +61,7 @@ internal data class OAuthManagerState( idTokenVerificationIssuer = idTokenVerificationIssuer, idTokenVerificationLeeway = idTokenVerificationLeeway, customAuthorizeUrl = this.customAuthorizeUrl, - dPoP = this.dPoP + dPoPEnabled = this.dPoPEnabled ) return gson.toJson(json) } finally { @@ -112,7 +111,7 @@ internal data class OAuthManagerState( idTokenVerificationIssuer = oauthManagerJson.idTokenVerificationIssuer, idTokenVerificationLeeway = oauthManagerJson.idTokenVerificationLeeway, customAuthorizeUrl = oauthManagerJson.customAuthorizeUrl, - dPoP = oauthManagerJson.dPoP + dPoPEnabled = oauthManagerJson.dPoPEnabled ) } finally { parcel.recycle() diff --git a/auth0/src/main/java/com/auth0/android/provider/WebAuthProvider.kt b/auth0/src/main/java/com/auth0/android/provider/WebAuthProvider.kt index 6d647a432..a668e0704 100644 --- a/auth0/src/main/java/com/auth0/android/provider/WebAuthProvider.kt +++ b/auth0/src/main/java/com/auth0/android/provider/WebAuthProvider.kt @@ -142,7 +142,7 @@ public object WebAuthProvider : SenderConstraining { } } - internal fun onRestoreInstanceState(bundle: Bundle) { + internal fun onRestoreInstanceState(bundle: Bundle, context: Context) { if (managerInstance == null) { val oauthStateJson = bundle.getString(KEY_BUNDLE_OAUTH_MANAGER_STATE).orEmpty() val parStateJson = bundle.getString(KEY_BUNDLE_PAR_MANAGER_STATE).orEmpty() @@ -162,7 +162,8 @@ public object WebAuthProvider : SenderConstraining { callback.onFailure(error) } } - } + }, + context ) } else if (parStateJson.isNotBlank()) { val state = PARCodeManagerState.deserializeState(parStateJson) diff --git a/auth0/src/test/java/com/auth0/android/provider/OAuthManagerStateTest.kt b/auth0/src/test/java/com/auth0/android/provider/OAuthManagerStateTest.kt index e4ac82383..0543e0a47 100644 --- a/auth0/src/test/java/com/auth0/android/provider/OAuthManagerStateTest.kt +++ b/auth0/src/test/java/com/auth0/android/provider/OAuthManagerStateTest.kt @@ -44,4 +44,88 @@ internal class OAuthManagerStateTest { Assert.assertEquals(1, deserializedState.idTokenVerificationLeeway) Assert.assertEquals("issuer", deserializedState.idTokenVerificationIssuer) } + + @Test + fun `serialize should persist dPoPEnabled flag as true`() { + val auth0 = Auth0.getInstance("clientId", "domain") + val state = OAuthManagerState( + auth0 = auth0, + parameters = mapOf("param1" to "value1"), + headers = mapOf("header1" to "value1"), + requestCode = 1, + ctOptions = CustomTabsOptions.newBuilder() + .showTitle(true) + .withBrowserPicker( + BrowserPicker.newBuilder().withAllowedPackages(emptyList()).build() + ) + .build(), + pkce = PKCE(mock(), "redirectUri", mapOf("header1" to "value1")), + idTokenVerificationLeeway = 1, + idTokenVerificationIssuer = "issuer", + dPoPEnabled = true + ) + + val json = state.serializeToJson() + + Assert.assertTrue(json.isNotBlank()) + Assert.assertTrue(json.contains("\"dPoPEnabled\":true")) + + val deserializedState = OAuthManagerState.deserializeState(json) + + Assert.assertTrue(deserializedState.dPoPEnabled) + } + + @Test + fun `serialize should persist dPoPEnabled flag as false by default`() { + val auth0 = Auth0.getInstance("clientId", "domain") + val state = OAuthManagerState( + auth0 = auth0, + parameters = mapOf("param1" to "value1"), + headers = mapOf("header1" to "value1"), + requestCode = 1, + ctOptions = CustomTabsOptions.newBuilder() + .showTitle(true) + .withBrowserPicker( + BrowserPicker.newBuilder().withAllowedPackages(emptyList()).build() + ) + .build(), + pkce = PKCE(mock(), "redirectUri", mapOf("header1" to "value1")), + idTokenVerificationLeeway = 1, + idTokenVerificationIssuer = "issuer" + ) + + val json = state.serializeToJson() + + val deserializedState = OAuthManagerState.deserializeState(json) + + Assert.assertFalse(deserializedState.dPoPEnabled) + } + + @Test + fun `deserialize should default dPoPEnabled to false when field is missing from JSON`() { + val auth0 = Auth0.getInstance("clientId", "domain") + val state = OAuthManagerState( + auth0 = auth0, + parameters = emptyMap(), + headers = emptyMap(), + requestCode = 0, + ctOptions = CustomTabsOptions.newBuilder() + .showTitle(true) + .withBrowserPicker( + BrowserPicker.newBuilder().withAllowedPackages(emptyList()).build() + ) + .build(), + pkce = PKCE(mock(), "redirectUri", emptyMap()), + idTokenVerificationLeeway = null, + idTokenVerificationIssuer = null + ) + + val json = state.serializeToJson() + // Remove the dPoPEnabled field to simulate legacy JSON + val legacyJson = json.replace(",\"dPoPEnabled\":false", "") + + val deserializedState = OAuthManagerState.deserializeState(legacyJson) + + Assert.assertFalse(deserializedState.dPoPEnabled) + } } diff --git a/auth0/src/test/java/com/auth0/android/provider/WebAuthProviderTest.kt b/auth0/src/test/java/com/auth0/android/provider/WebAuthProviderTest.kt index e033aa8df..db18e6565 100644 --- a/auth0/src/test/java/com/auth0/android/provider/WebAuthProviderTest.kt +++ b/auth0/src/test/java/com/auth0/android/provider/WebAuthProviderTest.kt @@ -4,6 +4,7 @@ import android.app.Activity import android.content.Context import android.content.Intent import android.net.Uri +import android.os.Bundle import android.os.Parcelable import androidx.test.espresso.intent.matcher.IntentMatchers import androidx.test.espresso.intent.matcher.UriMatchers @@ -2958,6 +2959,27 @@ public class WebAuthProviderTest { mockAPI.shutdown() } + @Test + public fun shouldReEnableDPoPOnOAuthManagerAfterProcessDeathRestore() { + `when`(mockKeyStore.hasKeyPair()).thenReturn(true) + `when`(mockKeyStore.getKeyPair()).thenReturn(Pair(mock(), FakeECPublicKey())) + + WebAuthProvider.useDPoP(mockContext) + .login(account) + .start(activity, callback) + + val bundle = Bundle() + WebAuthProvider.onSaveInstanceState(bundle) + + // Simulate the host process being killed and recreated: the manager instance is gone, + // and the activity is recreated with the saved state. + WebAuthProvider.resetManagerInstance() + WebAuthProvider.onRestoreInstanceState(bundle, activity) + + val restoredManager = WebAuthProvider.managerInstance as OAuthManager + assertThat(restoredManager.dPoP, `is`(notNullValue())) + } + //** ** ** ** ** ** **// //** ** ** ** ** ** **// //** Helpers Functions**// From caa3b5fbcf9116fb86d4b36fa3b3c6a578011069 Mon Sep 17 00:00:00 2001 From: Prince Mathew Date: Mon, 8 Jun 2026 14:59:40 +0530 Subject: [PATCH 2/8] Addressed review comments --- .../auth0/android/provider/OAuthManager.kt | 4 +- .../android/provider/OAuthManagerState.kt | 2 +- .../android/provider/OAuthManagerStateTest.kt | 58 +++++++++++++++++++ .../android/provider/WebAuthProviderTest.kt | 4 ++ 4 files changed, 65 insertions(+), 3 deletions(-) diff --git a/auth0/src/main/java/com/auth0/android/provider/OAuthManager.kt b/auth0/src/main/java/com/auth0/android/provider/OAuthManager.kt index 718d3a788..242a17335 100644 --- a/auth0/src/main/java/com/auth0/android/provider/OAuthManager.kt +++ b/auth0/src/main/java/com/auth0/android/provider/OAuthManager.kt @@ -394,7 +394,7 @@ internal fun OAuthManager.Companion.fromState( ): OAuthManager { // Enable DPoP on the restored PKCE's AuthenticationAPIClient so that // the token exchange request includes the DPoP proof after process restore. - if (state.dPoPEnabled && state.pkce != null) { + if (state.dPoPEnabled && state.pkce != null) { state.pkce.apiClient.useDPoP(context) } return OAuthManager( @@ -403,7 +403,7 @@ internal fun OAuthManager.Companion.fromState( parameters = state.parameters, callback = callback, customAuthorizeUrl = state.customAuthorizeUrl, - dPoP = if (state.dPoPEnabled ) DPoP(context) else null + dPoP = if (state.dPoPEnabled) DPoP(context) else null ).apply { setHeaders( state.headers diff --git a/auth0/src/main/java/com/auth0/android/provider/OAuthManagerState.kt b/auth0/src/main/java/com/auth0/android/provider/OAuthManagerState.kt index 06d0c6b0b..9f10bb8e5 100644 --- a/auth0/src/main/java/com/auth0/android/provider/OAuthManagerState.kt +++ b/auth0/src/main/java/com/auth0/android/provider/OAuthManagerState.kt @@ -36,7 +36,7 @@ internal data class OAuthManagerState( val idTokenVerificationLeeway: Int?, val idTokenVerificationIssuer: String?, val customAuthorizeUrl: String? = null, - val dPoPEnabled: Boolean + val dPoPEnabled: Boolean = false ) fun serializeToJson( diff --git a/auth0/src/test/java/com/auth0/android/provider/OAuthManagerStateTest.kt b/auth0/src/test/java/com/auth0/android/provider/OAuthManagerStateTest.kt index 0543e0a47..21ac39659 100644 --- a/auth0/src/test/java/com/auth0/android/provider/OAuthManagerStateTest.kt +++ b/auth0/src/test/java/com/auth0/android/provider/OAuthManagerStateTest.kt @@ -1,8 +1,16 @@ package com.auth0.android.provider +import android.content.Context import android.graphics.Color import com.auth0.android.Auth0 +import com.auth0.android.authentication.AuthenticationAPIClient +import com.auth0.android.authentication.AuthenticationException +import com.auth0.android.callback.Callback +import com.auth0.android.result.Credentials import com.nhaarman.mockitokotlin2.mock +import com.nhaarman.mockitokotlin2.whenever +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.core.Is.`is` import org.junit.Assert import org.junit.Test import org.junit.runner.RunWith @@ -128,4 +136,54 @@ internal class OAuthManagerStateTest { Assert.assertFalse(deserializedState.dPoPEnabled) } + + @Test + fun `fromState should re-enable DPoP on the restored PKCE's API client when dPoPEnabled is true`() { + val context = mock() + whenever(context.applicationContext).thenReturn(context) + val auth0 = Auth0.getInstance("clientId", "domain") + val apiClient = AuthenticationAPIClient(auth0) + val state = OAuthManagerState( + auth0 = auth0, + parameters = emptyMap(), + headers = emptyMap(), + requestCode = 0, + ctOptions = CustomTabsOptions.newBuilder().build(), + pkce = PKCE(apiClient, "codeVerifier", "redirectUri", "codeChallenge", emptyMap()), + idTokenVerificationLeeway = null, + idTokenVerificationIssuer = null, + dPoPEnabled = true + ) + val callback = mock>() + + OAuthManager.fromState(state, callback, context) + + // This is the actual regression guard: the token exchange after process death only + // includes the DPoP proof because fromState re-enables DPoP on the restored API client. + assertThat(apiClient.isDPoPEnabled, `is`(true)) + } + + @Test + fun `fromState should not enable DPoP on the restored PKCE's API client when dPoPEnabled is false`() { + val context = mock() + whenever(context.applicationContext).thenReturn(context) + val auth0 = Auth0.getInstance("clientId", "domain") + val apiClient = AuthenticationAPIClient(auth0) + val state = OAuthManagerState( + auth0 = auth0, + parameters = emptyMap(), + headers = emptyMap(), + requestCode = 0, + ctOptions = CustomTabsOptions.newBuilder().build(), + pkce = PKCE(apiClient, "codeVerifier", "redirectUri", "codeChallenge", emptyMap()), + idTokenVerificationLeeway = null, + idTokenVerificationIssuer = null, + dPoPEnabled = false + ) + val callback = mock>() + + OAuthManager.fromState(state, callback, context) + + assertThat(apiClient.isDPoPEnabled, `is`(false)) + } } diff --git a/auth0/src/test/java/com/auth0/android/provider/WebAuthProviderTest.kt b/auth0/src/test/java/com/auth0/android/provider/WebAuthProviderTest.kt index db18e6565..dd195dbf2 100644 --- a/auth0/src/test/java/com/auth0/android/provider/WebAuthProviderTest.kt +++ b/auth0/src/test/java/com/auth0/android/provider/WebAuthProviderTest.kt @@ -2977,6 +2977,10 @@ public class WebAuthProviderTest { WebAuthProvider.onRestoreInstanceState(bundle, activity) val restoredManager = WebAuthProvider.managerInstance as OAuthManager + // This asserts the save/restore wiring reconstructs a DPoP-enabled manager. The actual + // regression guard — that DPoP is re-enabled on the restored PKCE's API client so the + // token exchange carries the proof — lives in OAuthManagerStateTest.fromState tests, + // since OAuthManager.pkce is private and not reachable here without reflection. assertThat(restoredManager.dPoP, `is`(notNullValue())) } From cee8fb1589acbd5dbcb3ebf5e056164e7891c315 Mon Sep 17 00:00:00 2001 From: Prince Mathew Date: Mon, 8 Jun 2026 12:14:15 +0530 Subject: [PATCH 3/8] fix : Handling DPoP enabled WebAuth flow after process death --- .../provider/AuthenticationActivity.kt | 2 +- .../auth0/android/provider/OAuthManager.kt | 15 +++- .../android/provider/OAuthManagerState.kt | 9 +- .../auth0/android/provider/WebAuthProvider.kt | 5 +- .../android/provider/OAuthManagerStateTest.kt | 84 +++++++++++++++++++ .../android/provider/WebAuthProviderTest.kt | 22 +++++ 6 files changed, 126 insertions(+), 11 deletions(-) diff --git a/auth0/src/main/java/com/auth0/android/provider/AuthenticationActivity.kt b/auth0/src/main/java/com/auth0/android/provider/AuthenticationActivity.kt index 5f3c3f4e6..b0f413cc6 100644 --- a/auth0/src/main/java/com/auth0/android/provider/AuthenticationActivity.kt +++ b/auth0/src/main/java/com/auth0/android/provider/AuthenticationActivity.kt @@ -40,7 +40,7 @@ public open class AuthenticationActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) if (savedInstanceState != null) { - WebAuthProvider.onRestoreInstanceState(savedInstanceState) + WebAuthProvider.onRestoreInstanceState(savedInstanceState, this) intentLaunched = savedInstanceState.getBoolean(EXTRA_INTENT_LAUNCHED, false) } } diff --git a/auth0/src/main/java/com/auth0/android/provider/OAuthManager.kt b/auth0/src/main/java/com/auth0/android/provider/OAuthManager.kt index acc2b2864..718d3a788 100644 --- a/auth0/src/main/java/com/auth0/android/provider/OAuthManager.kt +++ b/auth0/src/main/java/com/auth0/android/provider/OAuthManager.kt @@ -29,6 +29,7 @@ internal class OAuthManager( @get:VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) internal val dPoP: DPoP? = null ) : ResumableManager() { + private val parameters: MutableMap private val headers: MutableMap private val ctOptions: CustomTabsOptions @@ -211,7 +212,8 @@ internal class OAuthManager( auth0 = account, idTokenVerificationIssuer = idTokenVerificationIssuer, idTokenVerificationLeeway = idTokenVerificationLeeway, - customAuthorizeUrl = this.customAuthorizeUrl + customAuthorizeUrl = this.customAuthorizeUrl, + dPoPEnabled = dPoP != null ) } @@ -387,14 +389,21 @@ internal class OAuthManager( internal fun OAuthManager.Companion.fromState( state: OAuthManagerState, - callback: Callback + callback: Callback, + context: Context ): OAuthManager { + // Enable DPoP on the restored PKCE's AuthenticationAPIClient so that + // the token exchange request includes the DPoP proof after process restore. + if (state.dPoPEnabled && state.pkce != null) { + state.pkce.apiClient.useDPoP(context) + } return OAuthManager( account = state.auth0, ctOptions = state.ctOptions, parameters = state.parameters, callback = callback, - customAuthorizeUrl = state.customAuthorizeUrl + customAuthorizeUrl = state.customAuthorizeUrl, + dPoP = if (state.dPoPEnabled ) DPoP(context) else null ).apply { setHeaders( state.headers diff --git a/auth0/src/main/java/com/auth0/android/provider/OAuthManagerState.kt b/auth0/src/main/java/com/auth0/android/provider/OAuthManagerState.kt index ab677af6c..06d0c6b0b 100644 --- a/auth0/src/main/java/com/auth0/android/provider/OAuthManagerState.kt +++ b/auth0/src/main/java/com/auth0/android/provider/OAuthManagerState.kt @@ -6,7 +6,6 @@ import android.util.Base64 import androidx.core.os.ParcelCompat import com.auth0.android.Auth0 import com.auth0.android.authentication.AuthenticationAPIClient -import com.auth0.android.dpop.DPoP import com.auth0.android.request.internal.GsonProvider import com.google.gson.Gson @@ -20,7 +19,7 @@ internal data class OAuthManagerState( val idTokenVerificationLeeway: Int?, val idTokenVerificationIssuer: String?, val customAuthorizeUrl: String? = null, - val dPoP: DPoP? = null + val dPoPEnabled: Boolean = false ) { private class OAuthManagerJson( @@ -37,7 +36,7 @@ internal data class OAuthManagerState( val idTokenVerificationLeeway: Int?, val idTokenVerificationIssuer: String?, val customAuthorizeUrl: String? = null, - val dPoP: DPoP? = null + val dPoPEnabled: Boolean ) fun serializeToJson( @@ -62,7 +61,7 @@ internal data class OAuthManagerState( idTokenVerificationIssuer = idTokenVerificationIssuer, idTokenVerificationLeeway = idTokenVerificationLeeway, customAuthorizeUrl = this.customAuthorizeUrl, - dPoP = this.dPoP + dPoPEnabled = this.dPoPEnabled ) return gson.toJson(json) } finally { @@ -112,7 +111,7 @@ internal data class OAuthManagerState( idTokenVerificationIssuer = oauthManagerJson.idTokenVerificationIssuer, idTokenVerificationLeeway = oauthManagerJson.idTokenVerificationLeeway, customAuthorizeUrl = oauthManagerJson.customAuthorizeUrl, - dPoP = oauthManagerJson.dPoP + dPoPEnabled = oauthManagerJson.dPoPEnabled ) } finally { parcel.recycle() diff --git a/auth0/src/main/java/com/auth0/android/provider/WebAuthProvider.kt b/auth0/src/main/java/com/auth0/android/provider/WebAuthProvider.kt index 6d647a432..a668e0704 100644 --- a/auth0/src/main/java/com/auth0/android/provider/WebAuthProvider.kt +++ b/auth0/src/main/java/com/auth0/android/provider/WebAuthProvider.kt @@ -142,7 +142,7 @@ public object WebAuthProvider : SenderConstraining { } } - internal fun onRestoreInstanceState(bundle: Bundle) { + internal fun onRestoreInstanceState(bundle: Bundle, context: Context) { if (managerInstance == null) { val oauthStateJson = bundle.getString(KEY_BUNDLE_OAUTH_MANAGER_STATE).orEmpty() val parStateJson = bundle.getString(KEY_BUNDLE_PAR_MANAGER_STATE).orEmpty() @@ -162,7 +162,8 @@ public object WebAuthProvider : SenderConstraining { callback.onFailure(error) } } - } + }, + context ) } else if (parStateJson.isNotBlank()) { val state = PARCodeManagerState.deserializeState(parStateJson) diff --git a/auth0/src/test/java/com/auth0/android/provider/OAuthManagerStateTest.kt b/auth0/src/test/java/com/auth0/android/provider/OAuthManagerStateTest.kt index e4ac82383..0543e0a47 100644 --- a/auth0/src/test/java/com/auth0/android/provider/OAuthManagerStateTest.kt +++ b/auth0/src/test/java/com/auth0/android/provider/OAuthManagerStateTest.kt @@ -44,4 +44,88 @@ internal class OAuthManagerStateTest { Assert.assertEquals(1, deserializedState.idTokenVerificationLeeway) Assert.assertEquals("issuer", deserializedState.idTokenVerificationIssuer) } + + @Test + fun `serialize should persist dPoPEnabled flag as true`() { + val auth0 = Auth0.getInstance("clientId", "domain") + val state = OAuthManagerState( + auth0 = auth0, + parameters = mapOf("param1" to "value1"), + headers = mapOf("header1" to "value1"), + requestCode = 1, + ctOptions = CustomTabsOptions.newBuilder() + .showTitle(true) + .withBrowserPicker( + BrowserPicker.newBuilder().withAllowedPackages(emptyList()).build() + ) + .build(), + pkce = PKCE(mock(), "redirectUri", mapOf("header1" to "value1")), + idTokenVerificationLeeway = 1, + idTokenVerificationIssuer = "issuer", + dPoPEnabled = true + ) + + val json = state.serializeToJson() + + Assert.assertTrue(json.isNotBlank()) + Assert.assertTrue(json.contains("\"dPoPEnabled\":true")) + + val deserializedState = OAuthManagerState.deserializeState(json) + + Assert.assertTrue(deserializedState.dPoPEnabled) + } + + @Test + fun `serialize should persist dPoPEnabled flag as false by default`() { + val auth0 = Auth0.getInstance("clientId", "domain") + val state = OAuthManagerState( + auth0 = auth0, + parameters = mapOf("param1" to "value1"), + headers = mapOf("header1" to "value1"), + requestCode = 1, + ctOptions = CustomTabsOptions.newBuilder() + .showTitle(true) + .withBrowserPicker( + BrowserPicker.newBuilder().withAllowedPackages(emptyList()).build() + ) + .build(), + pkce = PKCE(mock(), "redirectUri", mapOf("header1" to "value1")), + idTokenVerificationLeeway = 1, + idTokenVerificationIssuer = "issuer" + ) + + val json = state.serializeToJson() + + val deserializedState = OAuthManagerState.deserializeState(json) + + Assert.assertFalse(deserializedState.dPoPEnabled) + } + + @Test + fun `deserialize should default dPoPEnabled to false when field is missing from JSON`() { + val auth0 = Auth0.getInstance("clientId", "domain") + val state = OAuthManagerState( + auth0 = auth0, + parameters = emptyMap(), + headers = emptyMap(), + requestCode = 0, + ctOptions = CustomTabsOptions.newBuilder() + .showTitle(true) + .withBrowserPicker( + BrowserPicker.newBuilder().withAllowedPackages(emptyList()).build() + ) + .build(), + pkce = PKCE(mock(), "redirectUri", emptyMap()), + idTokenVerificationLeeway = null, + idTokenVerificationIssuer = null + ) + + val json = state.serializeToJson() + // Remove the dPoPEnabled field to simulate legacy JSON + val legacyJson = json.replace(",\"dPoPEnabled\":false", "") + + val deserializedState = OAuthManagerState.deserializeState(legacyJson) + + Assert.assertFalse(deserializedState.dPoPEnabled) + } } diff --git a/auth0/src/test/java/com/auth0/android/provider/WebAuthProviderTest.kt b/auth0/src/test/java/com/auth0/android/provider/WebAuthProviderTest.kt index e033aa8df..db18e6565 100644 --- a/auth0/src/test/java/com/auth0/android/provider/WebAuthProviderTest.kt +++ b/auth0/src/test/java/com/auth0/android/provider/WebAuthProviderTest.kt @@ -4,6 +4,7 @@ import android.app.Activity import android.content.Context import android.content.Intent import android.net.Uri +import android.os.Bundle import android.os.Parcelable import androidx.test.espresso.intent.matcher.IntentMatchers import androidx.test.espresso.intent.matcher.UriMatchers @@ -2958,6 +2959,27 @@ public class WebAuthProviderTest { mockAPI.shutdown() } + @Test + public fun shouldReEnableDPoPOnOAuthManagerAfterProcessDeathRestore() { + `when`(mockKeyStore.hasKeyPair()).thenReturn(true) + `when`(mockKeyStore.getKeyPair()).thenReturn(Pair(mock(), FakeECPublicKey())) + + WebAuthProvider.useDPoP(mockContext) + .login(account) + .start(activity, callback) + + val bundle = Bundle() + WebAuthProvider.onSaveInstanceState(bundle) + + // Simulate the host process being killed and recreated: the manager instance is gone, + // and the activity is recreated with the saved state. + WebAuthProvider.resetManagerInstance() + WebAuthProvider.onRestoreInstanceState(bundle, activity) + + val restoredManager = WebAuthProvider.managerInstance as OAuthManager + assertThat(restoredManager.dPoP, `is`(notNullValue())) + } + //** ** ** ** ** ** **// //** ** ** ** ** ** **// //** Helpers Functions**// From d0aa7c32e746c7f0cae0a587c6828db1d8025b93 Mon Sep 17 00:00:00 2001 From: Prince Mathew Date: Mon, 8 Jun 2026 14:59:40 +0530 Subject: [PATCH 4/8] Addressed review comments --- .../auth0/android/provider/OAuthManager.kt | 4 +- .../android/provider/OAuthManagerState.kt | 2 +- .../android/provider/OAuthManagerStateTest.kt | 58 +++++++++++++++++++ .../android/provider/WebAuthProviderTest.kt | 4 ++ 4 files changed, 65 insertions(+), 3 deletions(-) diff --git a/auth0/src/main/java/com/auth0/android/provider/OAuthManager.kt b/auth0/src/main/java/com/auth0/android/provider/OAuthManager.kt index 718d3a788..242a17335 100644 --- a/auth0/src/main/java/com/auth0/android/provider/OAuthManager.kt +++ b/auth0/src/main/java/com/auth0/android/provider/OAuthManager.kt @@ -394,7 +394,7 @@ internal fun OAuthManager.Companion.fromState( ): OAuthManager { // Enable DPoP on the restored PKCE's AuthenticationAPIClient so that // the token exchange request includes the DPoP proof after process restore. - if (state.dPoPEnabled && state.pkce != null) { + if (state.dPoPEnabled && state.pkce != null) { state.pkce.apiClient.useDPoP(context) } return OAuthManager( @@ -403,7 +403,7 @@ internal fun OAuthManager.Companion.fromState( parameters = state.parameters, callback = callback, customAuthorizeUrl = state.customAuthorizeUrl, - dPoP = if (state.dPoPEnabled ) DPoP(context) else null + dPoP = if (state.dPoPEnabled) DPoP(context) else null ).apply { setHeaders( state.headers diff --git a/auth0/src/main/java/com/auth0/android/provider/OAuthManagerState.kt b/auth0/src/main/java/com/auth0/android/provider/OAuthManagerState.kt index 06d0c6b0b..9f10bb8e5 100644 --- a/auth0/src/main/java/com/auth0/android/provider/OAuthManagerState.kt +++ b/auth0/src/main/java/com/auth0/android/provider/OAuthManagerState.kt @@ -36,7 +36,7 @@ internal data class OAuthManagerState( val idTokenVerificationLeeway: Int?, val idTokenVerificationIssuer: String?, val customAuthorizeUrl: String? = null, - val dPoPEnabled: Boolean + val dPoPEnabled: Boolean = false ) fun serializeToJson( diff --git a/auth0/src/test/java/com/auth0/android/provider/OAuthManagerStateTest.kt b/auth0/src/test/java/com/auth0/android/provider/OAuthManagerStateTest.kt index 0543e0a47..21ac39659 100644 --- a/auth0/src/test/java/com/auth0/android/provider/OAuthManagerStateTest.kt +++ b/auth0/src/test/java/com/auth0/android/provider/OAuthManagerStateTest.kt @@ -1,8 +1,16 @@ package com.auth0.android.provider +import android.content.Context import android.graphics.Color import com.auth0.android.Auth0 +import com.auth0.android.authentication.AuthenticationAPIClient +import com.auth0.android.authentication.AuthenticationException +import com.auth0.android.callback.Callback +import com.auth0.android.result.Credentials import com.nhaarman.mockitokotlin2.mock +import com.nhaarman.mockitokotlin2.whenever +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.core.Is.`is` import org.junit.Assert import org.junit.Test import org.junit.runner.RunWith @@ -128,4 +136,54 @@ internal class OAuthManagerStateTest { Assert.assertFalse(deserializedState.dPoPEnabled) } + + @Test + fun `fromState should re-enable DPoP on the restored PKCE's API client when dPoPEnabled is true`() { + val context = mock() + whenever(context.applicationContext).thenReturn(context) + val auth0 = Auth0.getInstance("clientId", "domain") + val apiClient = AuthenticationAPIClient(auth0) + val state = OAuthManagerState( + auth0 = auth0, + parameters = emptyMap(), + headers = emptyMap(), + requestCode = 0, + ctOptions = CustomTabsOptions.newBuilder().build(), + pkce = PKCE(apiClient, "codeVerifier", "redirectUri", "codeChallenge", emptyMap()), + idTokenVerificationLeeway = null, + idTokenVerificationIssuer = null, + dPoPEnabled = true + ) + val callback = mock>() + + OAuthManager.fromState(state, callback, context) + + // This is the actual regression guard: the token exchange after process death only + // includes the DPoP proof because fromState re-enables DPoP on the restored API client. + assertThat(apiClient.isDPoPEnabled, `is`(true)) + } + + @Test + fun `fromState should not enable DPoP on the restored PKCE's API client when dPoPEnabled is false`() { + val context = mock() + whenever(context.applicationContext).thenReturn(context) + val auth0 = Auth0.getInstance("clientId", "domain") + val apiClient = AuthenticationAPIClient(auth0) + val state = OAuthManagerState( + auth0 = auth0, + parameters = emptyMap(), + headers = emptyMap(), + requestCode = 0, + ctOptions = CustomTabsOptions.newBuilder().build(), + pkce = PKCE(apiClient, "codeVerifier", "redirectUri", "codeChallenge", emptyMap()), + idTokenVerificationLeeway = null, + idTokenVerificationIssuer = null, + dPoPEnabled = false + ) + val callback = mock>() + + OAuthManager.fromState(state, callback, context) + + assertThat(apiClient.isDPoPEnabled, `is`(false)) + } } diff --git a/auth0/src/test/java/com/auth0/android/provider/WebAuthProviderTest.kt b/auth0/src/test/java/com/auth0/android/provider/WebAuthProviderTest.kt index db18e6565..dd195dbf2 100644 --- a/auth0/src/test/java/com/auth0/android/provider/WebAuthProviderTest.kt +++ b/auth0/src/test/java/com/auth0/android/provider/WebAuthProviderTest.kt @@ -2977,6 +2977,10 @@ public class WebAuthProviderTest { WebAuthProvider.onRestoreInstanceState(bundle, activity) val restoredManager = WebAuthProvider.managerInstance as OAuthManager + // This asserts the save/restore wiring reconstructs a DPoP-enabled manager. The actual + // regression guard — that DPoP is re-enabled on the restored PKCE's API client so the + // token exchange carries the proof — lives in OAuthManagerStateTest.fromState tests, + // since OAuthManager.pkce is private and not reachable here without reflection. assertThat(restoredManager.dPoP, `is`(notNullValue())) } From ff8bf8e43c7534ff8899cb8748792dcefd42f4a9 Mon Sep 17 00:00:00 2001 From: Subhankar Maiti Date: Thu, 11 Jun 2026 20:33:05 +0530 Subject: [PATCH 5/8] fix: Implement state recovery for DPoP enabled WebAuth flow after process death --- .../auth0/android/provider/WebAuthProvider.kt | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/auth0/src/main/java/com/auth0/android/provider/WebAuthProvider.kt b/auth0/src/main/java/com/auth0/android/provider/WebAuthProvider.kt index a668e0704..6546619a0 100644 --- a/auth0/src/main/java/com/auth0/android/provider/WebAuthProvider.kt +++ b/auth0/src/main/java/com/auth0/android/provider/WebAuthProvider.kt @@ -38,14 +38,43 @@ public object WebAuthProvider : SenderConstraining { private val callbacks = CopyOnWriteArraySet>() private val parCallbacks = CopyOnWriteArraySet>() + // Buffers a state-restore result that completed before any callback was registered (process + // death during login: the restored AuthenticationActivity finishes the token exchange before + // the host app can subscribe). Delivered to the next addCallback subscriber. + private sealed class RecoveredResult { + class Success(val credentials: Credentials) : RecoveredResult() + class Failure(val error: AuthenticationException) : RecoveredResult() + } + + private val recoveryLock = Any() + private var pendingRecovered: RecoveredResult? = null + @JvmStatic @get:VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) internal var managerInstance: ResumableManager? = null private set + /** + * Registers a callback for Universal Login results from the state-restore path + * ([onRestoreInstanceState]). A result buffered before this call is delivered immediately and + * consumed. Normal in-process logins resolve through the [Builder.start] callback, not here. + */ @JvmStatic public fun addCallback(callback: Callback) { - callbacks += callback + val buffered = synchronized(recoveryLock) { + val pending = pendingRecovered + if (pending != null) { + pendingRecovered = null + } else { + callbacks += callback + } + pending + } + when (buffered) { + is RecoveredResult.Success -> callback.onSuccess(buffered.credentials) + is RecoveredResult.Failure -> callback.onFailure(buffered.error) + null -> {} + } } @JvmStatic @@ -152,12 +181,24 @@ public object WebAuthProvider : SenderConstraining { state, object : Callback { override fun onSuccess(result: Credentials) { + synchronized(recoveryLock) { + if (callbacks.isEmpty()) { + pendingRecovered = RecoveredResult.Success(result) + return + } + } for (callback in callbacks) { callback.onSuccess(result) } } override fun onFailure(error: AuthenticationException) { + synchronized(recoveryLock) { + if (callbacks.isEmpty()) { + pendingRecovered = RecoveredResult.Failure(error) + return + } + } for (callback in callbacks) { callback.onFailure(error) } From 29ce30e68b16379d996f1dc2e4d7222107f02298 Mon Sep 17 00:00:00 2001 From: Subhankar Maiti Date: Thu, 11 Jun 2026 23:23:18 +0530 Subject: [PATCH 6/8] fix: Ensure thread-safe callback removal and notification in WebAuthProvider --- .../com/auth0/android/provider/WebAuthProvider.kt | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/auth0/src/main/java/com/auth0/android/provider/WebAuthProvider.kt b/auth0/src/main/java/com/auth0/android/provider/WebAuthProvider.kt index 6546619a0..0b218deb8 100644 --- a/auth0/src/main/java/com/auth0/android/provider/WebAuthProvider.kt +++ b/auth0/src/main/java/com/auth0/android/provider/WebAuthProvider.kt @@ -79,7 +79,9 @@ public object WebAuthProvider : SenderConstraining { @JvmStatic public fun removeCallback(callback: Callback) { - callbacks -= callback + synchronized(recoveryLock) { + callbacks -= callback + } } // Public methods @@ -181,25 +183,27 @@ public object WebAuthProvider : SenderConstraining { state, object : Callback { override fun onSuccess(result: Credentials) { - synchronized(recoveryLock) { + val subscribers = synchronized(recoveryLock) { if (callbacks.isEmpty()) { pendingRecovered = RecoveredResult.Success(result) return } + callbacks.toList() } - for (callback in callbacks) { + for (callback in subscribers) { callback.onSuccess(result) } } override fun onFailure(error: AuthenticationException) { - synchronized(recoveryLock) { + val subscribers = synchronized(recoveryLock) { if (callbacks.isEmpty()) { pendingRecovered = RecoveredResult.Failure(error) return } + callbacks.toList() } - for (callback in callbacks) { + for (callback in subscribers) { callback.onFailure(error) } } From e17253e5f2a2354dca3b962cb6a3dc4501bfc0c9 Mon Sep 17 00:00:00 2001 From: Subhankar Maiti Date: Fri, 12 Jun 2026 09:42:01 +0530 Subject: [PATCH 7/8] Revert "fix: Ensure thread-safe callback removal and notification in WebAuthProvider" This reverts commit 29ce30e68b16379d996f1dc2e4d7222107f02298. --- .../com/auth0/android/provider/WebAuthProvider.kt | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/auth0/src/main/java/com/auth0/android/provider/WebAuthProvider.kt b/auth0/src/main/java/com/auth0/android/provider/WebAuthProvider.kt index 0b218deb8..6546619a0 100644 --- a/auth0/src/main/java/com/auth0/android/provider/WebAuthProvider.kt +++ b/auth0/src/main/java/com/auth0/android/provider/WebAuthProvider.kt @@ -79,9 +79,7 @@ public object WebAuthProvider : SenderConstraining { @JvmStatic public fun removeCallback(callback: Callback) { - synchronized(recoveryLock) { - callbacks -= callback - } + callbacks -= callback } // Public methods @@ -183,27 +181,25 @@ public object WebAuthProvider : SenderConstraining { state, object : Callback { override fun onSuccess(result: Credentials) { - val subscribers = synchronized(recoveryLock) { + synchronized(recoveryLock) { if (callbacks.isEmpty()) { pendingRecovered = RecoveredResult.Success(result) return } - callbacks.toList() } - for (callback in subscribers) { + for (callback in callbacks) { callback.onSuccess(result) } } override fun onFailure(error: AuthenticationException) { - val subscribers = synchronized(recoveryLock) { + synchronized(recoveryLock) { if (callbacks.isEmpty()) { pendingRecovered = RecoveredResult.Failure(error) return } - callbacks.toList() } - for (callback in subscribers) { + for (callback in callbacks) { callback.onFailure(error) } } From 0d4c16f489fbf8b7075422e74316ce0918c30eeb Mon Sep 17 00:00:00 2001 From: Subhankar Maiti Date: Fri, 12 Jun 2026 09:42:01 +0530 Subject: [PATCH 8/8] Revert "fix: Implement state recovery for DPoP enabled WebAuth flow after process death" This reverts commit ff8bf8e43c7534ff8899cb8748792dcefd42f4a9. --- .../auth0/android/provider/WebAuthProvider.kt | 43 +------------------ 1 file changed, 1 insertion(+), 42 deletions(-) diff --git a/auth0/src/main/java/com/auth0/android/provider/WebAuthProvider.kt b/auth0/src/main/java/com/auth0/android/provider/WebAuthProvider.kt index 6546619a0..a668e0704 100644 --- a/auth0/src/main/java/com/auth0/android/provider/WebAuthProvider.kt +++ b/auth0/src/main/java/com/auth0/android/provider/WebAuthProvider.kt @@ -38,43 +38,14 @@ public object WebAuthProvider : SenderConstraining { private val callbacks = CopyOnWriteArraySet>() private val parCallbacks = CopyOnWriteArraySet>() - // Buffers a state-restore result that completed before any callback was registered (process - // death during login: the restored AuthenticationActivity finishes the token exchange before - // the host app can subscribe). Delivered to the next addCallback subscriber. - private sealed class RecoveredResult { - class Success(val credentials: Credentials) : RecoveredResult() - class Failure(val error: AuthenticationException) : RecoveredResult() - } - - private val recoveryLock = Any() - private var pendingRecovered: RecoveredResult? = null - @JvmStatic @get:VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) internal var managerInstance: ResumableManager? = null private set - /** - * Registers a callback for Universal Login results from the state-restore path - * ([onRestoreInstanceState]). A result buffered before this call is delivered immediately and - * consumed. Normal in-process logins resolve through the [Builder.start] callback, not here. - */ @JvmStatic public fun addCallback(callback: Callback) { - val buffered = synchronized(recoveryLock) { - val pending = pendingRecovered - if (pending != null) { - pendingRecovered = null - } else { - callbacks += callback - } - pending - } - when (buffered) { - is RecoveredResult.Success -> callback.onSuccess(buffered.credentials) - is RecoveredResult.Failure -> callback.onFailure(buffered.error) - null -> {} - } + callbacks += callback } @JvmStatic @@ -181,24 +152,12 @@ public object WebAuthProvider : SenderConstraining { state, object : Callback { override fun onSuccess(result: Credentials) { - synchronized(recoveryLock) { - if (callbacks.isEmpty()) { - pendingRecovered = RecoveredResult.Success(result) - return - } - } for (callback in callbacks) { callback.onSuccess(result) } } override fun onFailure(error: AuthenticationException) { - synchronized(recoveryLock) { - if (callbacks.isEmpty()) { - pendingRecovered = RecoveredResult.Failure(error) - return - } - } for (callback in callbacks) { callback.onFailure(error) }