added errors to blacklist

This commit is contained in:
ston1th 2018-09-22 16:34:10 +02:00
commit 6d28aadaaf
2 changed files with 14 additions and 9 deletions

View file

@ -6,10 +6,10 @@ import "sync"
// Blacklist is the blacklisting storage interface // Blacklist is the blacklisting storage interface
type Blacklist interface { type Blacklist interface {
Add(string, int64) Add(string, int64) error
Remove(string) Remove(string) error
Check(string) bool Check(string) bool
Map() BlacklistMap Map() (BlacklistMap, error)
} }
// BlacklistMap is the blacklist map structure // BlacklistMap is the blacklist map structure
@ -28,17 +28,19 @@ func NewMemBlacklist() *MemBlacklist {
} }
// Add adds a new token signature with expiration time to the blacklist // Add adds a new token signature with expiration time to the blacklist
func (mb *MemBlacklist) Add(sig string, exp int64) { func (mb *MemBlacklist) Add(sig string, exp int64) error {
mb.Lock() mb.Lock()
mb.list[sig] = exp mb.list[sig] = exp
mb.Unlock() mb.Unlock()
return nil
} }
// Remove deletes a token signature from the blacklist // Remove deletes a token signature from the blacklist
func (mb *MemBlacklist) Remove(sig string) { func (mb *MemBlacklist) Remove(sig string) error {
mb.Lock() mb.Lock()
delete(mb.list, sig) delete(mb.list, sig)
mb.Unlock() mb.Unlock()
return nil
} }
// Check returns true if a token signature is blacklisted and false otherwise // Check returns true if a token signature is blacklisted and false otherwise
@ -50,7 +52,7 @@ func (mb *MemBlacklist) Check(sig string) (ok bool) {
} }
// Map returns the blacklist in the form of a iterable map structure for cleanup // Map returns the blacklist in the form of a iterable map structure for cleanup
func (mb *MemBlacklist) Map() (list BlacklistMap) { func (mb *MemBlacklist) Map() (list BlacklistMap, err error) {
list = make(BlacklistMap) list = make(BlacklistMap)
mb.RLock() mb.RLock()
for k, v := range mb.list { for k, v := range mb.list {

9
jwt.go
View file

@ -143,8 +143,7 @@ func (jwt *JWT) Invalidate(t *Token) error {
if !hmac.Equal(jwt.sum(t.Data(), h), t.RawSig()) { if !hmac.Equal(jwt.sum(t.Data(), h), t.RawSig()) {
return ErrInvalid return ErrInvalid
} }
jwt.blacklist.Add(t.Sig(), exp) return jwt.blacklist.Add(t.Sig(), exp)
return nil
} }
// blacklisted checks if a token is blacklisted // blacklisted checks if a token is blacklisted
@ -169,7 +168,11 @@ func (jwt *JWT) clean() {
return return
} }
now := Now() now := Now()
for k, v := range jwt.blacklist.Map() { m, err := jwt.blacklist.Map()
if err != nil {
continue
}
for k, v := range m {
if now > v { if now > v {
jwt.blacklist.Remove(k) jwt.blacklist.Remove(k)
} }