Skip to content

Commit 2c0ebe5

Browse files
committed
[otp] fix lint issues
1 parent 0446f4d commit 2c0ebe5

File tree

6 files changed

+39
-17
lines changed

6 files changed

+39
-17
lines changed

internal/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ type PubSub struct {
8888
}
8989

9090
type OTP struct {
91-
TTL uint16 `yaml:"ttl" envconfig:"OTP__TTL"`
91+
TTL uint16 `yaml:"ttl" envconfig:"OTP__TTL"`
9292
Retries uint8 `yaml:"retries" envconfig:"OTP__RETRIES"`
9393
}
9494

internal/sms-gateway/otp/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
// Config configures the OTP service.
99
type Config struct {
10-
TTL time.Duration `yaml:"ttl" envconfig:"OTP__TTL"`
10+
TTL time.Duration `yaml:"ttl" envconfig:"OTP__TTL"`
1111
Retries int `yaml:"retries" envconfig:"OTP__RETRIES"`
1212
}
1313

internal/sms-gateway/otp/domain.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package otp
22

33
import "time"
44

5-
// Code is a one-time user authorization code
5+
// Code is a one-time user authorization code.
66
type Code struct {
77
Code string
88
ValidUntil time.Time

internal/sms-gateway/otp/module.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ func Module() fx.Option {
1010
return fx.Module(
1111
"otp",
1212
logger.WithNamedLogger("otp"),
13-
fx.Provide(func(factory cache.Factory) (cache.Cache, error) {
14-
return factory.New("otp")
15-
16-
}, NewStorage, fx.Private),
13+
fx.Provide(
14+
func(factory cache.Factory) (cache.Cache, error) {
15+
return factory.New("otp")
16+
},
17+
NewStorage,
18+
fx.Private,
19+
),
1720
fx.Provide(NewService),
1821
)
1922
}

internal/sms-gateway/otp/service.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"github.com/android-sms-gateway/server/pkg/cache"
10+
"github.com/samber/lo"
1011
"go.uber.org/zap"
1112
)
1213

@@ -51,19 +52,29 @@ func NewService(cfg Config, storage *Storage, logger *zap.Logger) (*Service, err
5152
//
5253
// The generated code is stored with a TTL of the configured duration.
5354
func (s *Service) Generate(ctx context.Context, userID string) (*Code, error) {
55+
const maxValue = 999999
56+
const bytesLength = 3
57+
5458
var code string
5559
var err error
5660

57-
b := make([]byte, 3)
61+
b := make([]byte, bytesLength)
5862
validUntil := time.Now().Add(s.cfg.TTL)
5963

6064
for range s.cfg.Retries {
6165
if _, err = rand.Read(b); err != nil {
6266
s.logger.Warn("failed to read random bytes", zap.Error(err))
6367
continue
6468
}
65-
num := (int(b[0]) << 16) | (int(b[1]) << 8) | int(b[2])
66-
code = fmt.Sprintf("%06d", num%1000000)
69+
70+
num := lo.Reduce(
71+
b,
72+
func(agg uint64, item byte, _ int) uint64 {
73+
return (agg << 8) | uint64(item) //nolint:mnd // bits in byte
74+
},
75+
0,
76+
)
77+
code = fmt.Sprintf("%06d", num%(maxValue+1))
6778

6879
if err = s.storage.SetOrFail(ctx, code, userID, cache.WithValidUntil(validUntil)); err != nil {
6980
s.logger.Warn("failed to store code", zap.Error(err))

internal/sms-gateway/otp/storage.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,40 @@ package otp
22

33
import (
44
"context"
5+
"fmt"
56

67
"github.com/android-sms-gateway/server/pkg/cache"
78
)
89

9-
// Storage wraps the cache interface for string values
10+
// Storage wraps the cache interface for string values.
1011
type Storage struct {
1112
cache cache.Cache
1213
}
1314

14-
// NewStorage creates a new CodesCache with the underlying cache
15+
// NewStorage creates a new CodesCache with the underlying cache.
1516
func NewStorage(c cache.Cache) *Storage {
1617
return &Storage{cache: c}
1718
}
1819

19-
// SetOrFail is like Set, but returns an error if the key already exists
20+
// SetOrFail is like Set, but returns an error if the key already exists.
2021
func (s *Storage) SetOrFail(ctx context.Context, key string, value string, opts ...cache.Option) error {
21-
return s.cache.SetOrFail(ctx, key, []byte(value), opts...)
22+
if err := s.cache.SetOrFail(ctx, key, []byte(value), opts...); err != nil {
23+
return fmt.Errorf("failed to set cache item: %w", err)
24+
}
25+
26+
return nil
2227
}
2328

24-
// GetAndDelete retrieves and deletes a string value
29+
// GetAndDelete retrieves and deletes a string value.
2530
func (s *Storage) GetAndDelete(ctx context.Context, key string) (string, error) {
2631
data, err := s.cache.GetAndDelete(ctx, key)
2732
return string(data), err
2833
}
2934

30-
// Cleanup removes expired items
35+
// Cleanup removes expired items.
3136
func (s *Storage) Cleanup(ctx context.Context) error {
32-
return s.cache.Cleanup(ctx)
37+
if err := s.cache.Cleanup(ctx); err != nil {
38+
return fmt.Errorf("failed to cleanup cache: %w", err)
39+
}
40+
return nil
3341
}

0 commit comments

Comments
 (0)