@@ -3,9 +3,7 @@ package auth
33import (
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
159157func (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