diff --git a/src/main/java/org/opentripplanner/middleware/controllers/api/OtpUserController.java b/src/main/java/org/opentripplanner/middleware/controllers/api/OtpUserController.java index 5d29fdccf..ac279122c 100644 --- a/src/main/java/org/opentripplanner/middleware/controllers/api/OtpUserController.java +++ b/src/main/java/org/opentripplanner/middleware/controllers/api/OtpUserController.java @@ -16,6 +16,7 @@ import spark.Request; import spark.Response; +import java.util.Date; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -116,6 +117,7 @@ public VerificationResult sendVerificationText(Request req, Response res) { if (verification.getStatus().equals("pending")) { otpUser.phoneNumber = phoneNumber; otpUser.isPhoneNumberVerified = false; + otpUser.smsConsentDate = new Date(); otpUser.notificationChannel.add(OtpUser.Notification.SMS); Persistence.otpUsers.replace(otpUser.id, otpUser); } diff --git a/src/main/java/org/opentripplanner/middleware/models/OtpUser.java b/src/main/java/org/opentripplanner/middleware/models/OtpUser.java index 7fd236861..bf0700aef 100644 --- a/src/main/java/org/opentripplanner/middleware/models/OtpUser.java +++ b/src/main/java/org/opentripplanner/middleware/models/OtpUser.java @@ -47,6 +47,14 @@ public enum Notification { */ public String phoneNumber; + /** + * The date when consent was given by user to receive SMS messages, as required by Twilio, + * see https://www.twilio.com/docs/verify/sms#consent-and-opt-in-policy. + * If the user starts the phone verification process, this field is populated + * just before the verification code is sent. + */ + public Date smsConsentDate; + /** * The user's preferred locale, in language tag format * e.g. 'en-US', 'fr-FR', 'es-ES', 'zh-CN', etc. diff --git a/src/test/java/org/opentripplanner/middleware/utils/NotificationUtilsTest.java b/src/test/java/org/opentripplanner/middleware/utils/NotificationUtilsTest.java index 261adced1..f622c23c9 100644 --- a/src/test/java/org/opentripplanner/middleware/utils/NotificationUtilsTest.java +++ b/src/test/java/org/opentripplanner/middleware/utils/NotificationUtilsTest.java @@ -13,6 +13,7 @@ import org.slf4j.LoggerFactory; import java.io.IOException; +import java.util.Date; import static org.junit.jupiter.api.Assumptions.assumeTrue; import static org.opentripplanner.middleware.testutils.PersistenceTestUtils.createUser; @@ -94,12 +95,17 @@ public void canSendSmsNotification() { */ @Test public void canSendTwilioVerificationText() { + Assertions.assertNull(user.smsConsentDate); + Date beforeVerificationDate = new Date(); Verification verification = NotificationUtils.sendVerificationText( // Note: phone number is configured in setup method above. user.phoneNumber ); LOG.info("Verification status: {}", verification.getStatus()); Assertions.assertNotNull(verification.getSid()); + Date afterVerificationDate = new Date(); + Assertions.assertTrue(user.smsConsentDate.getTime() >= beforeVerificationDate.getTime()); + Assertions.assertTrue(user.smsConsentDate.getTime() <= afterVerificationDate.getTime()); } /**