Skip to content

Commit e018832

Browse files
committed
[auth] separate cache from service
1 parent 96cd2b1 commit e018832

File tree

2 files changed

+60
-12
lines changed

2 files changed

+60
-12
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package auth
2+
3+
import (
4+
"crypto/sha256"
5+
"encoding/hex"
6+
"fmt"
7+
"time"
8+
9+
"github.com/android-sms-gateway/server/internal/sms-gateway/models"
10+
"github.com/capcom6/go-helpers/cache"
11+
)
12+
13+
type usersCache struct {
14+
cache *cache.Cache[models.User]
15+
}
16+
17+
func newUsersCache() *usersCache {
18+
return &usersCache{
19+
cache: cache.New[models.User](cache.Config{TTL: 1 * time.Hour}),
20+
}
21+
}
22+
23+
func (c *usersCache) makeKey(username, password string) string {
24+
hash := sha256.Sum256([]byte(username + "\x00" + password))
25+
return hex.EncodeToString(hash[:])
26+
}
27+
28+
func (c *usersCache) Get(username, password string) (models.User, error) {
29+
user, err := c.cache.Get(c.makeKey(username, password))
30+
if err != nil {
31+
return models.User{}, fmt.Errorf("failed to get user from cache: %w", err)
32+
}
33+
34+
return user, nil
35+
}
36+
37+
func (c *usersCache) Set(username, password string, user models.User) error {
38+
if err := c.cache.Set(c.makeKey(username, password), user); err != nil {
39+
return fmt.Errorf("failed to cache user: %w", err)
40+
}
41+
42+
return nil
43+
}
44+
45+
func (c *usersCache) Delete(username, password string) error {
46+
if err := c.cache.Delete(c.makeKey(username, password)); err != nil {
47+
return fmt.Errorf("failed to delete user from cache: %w", err)
48+
}
49+
50+
return nil
51+
}
52+
53+
func (c *usersCache) Cleanup() {
54+
c.cache.Cleanup()
55+
}

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ package auth
33
import (
44
"context"
55
"crypto/rand"
6-
"crypto/sha256"
76
"crypto/subtle"
8-
"encoding/hex"
97
"fmt"
108
"time"
119

@@ -41,7 +39,7 @@ type Service struct {
4139

4240
users *repository
4341
codesCache *cache.Cache[string]
44-
usersCache *cache.Cache[models.User]
42+
usersCache *usersCache
4543

4644
devicesSvc *devices.Service
4745
onlineSvc online.Service
@@ -64,7 +62,7 @@ func New(params Params) *Service {
6462
idgen: idgen,
6563

6664
codesCache: cache.New[string](cache.Config{TTL: codeTTL}),
67-
usersCache: cache.New[models.User](cache.Config{TTL: 1 * time.Hour}),
65+
usersCache: newUsersCache(),
6866
}
6967
}
7068

@@ -157,10 +155,7 @@ func (s *Service) AuthorizeDevice(token string) (models.Device, error) {
157155
}
158156

159157
func (s *Service) AuthorizeUser(username, password string) (*models.User, error) {
160-
hash := sha256.Sum256([]byte(username + "\x00" + password))
161-
cacheKey := hex.EncodeToString(hash[:])
162-
163-
if user, err := s.usersCache.Get(cacheKey); err == nil {
158+
if user, err := s.usersCache.Get(username, password); err == nil {
164159
return &user, nil
165160
}
166161

@@ -173,7 +168,7 @@ func (s *Service) AuthorizeUser(username, password string) (*models.User, error)
173168
return nil, fmt.Errorf("password is incorrect: %w", cmpErr)
174169
}
175170

176-
if setErr := s.usersCache.Set(cacheKey, *user); setErr != nil {
171+
if setErr := s.usersCache.Set(username, password, *user); setErr != nil {
177172
s.logger.Error("failed to cache user", zap.Error(setErr))
178173
}
179174

@@ -215,9 +210,7 @@ func (s *Service) ChangePassword(userID string, currentPassword string, newPassw
215210
}
216211

217212
// Invalidate cache
218-
hash := sha256.Sum256([]byte(userID + currentPassword))
219-
cacheKey := hex.EncodeToString(hash[:])
220-
if delErr := s.usersCache.Delete(cacheKey); delErr != nil {
213+
if delErr := s.usersCache.Delete(userID, currentPassword); delErr != nil {
221214
s.logger.Error("failed to invalidate user cache", zap.Error(delErr))
222215
}
223216

0 commit comments

Comments
 (0)