@@ -3,16 +3,17 @@ package com.auth0.android.authentication
33import com.auth0.android.Auth0
44import com.auth0.android.authentication.mfa.MfaApiClient
55import com.auth0.android.authentication.mfa.MfaEnrollmentType
6+ import com.auth0.android.authentication.mfa.MfaException.MfaChallengeException
7+ import com.auth0.android.authentication.mfa.MfaException.MfaEnrollmentException
8+ import com.auth0.android.authentication.mfa.MfaException.MfaListAuthenticatorsException
9+ import com.auth0.android.authentication.mfa.MfaException.MfaVerifyException
610import com.auth0.android.authentication.mfa.MfaVerificationType
7- import com.auth0.android.authentication.mfa.MfaException.*
811import com.auth0.android.request.internal.ThreadSwitcherShadow
912import com.auth0.android.result.Authenticator
1013import com.auth0.android.result.Challenge
1114import com.auth0.android.result.Credentials
1215import com.auth0.android.result.EnrollmentChallenge
13- import com.auth0.android.result.MfaEnrollmentChallenge
1416import com.auth0.android.result.TotpEnrollmentChallenge
15- import com.auth0.android.util.CallbackMatcher
1617import com.auth0.android.util.MockCallback
1718import com.auth0.android.util.SSLTestUtils
1819import com.google.gson.Gson
@@ -24,7 +25,12 @@ import okhttp3.mockwebserver.MockResponse
2425import okhttp3.mockwebserver.MockWebServer
2526import okhttp3.mockwebserver.RecordedRequest
2627import org.hamcrest.MatcherAssert.assertThat
27- import org.hamcrest.Matchers.*
28+ import org.hamcrest.Matchers.containsString
29+ import org.hamcrest.Matchers.hasSize
30+ import org.hamcrest.Matchers.instanceOf
31+ import org.hamcrest.Matchers.`is`
32+ import org.hamcrest.Matchers.notNullValue
33+ import org.hamcrest.Matchers.nullValue
2834import org.junit.After
2935import org.junit.Assert.assertThrows
3036import org.junit.Before
@@ -69,7 +75,11 @@ public class MfaApiClientTest {
6975 )
7076 }
7177
72- private fun enqueueErrorResponse (error : String , description : String , statusCode : Int = 400): Unit {
78+ private fun enqueueErrorResponse (
79+ error : String ,
80+ description : String ,
81+ statusCode : Int = 400
82+ ): Unit {
7383 val json = """ {"error": "$error ", "error_description": "$description "}"""
7484 enqueueMockResponse(json, statusCode)
7585 }
@@ -87,6 +97,51 @@ public class MfaApiClientTest {
8797 }
8898
8999
100+ @Test
101+ public fun shouldIncludeAuth0ClientHeaderInGetAuthenticators (): Unit = runTest {
102+ val json = """ [{"id": "sms|dev_123", "type": "oob", "active": true}]"""
103+ enqueueMockResponse(json)
104+
105+ mfaClient.getAuthenticators(listOf (" oob" )).await()
106+
107+ val request = mockServer.takeRequest()
108+ assertThat(request.getHeader(" Auth0-Client" ), `is `(notNullValue()))
109+ }
110+
111+ @Test
112+ public fun shouldIncludeAuth0ClientHeaderInEnroll (): Unit = runTest {
113+ val json = """ {"id": "sms|dev_123", "auth_session": "session_abc"}"""
114+ enqueueMockResponse(json)
115+
116+ mfaClient.enroll(MfaEnrollmentType .Phone (" +12025550135" )).await()
117+
118+ val request = mockServer.takeRequest()
119+ assertThat(request.getHeader(" Auth0-Client" ), `is `(notNullValue()))
120+ }
121+
122+ @Test
123+ public fun shouldIncludeAuth0ClientHeaderInChallenge (): Unit = runTest {
124+ val json = """ {"challenge_type": "oob", "oob_code": "oob_123"}"""
125+ enqueueMockResponse(json)
126+
127+ mfaClient.challenge(" sms|dev_123" ).await()
128+
129+ val request = mockServer.takeRequest()
130+ assertThat(request.getHeader(" Auth0-Client" ), `is `(notNullValue()))
131+ }
132+
133+ @Test
134+ public fun shouldIncludeAuth0ClientHeaderInVerify (): Unit = runTest {
135+ val json =
136+ """ {"access_token": "$ACCESS_TOKEN ", "id_token": "$ID_TOKEN ", "token_type": "Bearer", "expires_in": 86400}"""
137+ enqueueMockResponse(json)
138+
139+ mfaClient.verify(MfaVerificationType .Otp (" 123456" )).await()
140+
141+ val request = mockServer.takeRequest()
142+ assertThat(request.getHeader(" Auth0-Client" ), `is `(notNullValue()))
143+ }
144+
90145 @Test
91146 public fun shouldGetAuthenticatorsSuccess (): Unit = runTest {
92147 val json = """ [
@@ -436,7 +491,8 @@ public class MfaApiClientTest {
436491
437492 @Test
438493 public fun shouldVerifyOtpWithCorrectGrantType (): Unit = runTest {
439- val json = """ {"access_token": "$ACCESS_TOKEN ", "id_token": "$ID_TOKEN ", "token_type": "Bearer", "expires_in": 86400}"""
494+ val json =
495+ """ {"access_token": "$ACCESS_TOKEN ", "id_token": "$ID_TOKEN ", "token_type": "Bearer", "expires_in": 86400}"""
440496 enqueueMockResponse(json)
441497
442498 mfaClient.verify(MfaVerificationType .Otp (" 123456" )).await()
@@ -500,21 +556,25 @@ public class MfaApiClientTest {
500556
501557 @Test
502558 public fun shouldVerifyOobWithoutBindingCodeSuccess (): Unit = runTest {
503- val json = """ {"access_token": "$ACCESS_TOKEN ", "id_token": "$ID_TOKEN ", "token_type": "Bearer", "expires_in": 86400}"""
559+ val json =
560+ """ {"access_token": "$ACCESS_TOKEN ", "id_token": "$ID_TOKEN ", "token_type": "Bearer", "expires_in": 86400}"""
504561 enqueueMockResponse(json)
505562
506- val credentials = mfaClient.verify(MfaVerificationType .Oob (oobCode = " oob_code_123" )).await()
563+ val credentials =
564+ mfaClient.verify(MfaVerificationType .Oob (oobCode = " oob_code_123" )).await()
507565
508566 assertThat(credentials, `is `(notNullValue()))
509567 assertThat(credentials.accessToken, `is `(ACCESS_TOKEN ))
510568 }
511569
512570 @Test
513571 public fun shouldVerifyOobWithCorrectParameters (): Unit = runTest {
514- val json = """ {"access_token": "$ACCESS_TOKEN ", "id_token": "$ID_TOKEN ", "token_type": "Bearer", "expires_in": 86400}"""
572+ val json =
573+ """ {"access_token": "$ACCESS_TOKEN ", "id_token": "$ID_TOKEN ", "token_type": "Bearer", "expires_in": 86400}"""
515574 enqueueMockResponse(json)
516575
517- mfaClient.verify(MfaVerificationType .Oob (oobCode = " oob_code_123" , bindingCode = " 654321" )).await()
576+ mfaClient.verify(MfaVerificationType .Oob (oobCode = " oob_code_123" , bindingCode = " 654321" ))
577+ .await()
518578
519579 val request = mockServer.takeRequest()
520580 assertThat(request.path, `is `(" /oauth/token" ))
@@ -530,7 +590,8 @@ public class MfaApiClientTest {
530590
531591 @Test
532592 public fun shouldVerifyOobWithoutBindingCodeInRequest (): Unit = runTest {
533- val json = """ {"access_token": "$ACCESS_TOKEN ", "id_token": "$ID_TOKEN ", "token_type": "Bearer", "expires_in": 86400}"""
593+ val json =
594+ """ {"access_token": "$ACCESS_TOKEN ", "id_token": "$ID_TOKEN ", "token_type": "Bearer", "expires_in": 86400}"""
534595 enqueueMockResponse(json)
535596
536597 mfaClient.verify(MfaVerificationType .Oob (oobCode = " oob_code_123" )).await()
@@ -565,7 +626,8 @@ public class MfaApiClientTest {
565626 }"""
566627 enqueueMockResponse(json)
567628
568- val credentials = mfaClient.verify(MfaVerificationType .RecoveryCode (" OLD_RECOVERY_CODE" )).await()
629+ val credentials =
630+ mfaClient.verify(MfaVerificationType .RecoveryCode (" OLD_RECOVERY_CODE" )).await()
569631
570632 assertThat(credentials, `is `(notNullValue()))
571633 assertThat(credentials.accessToken, `is `(ACCESS_TOKEN ))
@@ -574,7 +636,8 @@ public class MfaApiClientTest {
574636
575637 @Test
576638 public fun shouldVerifyRecoveryCodeWithCorrectParameters (): Unit = runTest {
577- val json = """ {"access_token": "$ACCESS_TOKEN ", "id_token": "$ID_TOKEN ", "token_type": "Bearer", "expires_in": 86400}"""
639+ val json =
640+ """ {"access_token": "$ACCESS_TOKEN ", "id_token": "$ID_TOKEN ", "token_type": "Bearer", "expires_in": 86400}"""
578641 enqueueMockResponse(json)
579642
580643 mfaClient.verify(MfaVerificationType .RecoveryCode (" RECOVERY_123" )).await()
@@ -671,7 +734,8 @@ public class MfaApiClientTest {
671734
672735 @Test
673736 public fun shouldVerifyOtpWithCallback (): Unit {
674- val json = """ {"access_token": "$ACCESS_TOKEN ", "id_token": "$ID_TOKEN ", "token_type": "Bearer", "expires_in": 86400}"""
737+ val json =
738+ """ {"access_token": "$ACCESS_TOKEN ", "id_token": "$ID_TOKEN ", "token_type": "Bearer", "expires_in": 86400}"""
675739 enqueueMockResponse(json)
676740
677741 val callback = MockCallback <Credentials , MfaVerifyException >()
@@ -763,8 +827,10 @@ public class MfaApiClientTest {
763827 private companion object {
764828 private const val CLIENT_ID = " CLIENT_ID"
765829 private const val MFA_TOKEN = " MFA_TOKEN_123"
766- private const val ACCESS_TOKEN = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N_XgL0n3I9PlFUP0THsR8U"
767- private const val ID_TOKEN = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIn0.Gfx6VO9tcxwk6xqx9yYzSfebfeakZp5JYIgP_edcw_A"
830+ private const val ACCESS_TOKEN =
831+ " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N_XgL0n3I9PlFUP0THsR8U"
832+ private const val ID_TOKEN =
833+ " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIn0.Gfx6VO9tcxwk6xqx9yYzSfebfeakZp5JYIgP_edcw_A"
768834 private const val REFRESH_TOKEN = " REFRESH_TOKEN"
769835 }
770836}
0 commit comments