Skip to content

Commit 183bd83

Browse files
authored
fix: use postgres crypt to handle OTP hashing (#674)
### **PR Type** Enhancement, Bug fix ___ ### **Description** - Add configurable SMS provider flag - Simplify OTP generation and storage - Update SQL to cryptographically verify OTP - Refactor and export password hash verifier ___ ### Diagram Walkthrough ```mermaid flowchart LR A["CLI --sms-provider flag"] B["getSMS initializes provider"] C["Dev or Twilio backend"] D["SendVerificationCode generates OTP"] E["DB stores and verifies OTP using crypt"] A -- "provides flag" --> B B -- "selects provider" --> C C -- "sends code" --> D D -- "calls DB update" --> E ``` <details> <summary><h3> File Walkthrough</h3></summary> <table><thead><tr><th></th><th align="left">Relevant files</th></tr></thead><tbody><tr><td><strong>Enhancement</strong></td><td><details><summary>9 files</summary><table> <tr> <td><strong>email.go</strong><dd><code>Add SMS provider switch in getSMS</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://github.com/nhost/hasura-auth/pull/674/files#diff-a6364ed092c8bd789262322ec1e0108c274323462a0af0b7c7dd10a07f0bc449">+20/-11</a>&nbsp; </td> </tr> <tr> <td><strong>secrets.go</strong><dd><code>Export password hash verifier</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://github.com/nhost/hasura-auth/pull/674/files#diff-97ceb71f0d904089c77f432c1eb00e9b2b202b5b4ec191a52287386f06f0221b">+1/-1</a>&nbsp; &nbsp; &nbsp; </td> </tr> <tr> <td><strong>sign_in_email_password.go</strong><dd><code>Use exported VerifyHashPassword</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://github.com/nhost/hasura-auth/pull/674/files#diff-c10b97a61def73058ca42e90bcdd6eb373637816f1c2eb05f448887abea0c996">+1/-1</a>&nbsp; &nbsp; &nbsp; </td> </tr> <tr> <td><strong>sign_in_otp_email.go</strong><dd><code>Introduce generateOTP function</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://github.com/nhost/hasura-auth/pull/674/files#diff-ef51144d6b45769b4ac4e5bbe0531a8cc235ef35c8d44a655ec689c8a81a45a0">+13/-1</a>&nbsp; &nbsp; </td> </tr> <tr> <td><strong>dev.go</strong><dd><code>Add Dev SMS provider implementation</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://github.com/nhost/hasura-auth/pull/674/files#diff-875168f3184f1535f1a9893cd1c9f716e546b99057f4ce170b934d5ae661cac9">+32/-0</a>&nbsp; &nbsp; </td> </tr> <tr> <td><strong>sms.go</strong><dd><code>Simplify SMS OTP generation logic</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://github.com/nhost/hasura-auth/pull/674/files#diff-5c133e946d3d4678c97b6d28dfe7985b8f2b87a3f1fc26385bf07413e9508551">+13/-20</a>&nbsp; </td> </tr> <tr> <td><strong>twilio_sms.go</strong><dd><code>Remove OTP args in TwilioSMS constructor</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://github.com/nhost/hasura-auth/pull/674/files#diff-c61a03d180f37c02b5dfc5224be8d2cbf8511dd6782153a76d09d9ce0ac9eaf6">+0/-4</a>&nbsp; &nbsp; &nbsp; </td> </tr> <tr> <td><strong>sign_up_email_password.go</strong><dd><code>Remove OtpHash, set empty Otp field</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://github.com/nhost/hasura-auth/pull/674/files#diff-21603d0151e49e6bf3da5747febc7d63b05501ef9c49559202b906faba64809a">+2/-2</a>&nbsp; &nbsp; &nbsp; </td> </tr> <tr> <td><strong>sign_in_passwordless_email.go</strong><dd><code>Set empty Otp on signup with ticket</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://github.com/nhost/hasura-auth/pull/674/files#diff-75955329f33dc13dc34388566d2874aaf09cda6ed864be1775e04db48bd3029b">+2/-2</a>&nbsp; &nbsp; &nbsp; </td> </tr> </table></details></td></tr><tr><td><strong>Configuration changes</strong></td><td><details><summary>2 files</summary><table> <tr> <td><strong>serve.go</strong><dd><code>Add sms-provider CLI flag</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://github.com/nhost/hasura-auth/pull/674/files#diff-a900f3187c126bacf5c9c5b1745b5d14bc583c01ab8f1ca84ae449751c224b68">+8/-0</a>&nbsp; &nbsp; &nbsp; </td> </tr> <tr> <td><strong>.golangci.yaml</strong><dd><code>Add goconst linter for tests</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://github.com/nhost/hasura-auth/pull/674/files#diff-9917ddc9f1c3304218f7269265b746d997c5c0615478177b5fceecd33ef47cb5">+1/-0</a>&nbsp; &nbsp; &nbsp; </td> </tr> </table></details></td></tr><tr><td><strong>Cleanup</strong></td><td><details><summary>3 files</summary><table> <tr> <td><strong>workflows_tickets.go</strong><dd><code>Remove legacy OTP generator and hasher</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://github.com/nhost/hasura-auth/pull/674/files#diff-a4d16eaab24ba5b8c7b93fb112b8dd7a7ff7a70308e2d334b1ba6ba38df8b1ee">+0/-27</a>&nbsp; &nbsp; </td> </tr> <tr> <td><strong>controller.go</strong><dd><code>Remove obsolete OTP interface method</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://github.com/nhost/hasura-auth/pull/674/files#diff-68eba6c5b3be94c2016a5c821351ad07c60e395226594ff744901f759e22af15">+0/-3</a>&nbsp; &nbsp; &nbsp; </td> </tr> <tr> <td><strong>controller.go</strong><dd><code>Remove mocks for phone OTP method</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://github.com/nhost/hasura-auth/pull/674/files#diff-f1b1f168b0924b023c571d6274d53defef472a872f5fee4de1c4ae78959cc327">+0/-30</a>&nbsp; &nbsp; </td> </tr> </table></details></td></tr><tr><td><strong>Bug fix</strong></td><td><details><summary>3 files</summary><table> <tr> <td><strong>query.sql.go</strong><dd><code>Update GetUserByPhoneNumberAndOTP crypt logic</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://github.com/nhost/hasura-auth/pull/674/files#diff-442fde6e20ac506fb6bc13c00a7374ee9c33b183ffda72db4e49e1b013cd4cde">+17/-11</a>&nbsp; </td> </tr> <tr> <td><strong>query.sql</strong><dd><code>Align OTP SQL with crypt-based check</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://github.com/nhost/hasura-auth/pull/674/files#diff-c19dd96472c44a7389d4d1f72e0a1879df1e06e8e4d333ca3cb6b6dfbe3083ef">+10/-4</a>&nbsp; &nbsp; </td> </tr> <tr> <td><strong>sign_in_passwordless_sms.go</strong><dd><code>Use raw OTP instead of stored hash</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://github.com/nhost/hasura-auth/pull/674/files#diff-27f138a5979edb2e311a55e08f902030c7c1ad35ab4600c8e0b41b4c03a70b8d">+9/-13</a>&nbsp; &nbsp; </td> </tr> </table></details></td></tr><tr><td><strong>Documentation</strong></td><td><details><summary>1 files</summary><table> <tr> <td><strong>cli.md</strong><dd><code>Document --sms-provider option</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://github.com/nhost/hasura-auth/pull/674/files#diff-27c8c22b45605a0ddf42ce8017b6b2a68cb3b10534c18bf4a781e230d7b92361">+3/-0</a>&nbsp; &nbsp; &nbsp; </td> </tr> </table></details></td></tr><tr><td><strong>Tests</strong></td><td><details><summary>2 files</summary><table> <tr> <td><strong>sign_in_passwordless_sms_test.go</strong><dd><code>Update tests for OTP vs hash</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://github.com/nhost/hasura-auth/pull/674/files#diff-b51c9ca27064e2579444e6eecca4908c5c5f0f3d567b9a125406ca07f00354a7">+36/-36</a>&nbsp; </td> </tr> <tr> <td><strong>sign_up_email_password_test.go</strong><dd><code>Update tests to expect Otp field</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://github.com/nhost/hasura-auth/pull/674/files#diff-2cd7cf89bd4d3e6fdfacd10d66740c02196b2bee4f9ec44409c4ad6a77d53685">+6/-6</a>&nbsp; &nbsp; &nbsp; </td> </tr> </table></details></td></tr><tr><td><strong>Additional files</strong></td><td><details><summary>7 files</summary><table> <tr> <td><strong>elevate_webauthn_test.go</strong></td> <td><a href="https://github.com/nhost/hasura-auth/pull/674/files#diff-47ef225c96c0aa63c67e926b18c922a14e7ccbd71145ffe646da196d3cc13880">+1/-1</a>&nbsp; &nbsp; &nbsp; </td> </tr> <tr> <td><strong>refresh_token_test.go</strong></td> <td><a href="https://github.com/nhost/hasura-auth/pull/674/files#diff-7c06d2d7bbfc744dd9feaa835b3ff8f2856304325c0fe45035bd9c655b2086d8">+1/-1</a>&nbsp; &nbsp; &nbsp; </td> </tr> <tr> <td><strong>sign_in_otp_email_test.go</strong></td> <td><a href="https://github.com/nhost/hasura-auth/pull/674/files#diff-aff8747c582d84badc6a27e1f2f88dd91761ca1901f983908aab20f02c7eb5e3">+6/-6</a>&nbsp; &nbsp; &nbsp; </td> </tr> <tr> <td><strong>sign_in_passwordless_email_test.go</strong></td> <td><a href="https://github.com/nhost/hasura-auth/pull/674/files#diff-f8cb52277c756ae8dfb128d6cf53df90a0c8e3506fe8402eb9d77a7658138678">+6/-6</a>&nbsp; &nbsp; &nbsp; </td> </tr> <tr> <td><strong>sign_in_provider_callback_get_test.go</strong></td> <td><a href="https://github.com/nhost/hasura-auth/pull/674/files#diff-b46929201c1137439ed7682d5965a96d427110015ef31eb50fd3c86de3814d55">+1/-1</a>&nbsp; &nbsp; &nbsp; </td> </tr> <tr> <td><strong>verify_elevate_webauthn_test.go</strong></td> <td><a href="https://github.com/nhost/hasura-auth/pull/674/files#diff-8f96cae3346b1cd7cddadcb3d588a05dcf51bf7d814fa9ae6817ed776a6ccbca">+2/-2</a>&nbsp; &nbsp; &nbsp; </td> </tr> <tr> <td><strong>gocmp.go</strong></td> <td><a href="https://github.com/nhost/hasura-auth/pull/674/files#diff-27aaac461c10f73569f7c93a2c299818d040c491f1fbd5279c682f5488dc65ed">+1/-1</a>&nbsp; &nbsp; &nbsp; </td> </tr> </table></details></td></tr></tr></tbody></table> </details> ___
1 parent ccbc12f commit 183bd83

25 files changed

+190
-188
lines changed

.golangci.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ linters:
2929
- linters:
3030
- funlen
3131
- ireturn
32+
- goconst
3233
path: _test\.go
3334
- linters:
3435
- lll

docs/cli.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ auth
112112
[--require-elevated-claim]=[value]
113113
[--server-url]=[value]
114114
[--sms-passwordless-enabled]
115+
[--sms-provider]=[value]
115116
[--sms-twilio-account-sid]=[value]
116117
[--sms-twilio-auth-token]=[value]
117118
[--sms-twilio-messaging-service-id]=[value]
@@ -375,6 +376,8 @@ auth [GLOBAL OPTIONS] [command [COMMAND OPTIONS]] [ARGUMENTS...]
375376

376377
**--sms-passwordless-enabled**: Enable SMS passwordless authentication
377378

379+
**--sms-provider**="": SMS provider (twilio or modica) (default: twilio)
380+
378381
**--sms-twilio-account-sid**="": Twilio Account SID for SMS
379382

380383
**--sms-twilio-auth-token**="": Twilio Auth Token for SMS

go/cmd/email.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,26 @@ func getSMS( //nolint:ireturn
111111
return nil, nil //nolint:nilnil // SMS disabled, return nil client
112112
}
113113

114+
provider := strings.ToLower(cmd.String(flagSMSProvider))
115+
if provider == "" {
116+
provider = "twilio" // Default to Twilio for backward compatibility
117+
}
118+
119+
switch strings.ToLower(cmd.String(flagSMSProvider)) {
120+
case "twilio":
121+
return getTwilioSMS(cmd, templates, db)
122+
case "dev":
123+
return sms.NewDev(templates, db, logger), nil
124+
default:
125+
return nil, fmt.Errorf("unsupported SMS provider: %s", provider) //nolint:err113
126+
}
127+
}
128+
129+
func getTwilioSMS( //nolint:ireturn
130+
cmd *cli.Command,
131+
templates *notifications.Templates,
132+
db *sql.Queries,
133+
) (controller.SMSer, error) {
114134
accountSid := cmd.String(flagSMSTwilioAccountSid)
115135
authToken := cmd.String(flagSMSTwilioAuthToken)
116136
messagingServiceID := cmd.String(flagSMSTwilioMessagingServiceID)
@@ -126,19 +146,8 @@ func getSMS( //nolint:ireturn
126146
), nil
127147
}
128148

129-
if templates == nil {
130-
var err error
131-
132-
templates, err = getTemplates(cmd, logger)
133-
if err != nil {
134-
return nil, fmt.Errorf("problem creating templates: %w", err)
135-
}
136-
}
137-
138149
return sms.NewTwilioSMS(
139150
templates,
140-
controller.GenerateOTP,
141-
controller.HashOTP,
142151
accountSid,
143152
authToken,
144153
messagingServiceID,

go/cmd/serve.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ const (
9595
flagGoogleAudience = "google-audience"
9696
flagOTPEmailEnabled = "otp-email-enabled"
9797
flagSMSPasswordlessEnabled = "sms-passwordless-enabled"
98+
flagSMSProvider = "sms-provider"
9899
flagSMSTwilioAccountSid = "sms-twilio-account-sid"
99100
flagSMSTwilioAuthToken = "sms-twilio-auth-token" //nolint:gosec
100101
flagSMSTwilioMessagingServiceID = "sms-twilio-messaging-service-id"
@@ -665,6 +666,13 @@ func CommandServe() *cli.Command { //nolint:funlen,maintidx
665666
Category: "sms",
666667
Sources: cli.EnvVars("AUTH_SMS_PASSWORDLESS_ENABLED"),
667668
},
669+
&cli.StringFlag{ //nolint: exhaustruct
670+
Name: flagSMSProvider,
671+
Usage: "SMS provider (twilio or modica)",
672+
Category: "sms",
673+
Value: "twilio",
674+
Sources: cli.EnvVars("AUTH_SMS_PROVIDER"),
675+
},
668676
&cli.StringFlag{ //nolint: exhaustruct
669677
Name: flagSMSTwilioAccountSid,
670678
Usage: "Twilio Account SID for SMS",

go/controller/controller.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,6 @@ type DBClientGetUser interface {
5858
GetUserByEmailAndTicket(
5959
ctx context.Context, arg sql.GetUserByEmailAndTicketParams,
6060
) (sql.AuthUser, error)
61-
GetUserByPhoneNumberAndOTP(
62-
ctx context.Context, arg sql.GetUserByPhoneNumberAndOTPParams,
63-
) (sql.AuthUser, error)
6461
}
6562

6663
type DBClientInsertUser interface {

go/controller/elevate_webauthn_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestElevateWebauthn(t *testing.T) {
4646
}
4747
}
4848

49-
credentialIDString := "EuKJAraRGDcmHon-EjDoqoU5Yvk" //nolint:gosec,goconst,nolintlint
49+
credentialIDString := "EuKJAraRGDcmHon-EjDoqoU5Yvk" //nolint:gosec
5050

5151
var credentialID protocol.URLEncodedBase64
5252
if err := credentialID.UnmarshalJSON([]byte(credentialIDString)); err != nil {

go/controller/mock/controller.go

Lines changed: 0 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/controller/refresh_token_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func TestRefreshToken(t *testing.T) { //nolint:maintidx
5555

5656
userID := uuid.MustParse("db477732-48fa-4289-b694-2886a646b6eb")
5757
token := uuid.MustParse("1fb17604-86c7-444e-b337-09a644465f2d")
58-
hashedToken := `\x9698157153010b858587119503cbeef0cf288f11775e51cdb6bfd65e930d9310` //nolint:goconst
58+
hashedToken := `\x9698157153010b858587119503cbeef0cf288f11775e51cdb6bfd65e930d9310`
5959
newTokenID := uuid.MustParse("1fb13604-86c7-4444-a337-09a644465f2d")
6060

6161
cases := []testRequest[api.RefreshTokenRequestObject, api.RefreshTokenResponseObject]{

go/controller/sign_in_otp_email.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,26 @@ package controller
22

33
import (
44
"context"
5+
"crypto/rand"
6+
"fmt"
57
"log/slog"
8+
"math/big"
69
"time"
710

811
"github.com/nhost/hasura-auth/go/api"
912
"github.com/nhost/hasura-auth/go/middleware"
1013
"github.com/nhost/hasura-auth/go/notifications"
1114
)
1215

16+
func generateOTP() (string, error) {
17+
n, err := rand.Int(rand.Reader, big.NewInt(1000000)) //nolint:mnd
18+
if err != nil {
19+
return "", fmt.Errorf("error generating OTP: %w", err)
20+
}
21+
22+
return fmt.Sprintf("%06d", n), nil
23+
}
24+
1325
func (ctrl *Controller) SignInOTPEmail( //nolint:ireturn
1426
ctx context.Context,
1527
request api.SignInOTPEmailRequestObject,
@@ -28,7 +40,7 @@ func (ctrl *Controller) SignInOTPEmail( //nolint:ireturn
2840
return ctrl.respondWithError(apiErr), nil
2941
}
3042

31-
otp, _, err := GenerateOTP()
43+
otp, err := generateOTP()
3244
if err != nil {
3345
logger.ErrorContext(ctx, "error generating OTP", logError(err))
3446
return ctrl.sendError(ErrInternalServerError), nil

go/controller/sign_in_otp_email_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ func TestSignInOTPEmail(t *testing.T) { //nolint:maintidx
5151
DefaultRole: "user",
5252
Metadata: []byte("null"),
5353
Roles: []string{"user", "me"},
54-
PhoneNumber: pgtype.Text{}, //nolint:exhaustruct
55-
OtpHash: pgtype.Text{}, //nolint:exhaustruct
54+
PhoneNumber: pgtype.Text{}, //nolint:exhaustruct
55+
Otp: "",
5656
OtpHashExpiresAt: pgtype.Timestamptz{}, //nolint:exhaustruct
5757
OtpMethodLastUsed: pgtype.Text{}, //nolint:exhaustruct
5858
},
@@ -222,8 +222,8 @@ func TestSignInOTPEmail(t *testing.T) { //nolint:maintidx
222222
DefaultRole: "user",
223223
Metadata: []byte("null"),
224224
Roles: []string{"user", "me"},
225-
PhoneNumber: pgtype.Text{}, //nolint:exhaustruct
226-
OtpHash: pgtype.Text{}, //nolint:exhaustruct
225+
PhoneNumber: pgtype.Text{}, //nolint:exhaustruct
226+
Otp: "",
227227
OtpHashExpiresAt: pgtype.Timestamptz{}, //nolint:exhaustruct
228228
OtpMethodLastUsed: pgtype.Text{}, //nolint:exhaustruct
229229
},
@@ -347,8 +347,8 @@ func TestSignInOTPEmail(t *testing.T) { //nolint:maintidx
347347
DefaultRole: "user",
348348
Metadata: []byte(`{"asd":"asd"}`),
349349
Roles: []string{"user"},
350-
PhoneNumber: pgtype.Text{}, //nolint:exhaustruct
351-
OtpHash: pgtype.Text{}, //nolint:exhaustruct
350+
PhoneNumber: pgtype.Text{}, //nolint:exhaustruct
351+
Otp: "",
352352
OtpHashExpiresAt: pgtype.Timestamptz{}, //nolint:exhaustruct
353353
OtpMethodLastUsed: pgtype.Text{}, //nolint:exhaustruct
354354
},

0 commit comments

Comments
 (0)