From bfa9af930e1eee6d9378c7cb1ca8c84a46fca512 Mon Sep 17 00:00:00 2001 From: qowevisa Date: Sat, 3 Aug 2024 08:22:01 +0300 Subject: [PATCH] Add AmIAllowed func to tokens package --- tokens/dispatcher.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/tokens/dispatcher.go b/tokens/dispatcher.go index 911b09a..17459ec 100644 --- a/tokens/dispatcher.go +++ b/tokens/dispatcher.go @@ -67,12 +67,13 @@ var ( ) func GetToken(id uint) (*Token, error) { - toks.Mu.RLock() + toks.Mu.Lock() + defer toks.Mu.Unlock() val, exists := toks.Tokmap[id] - toks.Mu.RUnlock() if !exists { return nil, ERROR_DONT_HAVE_TOKEN } + val.LastActive = time.Now() return val, nil } @@ -138,3 +139,14 @@ func AddToken(id uint) (*Token, error) { toks.Mu.Unlock() return token, nil } + +func AmIAllowed(token string) bool { + toks.Mu.Lock() + defer toks.Mu.Unlock() + val, exists := toks.TokmapRev[token] + if !exists { + return false + } + val.LastActive = time.Now() + return true +}