Add phone registration with SMS verification and regenerate Swagger docs.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
704606b3f5
commit
5e3c46da83
17 files changed
+1008
-20
No files matched your search
@@ -3,23 +3,40 @@ package auth
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"online-back/pkg/sms"
|
||||
"online-back/pkg/utils"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
// ErrInvalidCredentials covers both "no such user" and "wrong password" for
|
||||
// login, and any refresh-token validation failure — callers map it to 401.
|
||||
// Any other error from these methods (token generation failure) maps to 400.
|
||||
var ErrInvalidCredentials = errors.New("invalid credentials")
|
||||
const (
|
||||
otpTTL = 1 * time.Minute
|
||||
maxVerifyTries = 5
|
||||
otpMessageTm = "Onlaýn kabulhana tassyklaýyş belgiňiz: %s"
|
||||
// localOTP is a fixed code for local testing. Switch to generateOTP() in production.
|
||||
localOTP = "123456"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalidCredentials = errors.New("invalid credentials")
|
||||
ErrPasswordMismatch = errors.New("password confirmation does not match")
|
||||
ErrConflict = errors.New("user already exists")
|
||||
ErrInvalidPhone = errors.New("invalid phone number")
|
||||
ErrInvalidCode = errors.New("invalid or expired verification code")
|
||||
ErrInvalidInput = errors.New("required parameter is missing or invalid")
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
repository Repository
|
||||
sms sms.Sender
|
||||
}
|
||||
|
||||
func NewService(repository Repository) *Service {
|
||||
return &Service{repository: repository}
|
||||
func NewService(repository Repository, sender sms.Sender) *Service {
|
||||
return &Service{repository: repository, sms: sender}
|
||||
}
|
||||
|
||||
func (s *Service) Login(ctx context.Context, dto LoginDTO) (map[string]string, error) {
|
||||
@@ -53,3 +70,112 @@ func (s *Service) RefreshToken(ctx context.Context, refreshToken string) (map[st
|
||||
|
||||
return utils.GenerateTokenPair(id)
|
||||
}
|
||||
|
||||
func (s *Service) Register(ctx context.Context, dto RegisterDTO) (RegisterResult, error) {
|
||||
if dto.Password != dto.PasswordConfirm {
|
||||
return RegisterResult{}, ErrPasswordMismatch
|
||||
}
|
||||
|
||||
phone, err := NormalizePhone(dto.Phone)
|
||||
if err != nil {
|
||||
return RegisterResult{}, err
|
||||
}
|
||||
|
||||
username := strings.TrimSpace(dto.Username)
|
||||
name := strings.TrimSpace(dto.Name)
|
||||
if name == "" || username == "" {
|
||||
return RegisterResult{}, ErrInvalidInput
|
||||
}
|
||||
|
||||
exists, err := s.repository.UserExists(ctx, username, phone)
|
||||
if err != nil {
|
||||
return RegisterResult{}, err
|
||||
}
|
||||
if exists {
|
||||
return RegisterResult{}, ErrConflict
|
||||
}
|
||||
|
||||
taken, err := s.repository.UsernameTaken(ctx, username, phone)
|
||||
if err != nil {
|
||||
return RegisterResult{}, err
|
||||
}
|
||||
if taken {
|
||||
return RegisterResult{}, ErrConflict
|
||||
}
|
||||
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(dto.Password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return RegisterResult{}, err
|
||||
}
|
||||
|
||||
code := localOTP
|
||||
// Production: replace the line above with generateOTP().
|
||||
// code, err := generateOTP()
|
||||
// if err != nil {
|
||||
// return RegisterResult{}, err
|
||||
// }
|
||||
|
||||
codeHash, err := bcrypt.GenerateFromPassword([]byte(code), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return RegisterResult{}, err
|
||||
}
|
||||
|
||||
expiresAt := time.Now().UTC().Add(otpTTL)
|
||||
if err := s.repository.UpsertVerification(ctx, Verification{
|
||||
Name: name,
|
||||
Username: username,
|
||||
Phone: phone,
|
||||
Password: string(hashedPassword),
|
||||
CodeHash: string(codeHash),
|
||||
ExpiresAt: expiresAt,
|
||||
}); err != nil {
|
||||
return RegisterResult{}, err
|
||||
}
|
||||
|
||||
if err := s.sms.Send(ctx, phone, fmt.Sprintf(otpMessageTm, code)); err != nil {
|
||||
return RegisterResult{}, err
|
||||
}
|
||||
|
||||
return RegisterResult{
|
||||
Phone: phone,
|
||||
ExpiresIn: int(otpTTL.Seconds()),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) Verify(ctx context.Context, dto VerifyDTO) (map[string]string, error) {
|
||||
phone, err := NormalizePhone(dto.Phone)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
v, err := s.repository.GetVerificationByPhone(ctx, phone)
|
||||
if err != nil {
|
||||
return nil, ErrInvalidCode
|
||||
}
|
||||
|
||||
if v.Attempts >= maxVerifyTries || time.Now().UTC().After(v.ExpiresAt) {
|
||||
return nil, ErrInvalidCode
|
||||
}
|
||||
|
||||
if err := bcrypt.CompareHashAndPassword([]byte(v.CodeHash), []byte(strings.TrimSpace(dto.Code))); err != nil {
|
||||
_ = s.repository.IncrementVerifyAttempts(ctx, v.ID)
|
||||
return nil, ErrInvalidCode
|
||||
}
|
||||
|
||||
userID, err := s.repository.CompleteRegistration(ctx, v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return utils.GenerateUserTokenPair(userID)
|
||||
}
|
||||
|
||||
// Production: uncomment generateOTP and add imports "crypto/rand" and "math/big".
|
||||
// func generateOTP() (string, error) {
|
||||
// max := big.NewInt(1_000_000)
|
||||
// n, err := rand.Int(rand.Reader, max)
|
||||
// if err != nil {
|
||||
// return "", err
|
||||
// }
|
||||
// return fmt.Sprintf("%06d", n.Int64()), nil
|
||||
// }
|
||||
Reference in new issue
Block a user