// Copyright (C) 2019 Marius Schellenberger

package db

import "git.giftfish.de/ston1th/jwt/v3"

const blacklistPrefix = "blacklist/"

func (db *DB) Add(sig string, exp int64) error {
	return db.store.Set(blacklistPrefix+sig, exp)
}

func (db *DB) Remove(sig string) error {
	return db.store.Delete(blacklistPrefix + sig)
}

func (db *DB) Check(sig string) (ok bool) {
	if db.store.Get(blacklistPrefix+sig, nil) == nil {
		return true
	}
	return false
}

func (db *DB) Map() (list jwt.BlacklistMap, err error) {
	list = make(jwt.BlacklistMap)
	err = db.store.ForEach(func(k string, v []byte) error {
		if trim, ok := hasTrimPrefix(k, blacklistPrefix); ok {
			var exp int64
			err := db.store.Unmarshal(v, &exp)
			if err != nil {
				return err
			}
			list[trim] = exp
		}
		return nil
	})
	return
}
