@@ -219,7 +219,7 @@ public class EmbeddedAuthClientTest {
219219 on { generateCodeVerifier() } doReturn " verifier"
220220 on { generateCodeChallenge(any()) } doThrow IllegalStateException (" no SHA-256" )
221221 }
222- val failing = EmbeddedAuthClient (auth0, helper)
222+ val failing = EmbeddedAuthClient .create (auth0, helper)
223223
224224 val error = assertEmbeddedErrorSuspending { failing.authorize(EmbeddedAuthMockServer .AUTHORIZE_CONNECTION ).await() }
225225
@@ -228,6 +228,109 @@ public class EmbeddedAuthClientTest {
228228 assertThat(mockAPI.server.requestCount, `is `(0 ))
229229 }
230230
231+ @Test
232+ public fun `authorize sends exactly the initial request fields` (): Unit = runTest {
233+ mockAPI.willReturnInsufficientAuthorization()
234+
235+ assertEmbeddedErrorSuspending { client.authorize(EmbeddedAuthMockServer .AUTHORIZE_CONNECTION ).await() }
236+
237+ val body = bodyOf(mockAPI.takeRequest())
238+ assertThat(
239+ body.keys().asSequence().toSet(),
240+ `is `(setOf (" client_id" , " connection" , " scope" , " capabilities" , " code_challenge" , " code_challenge_method" ))
241+ )
242+ }
243+
244+ @Test
245+ public fun `the token exchange sends exactly the authorization code grant fields` (): Unit = runTest {
246+ mockAPI.willReturnInsufficientAuthorization()
247+ assertEmbeddedErrorSuspending { client.authorize(EmbeddedAuthMockServer .AUTHORIZE_CONNECTION ).await() }
248+ mockAPI.takeRequest()
249+ mockAPI.willReturnAuthorizeCode()
250+ mockAPI.willReturnTokens()
251+
252+ client.verifyOtp(" 123456" , OtpType .OOB ).await()
253+ mockAPI.takeRequest()
254+
255+ assertThat(
256+ bodyOf(mockAPI.takeRequest()).keys().asSequence().toSet(),
257+ `is `(setOf (" client_id" , " grant_type" , " code" , " code_verifier" ))
258+ )
259+ }
260+
261+ @Test
262+ public fun `only authorize carries PKCE parameters, not the later steps` (): Unit = runTest {
263+ mockAPI.willReturnInsufficientAuthorization()
264+ assertEmbeddedErrorSuspending { client.authorize(EmbeddedAuthMockServer .AUTHORIZE_CONNECTION ).await() }
265+ mockAPI.takeRequest()
266+ mockAPI.willReturnInsufficientAuthorization(EmbeddedAuthMockServer .ROTATED_AUTH_SESSION )
267+ assertEmbeddedErrorSuspending { client.identify(" jane@example.com" , IdentifierType .EMAIL ).await() }
268+ val identify = bodyOf(mockAPI.takeRequest())
269+ mockAPI.willReturnInsufficientAuthorization(EmbeddedAuthMockServer .ROTATED_AUTH_SESSION )
270+ assertEmbeddedErrorSuspending { client.challengeEmail().await() }
271+ val challenge = bodyOf(mockAPI.takeRequest())
272+ mockAPI.willReturnAuthorizeCode()
273+ mockAPI.willReturnTokens()
274+ client.verifyOtp(" 123456" , OtpType .OOB ).await()
275+ val verify = bodyOf(mockAPI.takeRequest())
276+
277+ for (body in listOf (identify, challenge, verify)) {
278+ assertThat(body.has(" code_challenge" ), `is `(false ))
279+ assertThat(body.has(" code_challenge_method" ), `is `(false ))
280+ assertThat(body.has(" code_verifier" ), `is `(false ))
281+ }
282+ }
283+
284+ @Test
285+ public fun `a rejected token exchange surfaces SessionExpired on invalid_grant` (): Unit = runTest {
286+ mockAPI.willReturnInsufficientAuthorization()
287+ assertEmbeddedErrorSuspending { client.authorize(EmbeddedAuthMockServer .AUTHORIZE_CONNECTION ).await() }
288+ mockAPI.takeRequest()
289+ mockAPI.willReturnAuthorizeCode()
290+ mockAPI.willReturnInvalidGrant()
291+
292+ val error = assertEmbeddedErrorSuspending { client.verifyOtp(" 123456" , OtpType .OOB ).await() }
293+
294+ assertThat(error.error, `is `(EmbeddedAuthError .SessionExpired ))
295+ }
296+
297+ @Test
298+ public fun `overlapping authorize calls keep the verifier and the session of the same response` (): Unit = runTest {
299+ val first = client.authorize(EmbeddedAuthMockServer .AUTHORIZE_CONNECTION )
300+ val second = client.authorize(EmbeddedAuthMockServer .AUTHORIZE_CONNECTION )
301+ mockAPI.willReturnInsufficientAuthorization(EmbeddedAuthMockServer .ROTATED_AUTH_SESSION )
302+ assertEmbeddedErrorSuspending { second.await() }
303+ mockAPI.takeRequest()
304+ mockAPI.willReturnInsufficientAuthorization(EmbeddedAuthMockServer .AUTH_SESSION )
305+ assertEmbeddedErrorSuspending { first.await() }
306+ val firstChallenge = bodyOf(mockAPI.takeRequest()).getString(" code_challenge" )
307+ mockAPI.willReturnAuthorizeCode()
308+ mockAPI.willReturnTokens()
309+
310+ client.verifyOtp(" 123456" , OtpType .OOB ).await()
311+ val verify = bodyOf(mockAPI.takeRequest())
312+ val exchange = bodyOf(mockAPI.takeRequest())
313+
314+ assertThat(verify.getString(" auth_session" ), `is `(EmbeddedAuthMockServer .AUTH_SESSION ))
315+ assertThat(s256(exchange.getString(" code_verifier" )), `is `(firstChallenge))
316+ }
317+
318+ @Test
319+ public fun `a failed challenge creation abandons the flow in progress` (): Unit = runTest {
320+ val helper = mock<AlgorithmHelper > {
321+ on { generateCodeVerifier() } doReturn " verifier"
322+ on { generateCodeChallenge(any()) } doReturn " challenge" doThrow IllegalStateException (" no SHA-256" )
323+ }
324+ val flaky = EmbeddedAuthClient .create(auth0, helper)
325+ mockAPI.willReturnInsufficientAuthorization()
326+ assertEmbeddedErrorSuspending { flaky.authorize(EmbeddedAuthMockServer .AUTHORIZE_CONNECTION ).await() }
327+ assertEmbeddedErrorSuspending { flaky.authorize(EmbeddedAuthMockServer .AUTHORIZE_CONNECTION ).await() }
328+
329+ val error = assertEmbeddedErrorSuspending { flaky.identify(" jane@example.com" , IdentifierType .EMAIL ).await() }
330+
331+ assertThat(error.error, `is `(EmbeddedAuthError .NoActiveSession ))
332+ }
333+
231334 // ── verifyOtp — token exchange ──────────────────────────────────────────────
232335
233336 @Test
0 commit comments