Skip to content

Commit 8fe07a6

Browse files
Consolidate to a single method
1 parent a2ec631 commit 8fe07a6

2 files changed

Lines changed: 28 additions & 76 deletions

File tree

src/main/java/com/auth0/client/auth/AuthAPI.java

Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,7 +1396,7 @@ public Request<CreatedOtpResponse> addOtpAuthenticator(String mfaToken) {
13961396
}
13971397

13981398
/**
1399-
* Associates or adds a new phone based OOB authenticator for multi-factor authentication (MFA).
1399+
* Associates or adds a new OOB authenticator for multi-factor authentication (MFA).
14001400
* Confidential clients (Regular Web Apps) <strong>must</strong> have a client secret configured on this {@code AuthAPI} instance.
14011401
* <pre>
14021402
* {@code
@@ -1413,12 +1413,19 @@ public Request<CreatedOtpResponse> addOtpAuthenticator(String mfaToken) {
14131413
* @param mfaToken The token received from mfa_required error. Must not be null.
14141414
* @param oobChannels The type of OOB channels supported by the client. Must not be null.
14151415
* @param phoneNumber The phone number for "sms" or "voice" channels. May be null if not using "sms" or "voice".
1416+
* @param emailAddress The email address for "email" channel. May be null if not using "email".
14161417
* @return a Request to execute.
14171418
* @see <a href="https://auth0.com/docs/secure/multi-factor-authentication/authenticate-using-ropg-flow-with-mfa/enroll-challenge-sms-voice-authenticators#enroll-with-sms-or-voice">Enroll with SMS or voice</a>
14181419
*/
1419-
public Request<CreatedOobResponse> addPhoneOobAuthenticator(String mfaToken, List<String> oobChannels, String phoneNumber) {
1420+
public Request<CreatedOobResponse> addOobAuthenticator(String mfaToken, List<String> oobChannels, String phoneNumber, String emailAddress) {
14201421
Asserts.assertNotNull(mfaToken, "mfa token");
14211422
Asserts.assertNotNull(oobChannels, "OOB channels");
1423+
if (oobChannels.contains("sms") || oobChannels.contains("voice")) {
1424+
Asserts.assertNotNull(phoneNumber, "phone number");
1425+
}
1426+
if (oobChannels.contains("email")) {
1427+
Asserts.assertNotNull(emailAddress, "email address");
1428+
}
14221429

14231430
String url = baseUrl
14241431
.newBuilder()
@@ -1433,57 +1440,18 @@ public Request<CreatedOobResponse> addPhoneOobAuthenticator(String mfaToken, Lis
14331440
request.addParameter("authenticator_types", Collections.singletonList("oob"));
14341441
request.addParameter("oob_channels", oobChannels);
14351442
request.addParameter(KEY_CLIENT_ID, clientId);
1443+
14361444
if (phoneNumber != null) {
14371445
request.addParameter("phone_number", phoneNumber);
14381446
}
1447+
if (emailAddress != null) {
1448+
request.addParameter("email", emailAddress);
1449+
}
14391450
addClientAuthentication(request, false);
14401451
request.addHeader("Authorization", "Bearer " + mfaToken);
14411452
return request;
14421453
}
14431454

1444-
/**
1445-
* Associates or adds a new email based OOB authenticator for multi-factor authentication (MFA).
1446-
* Confidential clients (Regular Web Apps) <strong>must</strong> have a client secret configured on this {@code AuthAPI} instance.
1447-
* <pre>
1448-
* {@code
1449-
* try {
1450-
* CreatedOobResponse result = authAPI.addOobAuthenticator("the-mfa-token", "email-address")
1451-
* .execute()
1452-
* .getBody();
1453-
* } catch (Auth0Exception e) {
1454-
* //Something happened
1455-
* }
1456-
* }
1457-
* </pre>
1458-
*
1459-
* @param mfaToken The token received from mfa_required error. Must not be null.
1460-
* @param emailAddress The email address for "email" channel.
1461-
* @return a Request to execute.
1462-
* @see <a href="https://auth0.com/docs/secure/multi-factor-authentication/authenticate-using-ropg-flow-with-mfa/enroll-and-challenge-email-authenticators#enroll-with-email">Enroll with email</a>
1463-
*/
1464-
public Request<CreatedOobResponse> addEmailOobAuthenticator(String mfaToken, String emailAddress) {
1465-
Asserts.assertNotNull(mfaToken, "mfa token");
1466-
1467-
String url = baseUrl
1468-
.newBuilder()
1469-
.addPathSegment("mfa")
1470-
.addPathSegment("associate")
1471-
.build()
1472-
.toString();
1473-
1474-
BaseRequest<CreatedOobResponse> request = new BaseRequest<>(client, null, url, HttpMethod.POST, new TypeReference<CreatedOobResponse>() {
1475-
});
1476-
1477-
request.addParameter("authenticator_types", Collections.singletonList("oob"));
1478-
request.addParameter("oob_channels", Collections.singletonList("email"));
1479-
request.addParameter(KEY_CLIENT_ID, clientId);
1480-
request.addParameter("email", emailAddress);
1481-
addClientAuthentication(request, false);
1482-
request.addHeader("Authorization", "Bearer " + mfaToken);
1483-
return request;
1484-
}
1485-
1486-
14871455
/**
14881456
* Returns a list of authenticators associated with your application.
14891457
* <pre>

src/test/java/com/auth0/client/auth/AuthAPITest.java

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,53 +1542,36 @@ public void addOtpAuthenticatorRequest() throws Exception {
15421542
}
15431543

15441544
@Test
1545-
public void addPhoneOobAuthenticatorThrowsWhenTokenNull() {
1545+
public void addOobAuthenticatorThrowsWhenTokenNull() {
15461546
verifyThrows(IllegalArgumentException.class,
1547-
() -> api.addPhoneOobAuthenticator(null, Collections.singletonList("otp"), null),
1547+
() -> api.addOobAuthenticator(null, Collections.singletonList("otp"), null, null),
15481548
"'mfa token' cannot be null!");
15491549
}
15501550

15511551
@Test
1552-
public void addPhoneOobAuthenticatorThrowsWhenChannelsNull() {
1552+
public void addOobAuthenticatorThrowsWhenChannelsNull() {
15531553
verifyThrows(IllegalArgumentException.class,
1554-
() -> api.addPhoneOobAuthenticator("mfaToken", null, null),
1554+
() -> api.addOobAuthenticator("mfaToken", null, null, null),
15551555
"'OOB channels' cannot be null!");
15561556
}
15571557

15581558
@Test
1559-
public void addOobAuthenticatorRequest() throws Exception {
1560-
Request<CreatedOobResponse> request = api.addPhoneOobAuthenticator("mfaToken", Collections.singletonList("sms"), "phone-number");
1561-
1562-
server.jsonResponse(AUTH_OOB_AUTHENTICATOR_RESPONSE, 200);
1563-
CreatedOobResponse response = request.execute().getBody();
1564-
RecordedRequest recordedRequest = server.takeRequest();
1565-
1566-
assertThat(recordedRequest, hasMethodAndPath(HttpMethod.POST, "/mfa/associate"));
1567-
assertThat(recordedRequest, hasHeader("Content-Type", "application/json"));
1568-
1569-
Map<String, Object> body = bodyFromRequest(recordedRequest);
1570-
assertThat(body, hasEntry("authenticator_types", Collections.singletonList("oob")));
1571-
assertThat(body, hasEntry("oob_channels", Collections.singletonList("sms")));
1572-
assertThat(body, hasEntry("phone_number", "phone-number"));
1573-
1574-
assertThat(response, is(notNullValue()));
1575-
assertThat(response.getAuthenticatorType(), not(emptyOrNullString()));
1576-
assertThat(response.getOobChannel(), not(emptyOrNullString()));
1577-
assertThat(response.getOobCode(), not(emptyOrNullString()));
1578-
assertThat(response.getBarcodeUri(), not(emptyOrNullString()));
1579-
assertThat(response.getRecoveryCodes(), notNullValue());
1559+
public void addOobAuthenticatorThrowsWhenPhoneNumberNull() {
1560+
verifyThrows(IllegalArgumentException.class,
1561+
() -> api.addOobAuthenticator("mfaToken", Collections.singletonList("sms"), null, null),
1562+
"'phone number' cannot be null!");
15801563
}
15811564

15821565
@Test
1583-
public void addEmailOobAuthenticatorThrowsWhenTokenNull() {
1566+
public void addOobAuthenticatorThrowsWhenEmailNull() {
15841567
verifyThrows(IllegalArgumentException.class,
1585-
() -> api.addEmailOobAuthenticator(null, null),
1586-
"'mfa token' cannot be null!");
1568+
() -> api.addOobAuthenticator("mfaToken", Collections.singletonList("email"), null, null),
1569+
"'email address' cannot be null!");
15871570
}
15881571

15891572
@Test
1590-
public void addEmailOobAuthenticatorRequest() throws Exception {
1591-
Request<CreatedOobResponse> request = api.addEmailOobAuthenticator("mfaToken", "email-address");
1573+
public void addOobAuthenticatorRequest() throws Exception {
1574+
Request<CreatedOobResponse> request = api.addOobAuthenticator("mfaToken", Arrays.asList("sms", "email"), "phone-number", "email-address");
15921575

15931576
server.jsonResponse(AUTH_OOB_AUTHENTICATOR_RESPONSE, 200);
15941577
CreatedOobResponse response = request.execute().getBody();
@@ -1599,7 +1582,8 @@ public void addEmailOobAuthenticatorRequest() throws Exception {
15991582

16001583
Map<String, Object> body = bodyFromRequest(recordedRequest);
16011584
assertThat(body, hasEntry("authenticator_types", Collections.singletonList("oob")));
1602-
assertThat(body, hasEntry("oob_channels", Collections.singletonList("email")));
1585+
assertThat(body, hasEntry("oob_channels", Arrays.asList("sms", "email")));
1586+
assertThat(body, hasEntry("phone_number", "phone-number"));
16031587
assertThat(body, hasEntry("email", "email-address"));
16041588

16051589
assertThat(response, is(notNullValue()));

0 commit comments

Comments
 (0)