Skip to content

Commit 8bd77fa

Browse files
Add phone number null checking in deprecated method and add tests
1 parent 6a8fa87 commit 8bd77fa

File tree

2 files changed

+115
-7
lines changed

2 files changed

+115
-7
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,9 +1397,6 @@ public Request<CreatedOtpResponse> addOtpAuthenticator(String mfaToken) {
13971397

13981398

13991399
private BaseRequest<CreatedOobResponse> createBaseOobRequest(String mfaToken, List<String> oobChannels) {
1400-
Asserts.assertNotNull(mfaToken, "mfa token");
1401-
Asserts.assertNotNull(oobChannels, "OOB channels");
1402-
14031400
String url = baseUrl
14041401
.newBuilder()
14051402
.addPathSegment("mfa")
@@ -1443,7 +1440,14 @@ private BaseRequest<CreatedOobResponse> createBaseOobRequest(String mfaToken, Li
14431440
*/
14441441
@Deprecated
14451442
public Request<CreatedOobResponse> addOobAuthenticator(String mfaToken, List<String> oobChannels, String phoneNumber) {
1443+
Asserts.assertNotNull(mfaToken, "mfa token");
1444+
Asserts.assertNotNull(oobChannels, "OOB channels");
1445+
if (oobChannels.contains("sms") || oobChannels.contains("voice")) {
1446+
Asserts.assertNotNull(phoneNumber, "phone number");
1447+
}
1448+
14461449
BaseRequest<CreatedOobResponse> request = createBaseOobRequest(mfaToken, oobChannels);
1450+
14471451
if (phoneNumber != null) {
14481452
request.addParameter("phone_number", phoneNumber);
14491453
}
@@ -1474,6 +1478,8 @@ public Request<CreatedOobResponse> addOobAuthenticator(String mfaToken, List<Str
14741478
* @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>
14751479
*/
14761480
public Request<CreatedOobResponse> addOobAuthenticator(String mfaToken, List<String> oobChannels, String phoneNumber, String emailAddress) {
1481+
Asserts.assertNotNull(mfaToken, "mfa token");
1482+
Asserts.assertNotNull(oobChannels, "OOB channels");
14771483
if (oobChannels.contains("sms") || oobChannels.contains("voice")) {
14781484
Asserts.assertNotNull(phoneNumber, "phone number");
14791485
}

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

Lines changed: 106 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,24 +1541,33 @@ public void addOtpAuthenticatorRequest() throws Exception {
15411541
assertThat(response.getRecoveryCodes(), notNullValue());
15421542
}
15431543

1544+
@SuppressWarnings("deprecation")
15441545
@Test
1545-
public void addOobAuthenticatorThrowsWhenTokenNull() {
1546+
public void addOobAuthenticatorDeprecatedThrowsWhenTokenNull() {
15461547
verifyThrows(IllegalArgumentException.class,
1547-
() -> api.addOobAuthenticator(null, Collections.singletonList("otp"), null, null),
1548+
() -> api.addOobAuthenticator(null, Collections.singletonList("auth0"), null),
15481549
"'mfa token' cannot be null!");
15491550
}
15501551

15511552
@SuppressWarnings("deprecation")
15521553
@Test
1553-
public void addOobAuthenticatorThrowsWhenChannelsNull() {
1554+
public void addOobAuthenticatorDeprecatedThrowsWhenChannelsNull() {
15541555
verifyThrows(IllegalArgumentException.class,
15551556
() -> api.addOobAuthenticator("mfaToken", null, null),
15561557
"'OOB channels' cannot be null!");
15571558
}
15581559

15591560
@SuppressWarnings("deprecation")
15601561
@Test
1561-
public void addOobAuthenticatorRequest() throws Exception {
1562+
public void addOobAuthenticatorDeprecatedThrowsWhenPhoneNumberNull() {
1563+
verifyThrows(IllegalArgumentException.class,
1564+
() -> api.addOobAuthenticator("mfaToken", Collections.singletonList("sms"), null),
1565+
"'phone number' cannot be null!");
1566+
}
1567+
1568+
@SuppressWarnings("deprecation")
1569+
@Test
1570+
public void addOobAuthenticatorDeprecatedRequest() throws Exception {
15621571
Request<CreatedOobResponse> request = api.addOobAuthenticator("mfaToken", Collections.singletonList("sms"), "phone-number");
15631572

15641573
server.jsonResponse(AUTH_OOB_AUTHENTICATOR_RESPONSE, 200);
@@ -1581,22 +1590,92 @@ public void addOobAuthenticatorRequest() throws Exception {
15811590
assertThat(response.getRecoveryCodes(), notNullValue());
15821591
}
15831592

1593+
@Test
1594+
public void addOobAuthenticatorThrowsWhenTokenNull() {
1595+
verifyThrows(IllegalArgumentException.class,
1596+
() -> api.addOobAuthenticator(null, Collections.singletonList("auth0"), null, null),
1597+
"'mfa token' cannot be null!");
1598+
}
1599+
1600+
1601+
@Test
1602+
public void addOobAuthenticatorThrowsWhenChannelsNull() {
1603+
verifyThrows(IllegalArgumentException.class,
1604+
() -> api.addOobAuthenticator("mfaToken", null, null, null),
1605+
"'OOB channels' cannot be null!");
1606+
}
1607+
15841608
@Test
15851609
public void addOobAuthenticatorThrowsWhenPhoneNumberNull() {
15861610
verifyThrows(IllegalArgumentException.class,
15871611
() -> api.addOobAuthenticator("mfaToken", Collections.singletonList("sms"), null, null),
15881612
"'phone number' cannot be null!");
15891613
}
15901614

1615+
@Test
1616+
public void addOobAuthenticatorThrowsWhenPhoneNumberNullWithVoiceChannel() {
1617+
verifyThrows(IllegalArgumentException.class,
1618+
() -> api.addOobAuthenticator("mfaToken", Collections.singletonList("voice"), null, null),
1619+
"'phone number' cannot be null!");
1620+
}
1621+
15911622
@Test
15921623
public void addOobAuthenticatorThrowsWhenEmailNull() {
15931624
verifyThrows(IllegalArgumentException.class,
15941625
() -> api.addOobAuthenticator("mfaToken", Collections.singletonList("email"), null, null),
15951626
"'email address' cannot be null!");
15961627
}
15971628

1629+
@Test
1630+
public void addOobAuthenticatorRequestWithPhoneNumber() throws Exception {
1631+
Request<CreatedOobResponse> request = api.addOobAuthenticator("mfaToken", Collections.singletonList("sms"), "phone-number", null);
1632+
1633+
server.jsonResponse(AUTH_OOB_AUTHENTICATOR_RESPONSE, 200);
1634+
CreatedOobResponse response = request.execute().getBody();
1635+
RecordedRequest recordedRequest = server.takeRequest();
1636+
1637+
assertThat(recordedRequest, hasMethodAndPath(HttpMethod.POST, "/mfa/associate"));
1638+
assertThat(recordedRequest, hasHeader("Content-Type", "application/json"));
1639+
1640+
Map<String, Object> body = bodyFromRequest(recordedRequest);
1641+
assertThat(body, hasEntry("authenticator_types", Collections.singletonList("oob")));
1642+
assertThat(body, hasEntry("oob_channels", Collections.singletonList("sms")));
1643+
assertThat(body, hasEntry("phone_number", "phone-number"));
1644+
1645+
assertThat(response, is(notNullValue()));
1646+
assertThat(response.getAuthenticatorType(), not(emptyOrNullString()));
1647+
assertThat(response.getOobChannel(), not(emptyOrNullString()));
1648+
assertThat(response.getOobCode(), not(emptyOrNullString()));
1649+
assertThat(response.getBarcodeUri(), not(emptyOrNullString()));
1650+
assertThat(response.getRecoveryCodes(), notNullValue());
1651+
}
1652+
15981653
@Test
15991654
public void addOobAuthenticatorRequestWithEmail() throws Exception {
1655+
Request<CreatedOobResponse> request = api.addOobAuthenticator("mfaToken", Collections.singletonList("email"), null, "email-address");
1656+
1657+
server.jsonResponse(AUTH_OOB_AUTHENTICATOR_RESPONSE, 200);
1658+
CreatedOobResponse response = request.execute().getBody();
1659+
RecordedRequest recordedRequest = server.takeRequest();
1660+
1661+
assertThat(recordedRequest, hasMethodAndPath(HttpMethod.POST, "/mfa/associate"));
1662+
assertThat(recordedRequest, hasHeader("Content-Type", "application/json"));
1663+
1664+
Map<String, Object> body = bodyFromRequest(recordedRequest);
1665+
assertThat(body, hasEntry("authenticator_types", Collections.singletonList("oob")));
1666+
assertThat(body, hasEntry("oob_channels", Collections.singletonList("email")));
1667+
assertThat(body, hasEntry("email", "email-address"));
1668+
1669+
assertThat(response, is(notNullValue()));
1670+
assertThat(response.getAuthenticatorType(), not(emptyOrNullString()));
1671+
assertThat(response.getOobChannel(), not(emptyOrNullString()));
1672+
assertThat(response.getOobCode(), not(emptyOrNullString()));
1673+
assertThat(response.getBarcodeUri(), not(emptyOrNullString()));
1674+
assertThat(response.getRecoveryCodes(), notNullValue());
1675+
}
1676+
1677+
@Test
1678+
public void addOobAuthenticatorRequestWithMultipleChannels() throws Exception {
16001679
Request<CreatedOobResponse> request = api.addOobAuthenticator("mfaToken", Arrays.asList("sms", "email"), "phone-number", "email-address");
16011680

16021681
server.jsonResponse(AUTH_OOB_AUTHENTICATOR_RESPONSE, 200);
@@ -1620,6 +1699,29 @@ public void addOobAuthenticatorRequestWithEmail() throws Exception {
16201699
assertThat(response.getRecoveryCodes(), notNullValue());
16211700
}
16221701

1702+
@Test
1703+
public void addOobAuthenticatorRequestWithAuth0Channel() throws Exception {
1704+
Request<CreatedOobResponse> request = api.addOobAuthenticator("mfaToken", Collections.singletonList("auth0"), null, "email-address");
1705+
1706+
server.jsonResponse(AUTH_OOB_AUTHENTICATOR_RESPONSE, 200);
1707+
CreatedOobResponse response = request.execute().getBody();
1708+
RecordedRequest recordedRequest = server.takeRequest();
1709+
1710+
assertThat(recordedRequest, hasMethodAndPath(HttpMethod.POST, "/mfa/associate"));
1711+
assertThat(recordedRequest, hasHeader("Content-Type", "application/json"));
1712+
1713+
Map<String, Object> body = bodyFromRequest(recordedRequest);
1714+
assertThat(body, hasEntry("authenticator_types", Collections.singletonList("oob")));
1715+
assertThat(body, hasEntry("oob_channels", Collections.singletonList("auth0")));
1716+
1717+
assertThat(response, is(notNullValue()));
1718+
assertThat(response.getAuthenticatorType(), not(emptyOrNullString()));
1719+
assertThat(response.getOobChannel(), not(emptyOrNullString()));
1720+
assertThat(response.getOobCode(), not(emptyOrNullString()));
1721+
assertThat(response.getBarcodeUri(), not(emptyOrNullString()));
1722+
assertThat(response.getRecoveryCodes(), notNullValue());
1723+
}
1724+
16231725
@Test
16241726
public void listAuthenticatorsThrowsWhenTokenNull() {
16251727
verifyThrows(IllegalArgumentException.class,

0 commit comments

Comments
 (0)