Skip to content

Commit ac2abdf

Browse files
author
Michael Mroz
committed
Revised comments and name for confirmation overload, added tests
1 parent 31a5c25 commit ac2abdf

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

src/main/java/com/auth0/guardian/Guardian.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,6 @@ public Transaction requestEnroll(String ticket, EnrollmentType type)
8686
* transaction initiated with {@link EnrollmentType#TOTP()}) or when the user received the OTP code delivered to his
8787
* phone number by SMS (for a transaction initiated with {@link EnrollmentType#SMS(String)}).
8888
*
89-
* This method can be used in stateful applications where a {@link Transaction} is preserved in memory between user
90-
* interactions.
91-
*
9289
* @param transaction the enrollment transaction
9390
* @param otp the code obtained from the TOTP app or delivered to the phone number by SMS
9491
* @return extra information about the enrollment, like the recovery code
@@ -119,16 +116,16 @@ public Enrollment confirmEnroll(Transaction transaction, String otp)
119116
* transaction initiated with {@link EnrollmentType#TOTP()}) or when the user received the OTP code delivered to his
120117
* phone number by SMS (for a transaction initiated with {@link EnrollmentType#SMS(String)}).
121118
*
122-
* This method can be used in stateless applications where {@link Transaction} may not be preserved between user
123-
* interactions.
119+
* This overload is intended for stateless applications where {@link java.io.Serializable} is not acceptable,
120+
* avoiding the necessity of utilising poor practises to preserve {@link Transaction} between user actions.
124121
*
125122
* @param transactionToken the token associated with the transaction to confirm.
126123
* @param otp the code obtained from the TOTP app or delivered to the phone number by SMS
127124
* @throws IOException when there's a connection issue
128125
* @throws IllegalArgumentException when the transaction is not valid
129126
* @throws GuardianException when there's a Guardian specific issue (invalid otp for example)
130127
*/
131-
public void confirmEnrollStateless(String transactionToken, String otp)
128+
public void confirmEnroll(String transactionToken, String otp)
132129
throws IOException, IllegalArgumentException, GuardianException {
133130
if (transactionToken == null) {
134131
throw new IllegalArgumentException("Invalid enrollment transaction");

src/test/java/com/auth0/guardian/GuardianTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,4 +264,57 @@ public void shouldFailConfirmationWhenOtpIsNull() throws Exception {
264264
guardian
265265
.confirmEnroll(new Transaction("TRANSACTION_TOKEN", null, null), null);
266266
}
267+
268+
@Test
269+
public void shouldConfirmEnrollOverload() throws Exception {
270+
server.jsonResponse(MockServer.START_FLOW_VALID, 201);
271+
server.emptyResponse();
272+
273+
Transaction transaction = guardian
274+
.requestEnroll(ENROLLMENT_TICKET, EnrollmentType.TOTP());
275+
276+
guardian
277+
.confirmEnroll(transaction.getTransactionToken(), OTP_CODE);
278+
279+
RecordedRequest startFlowRequest = server.takeRequest();
280+
281+
assertThat(startFlowRequest, hasMethodAndPath("POST", "/api/start-flow"));
282+
assertThat(startFlowRequest, hasHeader("Content-Type", "application/json; charset=utf-8"));
283+
assertThat(startFlowRequest, hasHeader("Authorization", "Ticket id=\"ENROLLMENT_TICKET\""));
284+
285+
Map<String, Object> startFlowBody = bodyFromRequest(startFlowRequest);
286+
assertThat(startFlowBody, hasEntry("state_transport", (Object) "polling"));
287+
288+
RecordedRequest verifyOtpRequest = server.takeRequest();
289+
290+
assertThat(verifyOtpRequest, hasMethodAndPath("POST", "/api/verify-otp"));
291+
assertThat(verifyOtpRequest, hasHeader("Content-Type", "application/json; charset=utf-8"));
292+
assertThat(verifyOtpRequest, hasHeader("Authorization", "Bearer THE_TRANSACTION_TOKEN"));
293+
294+
Map<String, Object> verifyOtpBody = bodyFromRequest(verifyOtpRequest);
295+
assertThat(verifyOtpBody, hasEntry("type", (Object) "manual_input"));
296+
assertThat(verifyOtpBody, hasEntry("code", (Object) "OTP_CODE"));
297+
}
298+
299+
@Test
300+
public void shouldFailConfirmationOverloadWhenNoTokenIsProvided() throws Exception {
301+
exception.expect(IllegalArgumentException.class);
302+
exception.expectMessage("Invalid enrollment transaction");
303+
304+
server.emptyResponse();
305+
306+
guardian
307+
.confirmEnroll((String)null, OTP_CODE);
308+
}
309+
310+
@Test
311+
public void shouldFailConfirmationOverloadWhenOtpIsNull() throws Exception {
312+
exception.expect(IllegalArgumentException.class);
313+
exception.expectMessage("Invalid OTP");
314+
315+
server.emptyResponse();
316+
317+
guardian
318+
.confirmEnroll("TRANSACTION_TOKEN", null);
319+
}
267320
}

0 commit comments

Comments
 (0)