Skip to content

Commit db02f71

Browse files
committed
Made updateAuthenticationMethodById internal and fixed review change
1 parent 5a71404 commit db02f71

File tree

3 files changed

+51
-90
lines changed

3 files changed

+51
-90
lines changed

EXAMPLES.md

Lines changed: 8 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,7 @@ client.enroll(passkeyCredential, challenge)
974974
**Scopes required:** `read:me:factors`
975975
```kotlin
976976
myAccountClient.getFactors()
977-
.start(object : Callback<Factors, MyAccountException> {
977+
.start(object : Callback<List<Factor>, MyAccountException> {
978978
override fun onSuccess(result: Factors) {
979979
// List of available factors in result.factors
980980
}
@@ -986,7 +986,7 @@ myAccountClient.getFactors()
986986

987987
```java
988988
myAccountClient.getFactors()
989-
.start(new Callback<Factors, MyAccountException>() {
989+
.start(new Callback<List<Factor>, MyAccountException>() {
990990
@Override
991991
public void onSuccess(Factors result) {
992992
// List of available factors in result.getFactors()
@@ -1108,7 +1108,7 @@ myAccountClient.enrollEmail("user@example.com")
11081108
**Scopes required:** `create:me:authentication_methods`
11091109
```kotlin
11101110
myAccountClient.enrollTotp()
1111-
.start(object : Callback<EnrollmentChallenge, MyAccountException> {
1111+
.start(object : Callback<TotpEnrollmentChallenge, MyAccountException> {
11121112
override fun onSuccess(result: EnrollmentChallenge) {
11131113
val totpChallenge = result as TotpEnrollmentChallenge
11141114
// Show QR code from totpChallenge.barcodeUri or manual code from totpChallenge.manualInputCode
@@ -1121,7 +1121,7 @@ myAccountClient.enrollTotp()
11211121

11221122
```java
11231123
myAccountClient.enrollTotp()
1124-
.start(new Callback<EnrollmentChallenge, MyAccountException>() {
1124+
.start(new Callback<TotpEnrollmentChallenge, MyAccountException>() {
11251125
@Override
11261126
public void onSuccess(EnrollmentChallenge result) {
11271127
TotpEnrollmentChallenge totpChallenge = (TotpEnrollmentChallenge) result;
@@ -1138,7 +1138,7 @@ myAccountClient.enrollTotp()
11381138
**Scopes required:** `create:me:authentication_methods`
11391139
```kotlin
11401140
myAccountClient.enrollPushNotification()
1141-
.start(object : Callback<EnrollmentChallenge, MyAccountException> {
1141+
.start(object : Callback<TotpEnrollmentChallenge, MyAccountException> {
11421142
override fun onSuccess(result: EnrollmentChallenge) {
11431143
val pushChallenge = result as TotpEnrollmentChallenge // Uses the same response format as TOTP
11441144
// Show QR code from pushChallenge.barcodeUri to be scanned by Auth0 Guardian/Verify
@@ -1152,7 +1152,7 @@ myAccountClient.enrollPushNotification()
11521152

11531153
```java
11541154
myAccountClient.enrollPushNotification()
1155-
.start(new Callback<EnrollmentChallenge, MyAccountException>() {
1155+
.start(new Callback<TotpEnrollmentChallenge, MyAccountException>() {
11561156
@Override
11571157
public void onSuccess(EnrollmentChallenge result) {
11581158
TotpEnrollmentChallenge pushChallenge = (TotpEnrollmentChallenge) result;
@@ -1169,7 +1169,7 @@ myAccountClient.enrollPushNotification()
11691169
**Scopes required:** `create:me:authentication_methods`
11701170
```kotlin
11711171
myAccountClient.enrollRecoveryCode()
1172-
.start(object : Callback<EnrollmentChallenge, MyAccountException> {
1172+
.start(object : Callback<RecoveryCodeEnrollmentChallenge, MyAccountException> {
11731173
override fun onSuccess(result: EnrollmentChallenge) {
11741174
val recoveryChallenge = result as RecoveryCodeEnrollmentChallenge
11751175
// Display and require the user to save recoveryChallenge.recoveryCode
@@ -1186,7 +1186,7 @@ myAccountClient.enrollRecoveryCode()
11861186
myAccountClient.enrollRecoveryCode()
11871187
.start(new Callback<EnrollmentChallenge, MyAccountException>() {
11881188
@Override
1189-
public void onSuccess(EnrollmentChallenge result) {
1189+
public void onSuccess(RecoveryCodeEnrollmentChallenge result) {
11901190
RecoveryCodeEnrollmentChallenge recoveryChallenge = (RecoveryCodeEnrollmentChallenge) result;
11911191
// Display and require the user to save recoveryChallenge.getRecoveryCode()
11921192
// This method is already verified.
@@ -1246,55 +1246,6 @@ myAccountClient.verify("challenge_id_from_enroll", "auth_session_from_enroll")
12461246
```
12471247
</details>
12481248

1249-
### Update an Authentication Method
1250-
**Scopes required:** `update:me:authentication_methods`
1251-
```kotlin
1252-
// Example: Update the name of a TOTP or Push method
1253-
myAccountClient.updateAuthenticationMethodById("totp|dev_...", name = "My Authenticator App")
1254-
.start(object : Callback<AuthenticationMethod, MyAccountException> {
1255-
override fun onSuccess(result: AuthenticationMethod) {
1256-
// Update successful
1257-
}
1258-
override fun onFailure(error: MyAccountException) { }
1259-
})
1260-
1261-
// Example: Update the preferred method of a Phone method
1262-
myAccountClient.updateAuthenticationMethodById("phone|dev_...", preferredAuthenticationMethod = PhoneAuthenticationMethodType.VOICE)
1263-
.start(object : Callback<AuthenticationMethod, MyAccountException> {
1264-
override fun onSuccess(result: AuthenticationMethod) {
1265-
// Update successful
1266-
}
1267-
override fun onFailure(error: MyAccountException) { }
1268-
})
1269-
```
1270-
<details>
1271-
<summary>Using Java</summary>
1272-
1273-
```java
1274-
// Example: Update the name of a TOTP or Push method
1275-
myAccountClient.updateAuthenticationMethodById("totp|dev_...", "My Authenticator App", null)
1276-
.start(new Callback<AuthenticationMethod, MyAccountException>() {
1277-
@Override
1278-
public void onSuccess(AuthenticationMethod result) {
1279-
// Update successful
1280-
}
1281-
@Override
1282-
public void onFailure(@NonNull MyAccountException error) { }
1283-
});
1284-
1285-
// Example: Update the preferred method of a Phone method
1286-
myAccountClient.updateAuthenticationMethodById("phone|dev_...", null, PhoneAuthenticationMethodType.VOICE)
1287-
.start(new Callback<AuthenticationMethod, MyAccountException>() {
1288-
@Override
1289-
public void onSuccess(AuthenticationMethod result) {
1290-
// Update successful
1291-
}
1292-
@Override
1293-
public void onFailure(@NonNull MyAccountException error) { }
1294-
});
1295-
```
1296-
</details>
1297-
12981249
### Delete an Authentication Method
12991250
**Scopes required:** `delete:me:authentication_methods`
13001251
```kotlin

auth0/src/main/java/com/auth0/android/myaccount/MyAccountAPIClient.kt

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import com.auth0.android.result.Factors
2222
import com.auth0.android.result.PasskeyAuthenticationMethod
2323
import com.auth0.android.result.PasskeyEnrollmentChallenge
2424
import com.auth0.android.result.PasskeyRegistrationChallenge
25+
import com.auth0.android.result.RecoveryCodeEnrollmentChallenge
26+
import com.auth0.android.result.TotpEnrollmentChallenge
2527

2628
import com.google.gson.Gson
2729
import okhttp3.HttpUrl
@@ -365,9 +367,9 @@ public class MyAccountAPIClient @VisibleForTesting(otherwise = VisibleForTesting
365367
*
366368
*/
367369
@JvmOverloads
368-
public fun updateAuthenticationMethodById(
370+
internal fun updateAuthenticationMethodById(
369371
authenticationMethodId: String,
370-
name: String? = null,
372+
authenticationMethodName: String? = null,
371373
preferredAuthenticationMethod: PhoneAuthenticationMethodType? = null
372374
): Request<AuthenticationMethod, MyAccountException> {
373375
val url = getDomainUrlBuilder()
@@ -376,7 +378,7 @@ public class MyAccountAPIClient @VisibleForTesting(otherwise = VisibleForTesting
376378
.build()
377379

378380
val params = ParameterBuilder.newBuilder().apply {
379-
name?.let { set(AUTHENTICATION_METHOD_NAME, it) }
381+
authenticationMethodName?.let { set(AUTHENTICATION_METHOD_NAME, it) }
380382
preferredAuthenticationMethod?.let {
381383
set(
382384
PREFERRED_AUTHENTICATION_METHOD,
@@ -441,7 +443,7 @@ public class MyAccountAPIClient @VisibleForTesting(otherwise = VisibleForTesting
441443
* Gets the list of factors available for the user to enroll.
442444
*
443445
* ## Scopes Required
444-
* `read:me`
446+
* `read:me:factors`
445447
*
446448
* ## Usage
447449
*
@@ -560,7 +562,7 @@ public class MyAccountAPIClient @VisibleForTesting(otherwise = VisibleForTesting
560562
* val apiClient = MyAccountAPIClient(auth0, accessToken)
561563
*
562564
* apiClient.enrollTotp()
563-
* .start(object : Callback<EnrollmentChallenge, MyAccountException> {
565+
* .start(object : Callback<TotpEnrollmentChallenge, MyAccountException> {
564566
* override fun onSuccess(result: EnrollmentChallenge) {
565567
* // The result will be a TotpEnrollmentChallenge with a barcode_uri
566568
* Log.d("MyApp", "Enrollment started for TOTP.")
@@ -570,9 +572,13 @@ public class MyAccountAPIClient @VisibleForTesting(otherwise = VisibleForTesting
570572
* ```
571573
* @return a request that will yield an enrollment challenge.
572574
*/
573-
public fun enrollTotp(): Request<EnrollmentChallenge, MyAccountException> {
575+
public fun enrollTotp(): Request<TotpEnrollmentChallenge, MyAccountException> {
574576
val params = ParameterBuilder.newBuilder().set(TYPE_KEY, "totp").asDictionary()
575-
return buildEnrollmentRequest(params)
577+
val url = getDomainUrlBuilder().addPathSegment(AUTHENTICATION_METHODS).build()
578+
val adapter = GsonAdapter(TotpEnrollmentChallenge::class.java, gson)
579+
return factory.post(url.toString(), adapter)
580+
.addParameters(params)
581+
.addHeader(AUTHORIZATION_KEY, "Bearer $accessToken")
576582
}
577583

578584
/**
@@ -588,7 +594,7 @@ public class MyAccountAPIClient @VisibleForTesting(otherwise = VisibleForTesting
588594
* val apiClient = MyAccountAPIClient(auth0, accessToken)
589595
*
590596
* apiClient.enrollPushNotification()
591-
* .start(object : Callback<EnrollmentChallenge, MyAccountException> {
597+
* .start(object : Callback<TotpEnrollmentChallenge, MyAccountException> {
592598
* override fun onSuccess(result: EnrollmentChallenge) {
593599
* // The result will be a TotpEnrollmentChallenge containing a barcode_uri
594600
* Log.d("MyApp", "Enrollment started for Push Notification.")
@@ -598,9 +604,14 @@ public class MyAccountAPIClient @VisibleForTesting(otherwise = VisibleForTesting
598604
* ```
599605
* @return a request that will yield an enrollment challenge.
600606
*/
601-
public fun enrollPushNotification(): Request<EnrollmentChallenge, MyAccountException> {
607+
public fun enrollPushNotification(): Request<TotpEnrollmentChallenge, MyAccountException> {
602608
val params = ParameterBuilder.newBuilder().set(TYPE_KEY, "push-notification").asDictionary()
603-
return buildEnrollmentRequest(params)
609+
val url = getDomainUrlBuilder().addPathSegment(AUTHENTICATION_METHODS).build()
610+
// The response structure for push notification challenge is the same as TOTP (contains barcode_uri)
611+
val adapter = GsonAdapter(TotpEnrollmentChallenge::class.java, gson)
612+
return factory.post(url.toString(), adapter)
613+
.addParameters(params)
614+
.addHeader(AUTHORIZATION_KEY, "Bearer $accessToken")
604615
}
605616

606617
/**
@@ -616,7 +627,7 @@ public class MyAccountAPIClient @VisibleForTesting(otherwise = VisibleForTesting
616627
* val apiClient = MyAccountAPIClient(auth0, accessToken)
617628
*
618629
* apiClient.enrollRecoveryCode()
619-
* .start(object : Callback<EnrollmentChallenge, MyAccountException> {
630+
* .start(object : Callback<RecoveryCodeEnrollmentChallenge, MyAccountException> {
620631
* override fun onSuccess(result: EnrollmentChallenge) {
621632
* // The result will be a RecoveryCodeEnrollmentChallenge containing the code
622633
* Log.d("MyApp", "Recovery Code enrollment started.")
@@ -626,9 +637,13 @@ public class MyAccountAPIClient @VisibleForTesting(otherwise = VisibleForTesting
626637
* ```
627638
* @return a request that will yield an enrollment challenge containing the recovery code.
628639
*/
629-
public fun enrollRecoveryCode(): Request<EnrollmentChallenge, MyAccountException> {
640+
public fun enrollRecoveryCode(): Request<RecoveryCodeEnrollmentChallenge, MyAccountException> {
630641
val params = ParameterBuilder.newBuilder().set(TYPE_KEY, "recovery-code").asDictionary()
631-
return buildEnrollmentRequest(params)
642+
val url = getDomainUrlBuilder().addPathSegment(AUTHENTICATION_METHODS).build()
643+
val adapter = GsonAdapter(RecoveryCodeEnrollmentChallenge::class.java, gson)
644+
return factory.post(url.toString(), adapter)
645+
.addParameters(params)
646+
.addHeader(AUTHORIZATION_KEY, "Bearer $accessToken")
632647
}
633648

634649
/**
@@ -770,14 +785,11 @@ public class MyAccountAPIClient @VisibleForTesting(otherwise = VisibleForTesting
770785
return buildEnrollmentRequest(params)
771786
}
772787

773-
private fun buildEnrollmentRequest(params: Map<String, Any>): Request<EnrollmentChallenge, MyAccountException> {
788+
private fun buildEnrollmentRequest(params: Map<String, String>): Request<EnrollmentChallenge, MyAccountException> {
774789
val url = getDomainUrlBuilder().addPathSegment(AUTHENTICATION_METHODS).build()
775-
val request =
776-
factory.post(url.toString(), GsonAdapter(EnrollmentChallenge::class.java, gson))
777-
for ((key, value) in params) {
778-
request.addParameter(key, value)
779-
}
780-
return request.addHeader(AUTHORIZATION_KEY, "Bearer $accessToken")
790+
return factory.post(url.toString(), GsonAdapter(EnrollmentChallenge::class.java, gson))
791+
.addParameters(params)
792+
.addHeader(AUTHORIZATION_KEY, "Bearer $accessToken")
781793
}
782794

783795
private fun getDomainUrlBuilder(): HttpUrl.Builder {

auth0/src/test/java/com/auth0/android/myaccount/MyAccountAPIClientTest.kt

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import com.auth0.android.Auth0
44
import com.auth0.android.request.PublicKeyCredentials
55
import com.auth0.android.request.Response
66
import com.auth0.android.result.AuthenticationMethod
7-
import com.auth0.android.result.AuthenticationMethods
87
import com.auth0.android.result.EnrollmentChallenge
98
import com.auth0.android.result.Factor
10-
import com.auth0.android.result.Factors
119
import com.auth0.android.result.PasskeyAuthenticationMethod
1210
import com.auth0.android.result.PasskeyEnrollmentChallenge
11+
import com.auth0.android.result.RecoveryCodeEnrollmentChallenge
12+
import com.auth0.android.result.TotpEnrollmentChallenge
1313
import com.auth0.android.util.AuthenticationAPIMockServer.Companion.SESSION_ID
1414
import com.auth0.android.util.MockMyAccountCallback
1515
import com.auth0.android.util.MyAccountAPIMockServer
@@ -376,7 +376,7 @@ public class MyAccountAPIClientTest {
376376
val callback = MockMyAccountCallback<AuthenticationMethod>()
377377
val methodId = "totp|12345"
378378
val name = "My Authenticator"
379-
client.updateAuthenticationMethodById(methodId, name = name).start(callback)
379+
client.updateAuthenticationMethodById(methodId, authenticationMethodName = name).start(callback)
380380

381381
val request = mockAPI.takeRequest()
382382
val body = bodyFromRequest<String>(request)
@@ -416,7 +416,7 @@ public class MyAccountAPIClientTest {
416416

417417
@Test
418418
public fun `enrollTotp should send correct payload`() {
419-
val callback = MockMyAccountCallback<EnrollmentChallenge>()
419+
val callback = MockMyAccountCallback<TotpEnrollmentChallenge>()
420420
client.enrollTotp().start(callback)
421421

422422
val request = mockAPI.takeRequest()
@@ -426,9 +426,10 @@ public class MyAccountAPIClientTest {
426426
assertThat(body, Matchers.hasEntry("type", "totp" as Any))
427427
}
428428

429+
429430
@Test
430431
public fun `enrollRecoveryCode should send correct payload`() {
431-
val callback = MockMyAccountCallback<EnrollmentChallenge>()
432+
val callback = MockMyAccountCallback<RecoveryCodeEnrollmentChallenge>()
432433
client.enrollRecoveryCode().start(callback)
433434

434435
val request = mockAPI.takeRequest()
@@ -455,18 +456,15 @@ public class MyAccountAPIClientTest {
455456
}
456457

457458
@Test
458-
public fun `verify for push notifications should send correct payload`() {
459-
val callback = MockMyAccountCallback<AuthenticationMethod>()
460-
val methodId = "push|123"
461-
val session = "abc-def"
462-
client.verify(methodId, session).start(callback)
459+
public fun `enrollPushNotification should send correct payload`() {
460+
val callback = MockMyAccountCallback<TotpEnrollmentChallenge>()
461+
client.enrollPushNotification().start(callback)
463462

464463
val request = mockAPI.takeRequest()
465464
val body = bodyFromRequest<String>(request)
466-
assertThat(request.path, Matchers.equalTo("/me/v1/authentication-methods/push%7C123/verify"))
465+
assertThat(request.path, Matchers.equalTo("/me/v1/authentication-methods"))
467466
assertThat(request.method, Matchers.equalTo("POST"))
468-
assertThat(body, Matchers.hasEntry("auth_session", session as Any))
469-
assertThat(body.containsKey("otp_code"), Matchers.`is`(false))
467+
assertThat(body, Matchers.hasEntry("type", "push-notification" as Any))
470468
}
471469

472470
private fun <T> bodyFromRequest(request: RecordedRequest): Map<String, T> {

0 commit comments

Comments
 (0)