|
1 | 1 | package com.auth0.android.provider |
2 | 2 |
|
| 3 | +import android.content.Context |
3 | 4 | import android.graphics.Color |
4 | 5 | import com.auth0.android.Auth0 |
| 6 | +import com.auth0.android.authentication.AuthenticationAPIClient |
| 7 | +import com.auth0.android.authentication.AuthenticationException |
| 8 | +import com.auth0.android.callback.Callback |
| 9 | +import com.auth0.android.result.Credentials |
5 | 10 | import com.nhaarman.mockitokotlin2.mock |
| 11 | +import com.nhaarman.mockitokotlin2.whenever |
| 12 | +import org.hamcrest.MatcherAssert.assertThat |
| 13 | +import org.hamcrest.core.Is.`is` |
6 | 14 | import org.junit.Assert |
7 | 15 | import org.junit.Test |
8 | 16 | import org.junit.runner.RunWith |
@@ -128,4 +136,54 @@ internal class OAuthManagerStateTest { |
128 | 136 |
|
129 | 137 | Assert.assertFalse(deserializedState.dPoPEnabled) |
130 | 138 | } |
| 139 | + |
| 140 | + @Test |
| 141 | + fun `fromState should re-enable DPoP on the restored PKCE's API client when dPoPEnabled is true`() { |
| 142 | + val context = mock<Context>() |
| 143 | + whenever(context.applicationContext).thenReturn(context) |
| 144 | + val auth0 = Auth0.getInstance("clientId", "domain") |
| 145 | + val apiClient = AuthenticationAPIClient(auth0) |
| 146 | + val state = OAuthManagerState( |
| 147 | + auth0 = auth0, |
| 148 | + parameters = emptyMap(), |
| 149 | + headers = emptyMap(), |
| 150 | + requestCode = 0, |
| 151 | + ctOptions = CustomTabsOptions.newBuilder().build(), |
| 152 | + pkce = PKCE(apiClient, "codeVerifier", "redirectUri", "codeChallenge", emptyMap()), |
| 153 | + idTokenVerificationLeeway = null, |
| 154 | + idTokenVerificationIssuer = null, |
| 155 | + dPoPEnabled = true |
| 156 | + ) |
| 157 | + val callback = mock<Callback<Credentials, AuthenticationException>>() |
| 158 | + |
| 159 | + OAuthManager.fromState(state, callback, context) |
| 160 | + |
| 161 | + // This is the actual regression guard: the token exchange after process death only |
| 162 | + // includes the DPoP proof because fromState re-enables DPoP on the restored API client. |
| 163 | + assertThat(apiClient.isDPoPEnabled, `is`(true)) |
| 164 | + } |
| 165 | + |
| 166 | + @Test |
| 167 | + fun `fromState should not enable DPoP on the restored PKCE's API client when dPoPEnabled is false`() { |
| 168 | + val context = mock<Context>() |
| 169 | + whenever(context.applicationContext).thenReturn(context) |
| 170 | + val auth0 = Auth0.getInstance("clientId", "domain") |
| 171 | + val apiClient = AuthenticationAPIClient(auth0) |
| 172 | + val state = OAuthManagerState( |
| 173 | + auth0 = auth0, |
| 174 | + parameters = emptyMap(), |
| 175 | + headers = emptyMap(), |
| 176 | + requestCode = 0, |
| 177 | + ctOptions = CustomTabsOptions.newBuilder().build(), |
| 178 | + pkce = PKCE(apiClient, "codeVerifier", "redirectUri", "codeChallenge", emptyMap()), |
| 179 | + idTokenVerificationLeeway = null, |
| 180 | + idTokenVerificationIssuer = null, |
| 181 | + dPoPEnabled = false |
| 182 | + ) |
| 183 | + val callback = mock<Callback<Credentials, AuthenticationException>>() |
| 184 | + |
| 185 | + OAuthManager.fromState(state, callback, context) |
| 186 | + |
| 187 | + assertThat(apiClient.isDPoPEnabled, `is`(false)) |
| 188 | + } |
131 | 189 | } |
0 commit comments