Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions internal/handlers/send_otp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package handlers

import (
"context"
"errors"
"fmt"

"github.com/ayush00git/goth/grpc/goth"
"github.com/ayush00git/goth/internal/helpers"
"github.com/ayush00git/goth/internal/interceptors"
"github.com/ayush00git/goth/internal/services"
"github.com/jackc/pgx/v5"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -124,7 +126,25 @@ func (g *GothServer) SendOTP(ctx context.Context, req *goth.SendOTPRequest) (*go
if err := g.otpCooldown(ctx, "password_reset:"+email); err != nil {
return nil, err
}


// no account: return the same success response without sending the mail.
// so the caller can't tell which emails are registered.
var exists bool
err := g.db.QueryRow(
ctx,
`SELECT EXISTS (SELECT 1 FROM users WHERE email = $1)`,
email,
).Scan(&exists)
if err != nil {
return nil, status.Error(codes.Internal, "database error")
}
if !exists {
return &goth.SendOTPResponse{
Success: true,
ExpiresInSeconds: int32(otpTTL.Seconds()),
}, nil
}

// generate + hash the otp and then store to redis.
otp, err := helpers.GenerateOTP()
if err != nil {
Expand Down Expand Up @@ -171,23 +191,28 @@ func (g *GothServer) SendOTP(ctx context.Context, req *goth.SendOTPRequest) (*go
// get the email once again.
email := req.Email

// check if the user's account is already verified.
// cooldown first, so unknown and registered emails behave the same.
if err := g.otpCooldown(ctx, "acc_verify:"+email); err != nil {
return nil, err
}

// no account or already verified: return the same success response
// without sending, so the caller can't tell which emails are registered.
var isVerified bool
err := g.db.QueryRow(
ctx,
`SELECT email_verified FROM users
WHERE email = $1`,
email,
).Scan(&isVerified)
if err != nil {
return nil, status.Error(codes.Internal, "failed sending email to user")
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
return nil, status.Error(codes.Internal, "database error")
}
if isVerified == true {
return nil, status.Error(codes.AlreadyExists, "your account is already verified")
}

if err := g.otpCooldown(ctx, "acc_verify:"+email); err != nil {
return nil, err
if errors.Is(err, pgx.ErrNoRows) || isVerified {
return &goth.SendOTPResponse{
Success: true,
ExpiresInSeconds: int32(otpTTL.Seconds()),
}, nil
}

otp, err := helpers.GenerateOTP()
Expand Down
4 changes: 3 additions & 1 deletion internal/services/access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func RefreshAccessToken(refreshToken string) (string, error) {
// values to generate a new access token
token, err := jwt.ParseWithClaims(refreshToken, &Claims{},
func(token *jwt.Token) (interface{}, error) {
return refreshSecret, nil
return []byte(refreshSecret), nil
},
)
if err != nil {
Expand Down Expand Up @@ -95,3 +95,5 @@ func RefreshAccessToken(refreshToken string) (string, error) {
}
return accessTokenString, nil
}
// this is a unused function, the tasks are very safely and accurately handled by
// the ValidateRefreshToken, not removing it for now. but just "it is unused."
7 changes: 6 additions & 1 deletion scripts/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ expect_err "verify-otp without email" InvalidArgument verify-otp --purpose 2 --c
expect_err "verify-otp with wrong code" PermissionDenied verify-otp --purpose 2 --email "$EMAIL" --code 000000
expect_ok "verify-otp with mailed code" verify-otp --purpose 2 --email "$EMAIL" --code "$SIGNUP_CODE"
expect_err "verify-otp reusing the code" NotFound verify-otp --purpose 2 --email "$EMAIL" --code "$SIGNUP_CODE"
expect_err "send verification to verified account" AlreadyExists send-otp --purpose 2 --email "$EMAIL"
GHOST_EMAIL=$(email_for no-account)
expect_ok "send verification to unknown email looks the same" send-otp --purpose 2 --email "$GHOST_EMAIL"
expect_no_new_mail "no verification mail sent to unknown email" "$GHOST_EMAIL"
expect_err "unknown email resend within cooldown" ResourceExhausted send-otp --purpose 2 --email "$GHOST_EMAIL"
expect_err "verify-otp with MFA purpose" InvalidArgument verify-otp --purpose 0 --email "$EMAIL" --code 123456
expect_err "verify-otp with unknown purpose" InvalidArgument verify-otp --purpose 9 --email "$EMAIL" --code 123456

Expand Down Expand Up @@ -191,6 +194,8 @@ new_user reset
expect_ok "send reset code" send-otp --purpose 1 --email "$RESET_EMAIL"
mail_code "$RESET_EMAIL"; RESET_CODE=$MAIL_CODE
expect_err "resend reset code within cooldown" ResourceExhausted send-otp --purpose 1 --email "$RESET_EMAIL"
expect_ok "send reset code to unknown email looks the same" send-otp --purpose 1 --email "$GHOST_EMAIL"
expect_no_new_mail "no reset mail sent to unknown email" "$GHOST_EMAIL"
expect_err "verify reset without email" InvalidArgument verify-otp --purpose 1 --code "$RESET_CODE"
expect_err "verify reset with wrong code" PermissionDenied verify-otp --purpose 1 --email "$RESET_EMAIL" --code 000000
expect_ok "verify reset with mailed code" verify-otp --purpose 1 --email "$RESET_EMAIL" --code "$RESET_CODE"
Expand Down
Loading