Skip to content

Commit

Permalink
update to 0.0.5 (add example for redis blocklist too)
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Nov 2, 2020
1 parent ed7919e commit 1dcb790
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Import as `import "github.com/kataras/jwt"` and use it as `jwt.XXX`.
* [JSON Required Tag](_examples/required/main.go)
* [Custom Validations](_examples/custom-validations/main.go)
* [Advanced: Iris Middleware](https://github.com/kataras/iris/tree/jwt-new-features/middleware/jwt)
* [Advanced: Redis Blocklist](https://github.com/kataras/iris/tree/jwt-new-features/middleware/jwt/blocklist/redis/blocklist.go)
* [References](#references)
* [License](#license)

Expand Down
22 changes: 13 additions & 9 deletions blocklist.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (b *Blocklist) ValidateToken(token []byte, c Claims, err error) error {
return err // respect the previous error.
}

if b.Has(key) {
if has, _ := b.Has(key); has {
return ErrBlocked
}

Expand All @@ -90,47 +90,51 @@ func (b *Blocklist) ValidateToken(token []byte, c Claims, err error) error {
// Next request will be blocked, even if the token was not yet expired.
// This method can be used when the client-side does not clear the token
// on a user logout operation.
func (b *Blocklist) InvalidateToken(token []byte, c Claims) {
func (b *Blocklist) InvalidateToken(token []byte, c Claims) error {
if len(token) == 0 {
return
return ErrMissing
}

key := b.GetKey(token, c)

b.mu.Lock()
b.entries[key] = c.Expiry
b.mu.Unlock()

return nil
}

// Del removes a token based on its "key" from the blocklist.
func (b *Blocklist) Del(key string) {
func (b *Blocklist) Del(key string) error {
b.mu.Lock()
delete(b.entries, key)
b.mu.Unlock()

return nil
}

// Count returns the total amount of blocked tokens.
func (b *Blocklist) Count() int {
func (b *Blocklist) Count() (int64, error) {
b.mu.RLock()
n := len(b.entries)
b.mu.RUnlock()

return n
return int64(n), nil
}

// Has reports whether the given "key" is blocked by the server.
// This method is called before the token verification,
// so even if was expired it is removed from the blocklist.
func (b *Blocklist) Has(key string) bool {
func (b *Blocklist) Has(key string) (bool, error) {
if len(key) == 0 {
return false
return false, ErrMissing
}

b.mu.RLock()
_, ok := b.entries[key]
b.mu.RUnlock()

return ok
return ok, nil
}

// GC iterates over all entries and removes expired tokens.
Expand Down
18 changes: 9 additions & 9 deletions blocklist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ func TestBlocklist(t *testing.T) {
}

b.InvalidateToken(token, sc)
if !b.Has(key) {
if has, _ := b.Has(key); !has {
t.Fatalf("expected token to be in the list")
}

if b.Count() != 1 {
t.Fatalf("expected list to contain a single token entry")
if count, _ := b.Count(); count != 1 {
t.Fatalf("expected list to contain a single token entry but got: %d", count)
}

if err = b.ValidateToken(token, Claims{ID: key}, nil); err != ErrBlocked {
Expand All @@ -35,7 +35,7 @@ func TestBlocklist(t *testing.T) {
t.Fatalf("expected error: ErrExpired as it respects the previous one but got: %v", err)
}

if b.Has(key) {
if has, _ := b.Has(key); has {
t.Fatalf("expected token to be removed as the validate token's error was ErrExpired")
}

Expand All @@ -46,7 +46,7 @@ func TestBlocklist(t *testing.T) {

b.Del(key)

if count := b.Count(); count != 0 {
if count, _ := b.Count(); count != 0 {
t.Fatalf("expected count to be zero but got: %d", count)
}

Expand All @@ -55,17 +55,17 @@ func TestBlocklist(t *testing.T) {
}

b.InvalidateToken([]byte{}, Claims{Expiry: 1})
if got := b.Count(); got != 0 {
if got, _ := b.Count(); got != 0 {
t.Fatalf("expected zero entries as the token was empty but got: %d", got)
}

if b.Has("") {
if has, _ := b.Has(""); has {
t.Fatalf("expected Has to always return false as the given token was empty")
}

// Test GC expired.
b.InvalidateToken([]byte("expired one"), Claims{Expiry: 1})
if got := b.Count(); got != 1 {
if got, _ := b.Count(); got != 1 {
t.Fatalf("expected upsert not append")
}
if removed := b.GC(); removed != 1 {
Expand All @@ -81,7 +81,7 @@ func TestBlocklist(t *testing.T) {
time.Sleep(2 * time.Second)
cancel()

if got := b.Count(); got != 0 {
if got, _ := b.Count(); got != 0 {
t.Fatalf("expected all entries to be removed but: %d", got)
}
}

0 comments on commit 1dcb790

Please sign in to comment.