Skip to content

Commit bf02218

Browse files
committed
[otp] minor fixes
1 parent 68967a7 commit bf02218

File tree

5 files changed

+32
-5
lines changed

5 files changed

+32
-5
lines changed

configs/config.example.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pubsub: # pubsub config
4040
url: memory:// # pubsub url (memory:// or redis://) [PUBSUB__URL]
4141
otp: # otp config
4242
ttl: 300 # otp ttl in seconds [OTP__TTL]
43-
retries: 3 # otp generation retries [OTP__RETRIES]
43+
retries: 3 # otp generation retries (collision handling) [OTP__RETRIES]
4444

4545
## Worker Config ##
4646

internal/sms-gateway/modules/auth/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func New(
6666

6767
// GenerateUserCode generates a unique one-time user authorization code
6868
func (s *Service) GenerateUserCode(ctx context.Context, userID string) (*otp.Code, error) {
69-
code, err := s.otpSvc.Generate(context.Background(), userID)
69+
code, err := s.otpSvc.Generate(ctx, userID)
7070
if err != nil {
7171
return nil, fmt.Errorf("failed to generate code: %w", err)
7272
}

internal/sms-gateway/otp/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import (
55
"time"
66
)
77

8+
// Config configures the OTP service.
89
type Config struct {
9-
TTL time.Duration
10-
Retries int
10+
TTL time.Duration `yaml:"ttl" envconfig:"OTP__TTL"`
11+
Retries int `yaml:"retries" envconfig:"OTP__RETRIES"`
1112
}
1213

1314
func (c Config) Validate() error {

internal/sms-gateway/otp/errors.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package otp
33
import "errors"
44

55
var (
6+
// ErrInvalidConfig indicates that the OTP configuration is invalid.
67
ErrInvalidConfig = errors.New("invalid config")
7-
ErrInitFailed = errors.New("initialization failed")
8+
// ErrInitFailed indicates that the OTP service failed to initialize.
9+
ErrInitFailed = errors.New("initialization failed")
810
)

internal/sms-gateway/otp/service.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ type Service struct {
1818
logger *zap.Logger
1919
}
2020

21+
// NewService returns a new OTP service.
22+
//
23+
// It takes the configuration for the OTP service,
24+
// a storage interface for storing the OTP codes,
25+
// and a logger for logging events.
26+
//
27+
// It returns an error if the configuration is invalid,
28+
// or if either the storage or logger is nil.
2129
func NewService(cfg Config, storage *Storage, logger *zap.Logger) (*Service, error) {
2230
if err := cfg.Validate(); err != nil {
2331
return nil, err
@@ -34,6 +42,14 @@ func NewService(cfg Config, storage *Storage, logger *zap.Logger) (*Service, err
3442
return &Service{cfg: cfg, storage: storage, logger: logger}, nil
3543
}
3644

45+
// Generate generates a new one-time user authorization code.
46+
//
47+
// It takes a context and the ID of the user for whom the code is being generated.
48+
//
49+
// It returns the generated code and an error. If the code can't be generated
50+
// within the configured number of retries, it returns an error.
51+
//
52+
// The generated code is stored with a TTL of the configured duration.
3753
func (s *Service) Generate(ctx context.Context, userID string) (*Code, error) {
3854
var code string
3955
var err error
@@ -65,6 +81,14 @@ func (s *Service) Generate(ctx context.Context, userID string) (*Code, error) {
6581
return &Code{Code: code, ValidUntil: validUntil}, nil
6682
}
6783

84+
// Validate validates a one-time user authorization code.
85+
//
86+
// It takes a context and the one-time code to be validated.
87+
//
88+
// It returns the user ID associated with the code, and an error.
89+
// If the code is invalid, it returns ErrKeyNotFound.
90+
// If there is an error while validating the code, it returns the error.
91+
// If the code is valid, it deletes the code from the storage and returns the user ID.
6892
func (s *Service) Validate(ctx context.Context, code string) (string, error) {
6993
return s.storage.GetAndDelete(ctx, code)
7094
}

0 commit comments

Comments
 (0)