-
Notifications
You must be signed in to change notification settings - Fork 756
feat(otp): handle test OTPs and Twilio Verify on the one_time_tokens path #2798
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
annabkr
wants to merge
2
commits into
annabaker/auth-1553-switch-one_time_tokens-table-to-source-of-truth
Choose a base branch
from
annabaker/auth-1553-ott-phone-providers
base: annabaker/auth-1553-switch-one_time_tokens-table-to-source-of-truth
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,8 +9,11 @@ import ( | |
|
|
||
| "github.com/gofrs/uuid" | ||
| "github.com/stretchr/testify/require" | ||
| "gopkg.in/h2non/gock.v1" | ||
|
|
||
| "github.com/supabase/auth/internal/api/apierrors" | ||
| "github.com/supabase/auth/internal/api/sms_provider" | ||
| "github.com/supabase/auth/internal/conf" | ||
| "github.com/supabase/auth/internal/crypto" | ||
| mail "github.com/supabase/auth/internal/mailer" | ||
| "github.com/supabase/auth/internal/models" | ||
|
|
@@ -21,12 +24,13 @@ import ( | |
| // | ||
| // These tests run every flow once per store with identical seeding and ensure the outcome is equal. | ||
| const ( | ||
| parityOTP = "123456" | ||
| parityEmail = "test@example.com" | ||
| parityPhone = "12345678" | ||
| parityNewEmail = "new@example.com" | ||
| parityNewPhone = "1234567890" | ||
| parityForbidden = "Token has expired or is invalid" | ||
| parityOTP = "123456" | ||
| parityEmail = "test@example.com" | ||
| parityPhone = "12345678" | ||
| parityNewEmail = "new@example.com" | ||
| parityNewPhone = "1234567890" | ||
| parityForbidden = "Token has expired or is invalid" | ||
| twilioServiceSid = "VA-parity-test" | ||
| ) | ||
|
|
||
| // otpParityOutcome is everything a client or an operator can observe after a | ||
|
|
@@ -260,6 +264,58 @@ func (ts *VerifyTestSuite) TestVerifyOTPParityPhoneFlows() { | |
| requestBody: phoneOTPBody(phoneChangeVerification, parityNewPhone), | ||
| expected: phoneChanged, | ||
| }, | ||
| // A test OTP is accepted without any stored challenge. This is the | ||
| // path app store reviewers and CI rely on. | ||
| "sms with a test OTP succeeds with no stored challenge": { | ||
| configure: func() func() { | ||
| return ts.configureTestOTP(parityPhone, parityOTP) | ||
| }, | ||
| requestBody: phoneOTPBody(smsVerification, parityPhone), | ||
| expected: phoneSignedUp, | ||
| }, | ||
| "sms with a wrong code falls through the test OTP check and is rejected": { | ||
| configure: func() func() { | ||
| return ts.configureTestOTP(parityPhone, "000000") | ||
| }, | ||
| requestBody: phoneOTPBody(smsVerification, parityPhone), | ||
| expected: forbidden, | ||
| }, | ||
| // Twilio Verify generates and delivers its own code, so the locally | ||
| // stored hash never matches what the user types. Twilio's answer is the | ||
| // only thing that counts. | ||
| "sms with Twilio Verify accepts a code Twilio approves": { | ||
| configure: func() func() { | ||
| return ts.configureTwilioVerify(map[string]interface{}{"status": "approved", "valid": true}) | ||
| }, | ||
| seed: func(u *models.User) { | ||
| ts.seedChallenge(u, models.ConfirmationToken, parityPhone, crypto.GenerateTokenHash(parityPhone, "999999"), now, time.Hour) | ||
| }, | ||
| requestBody: phoneOTPBody(smsVerification, parityPhone), | ||
| expected: phoneSignedUp, | ||
| }, | ||
| "sms with Twilio Verify rejects a code Twilio does not approve": { | ||
| configure: func() func() { | ||
| return ts.configureTwilioVerify(map[string]interface{}{"status": "pending", "valid": false}) | ||
| }, | ||
| seed: func(u *models.User) { | ||
| ts.seedChallenge(u, models.ConfirmationToken, parityPhone, phoneHash, now, time.Hour) | ||
| }, | ||
| requestBody: phoneOTPBody(smsVerification, parityPhone), | ||
| expected: forbidden, | ||
| }, | ||
| // An SSO user has no local credentials to verify. Twilio approves the | ||
| // code here, so only the SSO check can reject the request. | ||
| "sms with Twilio Verify rejects an SSO user": { | ||
| configure: func() func() { | ||
| return ts.configureTwilioVerify(map[string]interface{}{"status": "approved", "valid": true}) | ||
| }, | ||
| seed: func(u *models.User) { | ||
| u.IsSSOUser = true | ||
| ts.seedChallenge(u, models.ConfirmationToken, parityPhone, phoneHash, now, time.Hour) | ||
| }, | ||
| requestBody: phoneOTPBody(smsVerification, parityPhone), | ||
| expected: forbidden, | ||
| }, | ||
| } | ||
|
|
||
| ts.runOTPParityCases(cases) | ||
|
|
@@ -336,8 +392,8 @@ func (ts *VerifyTestSuite) saveUser(u *models.User) { | |
|
|
||
| // seedChallenge stores hash in the users column and the one_time_tokens row | ||
| // for tokenType, mirroring what the send paths write. relatesTo is the address | ||
| // or number the code was sent to. It persists u, so it also saves any other | ||
| // change the case made. | ||
| // or number the code was sent to; the Twilio Verify path finds the row by it. | ||
| // It persists u, so it also saves any other change the case made. | ||
| func (ts *VerifyTestSuite) seedChallenge(u *models.User, tokenType models.OneTimeTokenType, relatesTo, hash string, sentAt time.Time, validity time.Duration) { | ||
| switch tokenType { | ||
| case models.ConfirmationToken: | ||
|
|
@@ -488,6 +544,42 @@ func (ts *VerifyTestSuite) responseMsg(w *httptest.ResponseRecorder) string { | |
| return body.Msg | ||
| } | ||
|
|
||
| func (ts *VerifyTestSuite) configureTestOTP(phone, otp string) func() { | ||
| previous := ts.Config.Sms.TestOTP | ||
| ts.Config.Sms.TestOTP = map[string]string{phone: otp} | ||
| return func() { ts.Config.Sms.TestOTP = previous } | ||
| } | ||
|
Comment on lines
+547
to
+551
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This pattern just lets us set + unset a config value. |
||
|
|
||
| // configureTwilioVerify switches the SMS provider to Twilio Verify and arms a | ||
| // single mocked VerificationCheck response. | ||
| func (ts *VerifyTestSuite) configureTwilioVerify(response map[string]interface{}) func() { | ||
| previousProvider := ts.Config.Sms.Provider | ||
| previousTwilio := ts.Config.Sms.TwilioVerify | ||
| previousMock := sms_provider.MockProvider | ||
|
|
||
| ts.Config.Sms.Provider = "twilio_verify" | ||
| ts.Config.Sms.TwilioVerify = conf.TwilioVerifyProviderConfiguration{ | ||
| AccountSid: "AC-parity-test", | ||
| AuthToken: "parity-test-token", | ||
| MessageServiceSid: twilioServiceSid, | ||
| } | ||
| // The mock provider would short-circuit GetSmsProvider and never reach | ||
| // the Twilio Verify type assertion. | ||
| sms_provider.MockProvider = nil | ||
|
|
||
| gock.New("https://verify.twilio.com/v2/Services/" + twilioServiceSid + "/VerificationCheck"). | ||
| Post(""). | ||
| Reply(http.StatusOK). | ||
| JSON(response) | ||
|
|
||
| return func() { | ||
| gock.OffAll() | ||
| sms_provider.MockProvider = previousMock | ||
| ts.Config.Sms.TwilioVerify = previousTwilio | ||
| ts.Config.Sms.Provider = previousProvider | ||
| } | ||
| } | ||
|
|
||
| func emailOTPBody(verifyType, email string) map[string]interface{} { | ||
| return map[string]interface{}{ | ||
| "type": verifyType, | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I liked this better, and the
_, oksyntax is more idiomatic.