Skip to content

Commit c1127d7

Browse files
committed
refactor: improved TokenValidator interface and implementations
1 parent 46ab7f5 commit c1127d7

File tree

5 files changed

+85
-109
lines changed

5 files changed

+85
-109
lines changed

cache/logger.go

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,42 +39,34 @@ func (l *Logger) SetTokenToCacheFailed(err error) {
3939
)
4040
}
4141

42-
// GetTokenFromCache logs the get token from cache event
43-
func (l *Logger) GetTokenFromCache(token gojwttoken.Token, id int64) {
42+
// RevokeTokenFromCache logs the revoke token from cache event
43+
func (l *Logger) RevokeTokenFromCache(token gojwttoken.Token, id int64) {
4444
l.logger.Debug(
45-
"get token from cache",
45+
"revoke token from cache",
4646
fmt.Sprintf("token: %s, id: %d", token, id),
4747
)
4848
}
4949

50-
// GetTokenFromCacheFailed logs the get token from cache failed event
51-
func (l *Logger) GetTokenFromCacheFailed(err error) {
50+
// RevokeTokenFromCacheFailed logs the revoke token from cache failed event
51+
func (l *Logger) RevokeTokenFromCacheFailed(err error) {
5252
l.logger.Error(
53-
"get token from cache failed",
53+
"revoke token from cache failed",
5454
err,
5555
)
5656
}
5757

58-
// HasTokenInCacheFailed logs the has token in cache failed event
59-
func (l *Logger) HasTokenInCacheFailed(err error) {
60-
l.logger.Error(
61-
"has token in cache failed",
62-
err,
63-
)
64-
}
65-
66-
// DeleteTokenFromCache logs the delete token from cache event
67-
func (l *Logger) DeleteTokenFromCache(token gojwttoken.Token, id int64) {
58+
// GetTokenFromCache logs the get token from cache event
59+
func (l *Logger) GetTokenFromCache(token gojwttoken.Token, id int64) {
6860
l.logger.Debug(
69-
"delete token from cache",
61+
"get token from cache",
7062
fmt.Sprintf("token: %s, id: %d", token, id),
7163
)
7264
}
7365

74-
// DeleteTokenFromCacheFailed logs the delete token from cache failed event
75-
func (l *Logger) DeleteTokenFromCacheFailed(err error) {
66+
// GetTokenFromCacheFailed logs the get token from cache failed event
67+
func (l *Logger) GetTokenFromCacheFailed(err error) {
7668
l.logger.Error(
77-
"delete token from cache failed",
69+
"get token from cache failed",
7870
err,
7971
)
8072
}

cache/redis/validator.go

Lines changed: 41 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
gojwttoken "github.com/ralvarezdev/go-jwt/token"
99
gostringsadd "github.com/ralvarezdev/go-strings/add"
1010
gostringsseparator "github.com/ralvarezdev/go-strings/separator"
11+
"strconv"
1112
"time"
1213
)
1314

@@ -54,24 +55,17 @@ func (d *TokenValidatorService) GetKey(
5455
), nil
5556
}
5657

57-
// Set sets the token with the value and period
58-
func (d *TokenValidatorService) Set(
59-
token gojwttoken.Token,
60-
id string,
61-
value interface{},
58+
// setWithFormattedKey sets the token with the value and expiration
59+
func (d *TokenValidatorService) setWithFormattedKey(
60+
key string,
61+
isValid bool,
6262
expiresAt time.Time,
6363
) error {
64-
// Get the key
65-
key, err := d.GetKey(token, id)
66-
if err != nil {
67-
return err
68-
}
69-
7064
// Set the initial value
71-
if err = d.redisClient.Set(
65+
if err := d.redisClient.Set(
7266
context.Background(),
7367
key,
74-
value,
68+
isValid,
7569
0,
7670
).Err(); err != nil {
7771
// Log the error
@@ -82,7 +76,7 @@ func (d *TokenValidatorService) Set(
8276
}
8377

8478
// Set expiration time for the key as a UNIX timestamp
85-
err = d.redisClient.ExpireAt(context.Background(), key, expiresAt).Err()
79+
err := d.redisClient.ExpireAt(context.Background(), key, expiresAt).Err()
8680
if err != nil {
8781
// Log the error
8882
if d.logger != nil {
@@ -92,76 +86,71 @@ func (d *TokenValidatorService) Set(
9286
return err
9387
}
9488

95-
// Has checks if the token is valid
96-
func (d *TokenValidatorService) Has(
89+
// Set sets the token with the value and expiration
90+
func (d *TokenValidatorService) Set(
9791
token gojwttoken.Token,
9892
id string,
99-
) (bool, error) {
93+
isValid bool,
94+
expiresAt time.Time,
95+
) error {
10096
// Get the key
10197
key, err := d.GetKey(token, id)
10298
if err != nil {
103-
return false, err
99+
return err
104100
}
105101

106-
// Check the JWT Identifier
107-
_, err = d.redisClient.Get(context.Background(), key).Result()
108-
if err != nil {
109-
// Log the error
110-
if d.logger != nil {
111-
d.logger.HasTokenInCacheFailed(err)
112-
}
113-
return false, err
114-
}
115-
return true, nil
102+
return d.setWithFormattedKey(key, isValid, expiresAt)
116103
}
117104

118-
// Get gets the token
119-
func (d *TokenValidatorService) Get(
105+
// Revoke revokes the token
106+
func (d *TokenValidatorService) Revoke(
120107
token gojwttoken.Token,
121108
id string,
122-
) (interface{}, error) {
109+
) error {
123110
// Get the key
124111
key, err := d.GetKey(token, id)
125112
if err != nil {
126-
return nil, err
113+
return err
127114
}
128115

129-
// Get the value
130-
value, err := d.redisClient.Get(
131-
context.Background(),
132-
key,
133-
).Result()
116+
// Get the current TTL of the key
117+
ttl, err := d.redisClient.TTL(context.Background(), key).Result()
134118
if err != nil {
135-
// Log the error
136-
if d.logger != nil {
137-
d.logger.GetTokenFromCacheFailed(err)
138-
}
139-
return nil, err
119+
return err
140120
}
141-
return value, err
121+
122+
// Update the value maintaining the TTL
123+
return d.setWithFormattedKey(key, false, time.Now().Add(ttl))
142124
}
143125

144-
// Delete deletes the token
145-
func (d *TokenValidatorService) Delete(
126+
// IsValid checks if the token is valid
127+
func (d *TokenValidatorService) IsValid(
146128
token gojwttoken.Token,
147129
id string,
148-
) error {
130+
) (bool, error) {
149131
// Get the key
150132
key, err := d.GetKey(token, id)
151133
if err != nil {
152-
return err
134+
return false, err
153135
}
154136

155-
// Delete the key
156-
err = d.redisClient.Del(
137+
// Get the value
138+
isValid, err := d.redisClient.Get(
157139
context.Background(),
158140
key,
159-
).Err()
141+
).Result()
160142
if err != nil {
161143
// Log the error
162144
if d.logger != nil {
163-
d.logger.DeleteTokenFromCacheFailed(err)
145+
d.logger.GetTokenFromCacheFailed(err)
164146
}
147+
return false, err
165148
}
166-
return err
149+
150+
// Parse the value
151+
parsedIsValue, err := strconv.ParseBool(isValid)
152+
if err != nil {
153+
return false, err
154+
}
155+
return parsedIsValue, nil
167156
}

cache/validator.go

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cache
22

33
import (
4+
gocache "github.com/ralvarezdev/go-cache"
45
gocachetimed "github.com/ralvarezdev/go-cache/timed"
56
gojwttoken "github.com/ralvarezdev/go-jwt/token"
67
gostringsadd "github.com/ralvarezdev/go-strings/add"
@@ -14,25 +15,24 @@ type (
1415
Set(
1516
token gojwttoken.Token,
1617
id string,
17-
value interface{},
18+
isValid bool,
1819
expiresAt time.Time,
1920
) error
20-
Has(token gojwttoken.Token, id string) (bool, error)
21-
Get(token gojwttoken.Token, id string) (interface{}, bool)
22-
Delete(token gojwttoken.Token, id string) error
21+
Revoke(token gojwttoken.Token, id string) error
22+
IsValid(token gojwttoken.Token, id string) (bool, error)
2323
}
2424

2525
// TokenValidatorService struct
2626
TokenValidatorService struct {
2727
logger *Logger
28-
gocachetimed.Cache
28+
cache gocachetimed.Cache
2929
}
3030
)
3131

3232
// NewTokenValidatorService creates a new token validator service
3333
func NewTokenValidatorService(logger *Logger) *TokenValidatorService {
3434
return &TokenValidatorService{
35-
Cache: gocachetimed.Cache{},
35+
cache: gocachetimed.Cache{},
3636
logger: logger,
3737
}
3838
}
@@ -55,7 +55,7 @@ func (t *TokenValidatorService) GetKey(
5555
func (t *TokenValidatorService) Set(
5656
token gojwttoken.Token,
5757
id string,
58-
value interface{},
58+
isValid bool,
5959
expiresAt time.Time,
6060
) error {
6161
// Get the key
@@ -65,7 +65,7 @@ func (t *TokenValidatorService) Set(
6565
}
6666

6767
// Set the token in the cache
68-
err = t.Cache.Set(key, gocachetimed.NewItem(value, expiresAt))
68+
err = t.cache.Set(key, gocachetimed.NewItem(isValid, expiresAt))
6969
if err != nil {
7070
// Log the error
7171
if t.logger != nil {
@@ -75,48 +75,43 @@ func (t *TokenValidatorService) Set(
7575
return err
7676
}
7777

78-
// Has checks if a token exists in the cache
79-
func (t *TokenValidatorService) Has(
78+
// Revoke revokes a token in the cache
79+
func (t *TokenValidatorService) Revoke(
8080
token gojwttoken.Token,
8181
id string,
82-
) (bool, error) {
82+
) error {
8383
// Get the key
8484
key, err := t.GetKey(token, id)
8585
if err != nil {
86-
return false, err
86+
return err
8787
}
8888

89-
// Check if the token exists in the cache
90-
return t.Cache.Has(key), nil
91-
}
92-
93-
// Get gets a token from the cache
94-
func (t *TokenValidatorService) Get(
95-
token gojwttoken.Token,
96-
id string,
97-
) (interface{}, bool) {
98-
// Get the key
99-
key, err := t.GetKey(token, id)
89+
// Revoke the token in the cache
90+
err = t.cache.UpdateValue(key, false)
10091
if err != nil {
101-
return nil, false
92+
// Log the error
93+
if t.logger != nil {
94+
t.logger.RevokeTokenFromCacheFailed(err)
95+
}
10296
}
103-
104-
// Get the token from the cache
105-
return t.Cache.Get(key)
97+
return err
10698
}
10799

108-
// Delete deletes a token from the cache
109-
func (t *TokenValidatorService) Delete(
100+
// IsValid checks if a token is valid in the cache
101+
func (t *TokenValidatorService) IsValid(
110102
token gojwttoken.Token,
111103
id string,
112-
) error {
104+
) (bool, error) {
113105
// Get the key
114106
key, err := t.GetKey(token, id)
115107
if err != nil {
116-
return err
108+
return false, err
117109
}
118110

119-
// Delete the token from the cache
120-
t.Cache.Delete(key)
121-
return nil
111+
// Get the token from the cache
112+
isValid, found := t.cache.Get(key)
113+
if !found {
114+
return false, gocache.ErrItemNotFound
115+
}
116+
return isValid.(bool), nil
122117
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/gin-gonic/gin v1.10.0
77
github.com/go-redis/redis/v8 v8.11.5
88
github.com/golang-jwt/jwt/v5 v5.2.1
9-
github.com/ralvarezdev/go-cache v0.1.0
9+
github.com/ralvarezdev/go-cache v0.1.1
1010
github.com/ralvarezdev/go-databases v0.4.13
1111
github.com/ralvarezdev/go-flags v0.3.1
1212
github.com/ralvarezdev/go-logger v0.4.5

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6
6363
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
6464
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6565
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
66-
github.com/ralvarezdev/go-cache v0.1.0 h1:yZeWQ8BKsHIU0tkhgOrnaIa+Ayt63cLn0l7ha3uBPZs=
67-
github.com/ralvarezdev/go-cache v0.1.0/go.mod h1:uxdoDOgKOj0tQReWRA7rc1RtiFPhg8tfzVK6nJJ7QKo=
66+
github.com/ralvarezdev/go-cache v0.1.1 h1:Gd+F6ghWEDNUG/EYtVOiYPnw6rV3aXenpnkEXBvZTUA=
67+
github.com/ralvarezdev/go-cache v0.1.1/go.mod h1:uxdoDOgKOj0tQReWRA7rc1RtiFPhg8tfzVK6nJJ7QKo=
6868
github.com/ralvarezdev/go-databases v0.4.13 h1:thB7s0XwOTfAumUFDbelABH77NesyMOJI1VX3If1dw4=
6969
github.com/ralvarezdev/go-databases v0.4.13/go.mod h1:JOUqccAyW1lAriqYEkUmvAtMeraAsAeT71gw0xj0vhc=
7070
github.com/ralvarezdev/go-flags v0.3.1 h1:YxGYUonmr0dwJL+9oDY/4GqCl1CYwYDJExpGXXMc9rU=

0 commit comments

Comments
 (0)