fin-check-api/tokens/dispatcher.go

166 lines
3.0 KiB
Go
Raw Permalink Normal View History

2024-08-01 23:03:01 +02:00
package tokens
import (
"crypto/rand"
"encoding/base64"
"errors"
"log"
2024-08-02 22:36:31 +02:00
"strings"
2024-08-01 23:03:01 +02:00
"sync"
"time"
)
type Token struct {
Id uint
2024-08-01 23:03:01 +02:00
Val string
LastActive time.Time
}
var (
ActiveDur = time.Duration(time.Hour)
)
func (t Token) IsExpired() bool {
return time.Now().Sub(t.LastActive) >= ActiveDur
}
type TokensMapMu struct {
Initialized bool
2024-08-02 22:36:31 +02:00
Tokmap map[uint]*Token
TokmapRev map[string]*Token
2024-08-01 23:03:01 +02:00
Mu sync.RWMutex
}
var toks TokensMapMu
// NOTE: should be launch with a goroutine
// NOTE: it cannot die
func StartTokens() {
if toks.Initialized {
return
}
2024-08-02 22:36:31 +02:00
toks.Tokmap = make(map[uint]*Token)
toks.TokmapRev = make(map[string]*Token)
2024-08-01 23:03:01 +02:00
toks.Initialized = true
for {
//
toks.Mu.Lock()
for id, token := range toks.Tokmap {
2024-08-02 22:36:31 +02:00
if token == nil {
log.Printf("DAFUQ: 001\n")
delete(toks.Tokmap, id)
2024-08-14 14:05:42 +02:00
continue
2024-08-02 22:36:31 +02:00
}
2024-08-01 23:03:01 +02:00
if token.IsExpired() {
2024-08-02 22:36:31 +02:00
val := token.Val
2024-08-01 23:03:01 +02:00
delete(toks.Tokmap, id)
2024-08-02 22:36:31 +02:00
delete(toks.TokmapRev, val)
2024-08-01 23:03:01 +02:00
}
}
toks.Mu.Unlock()
//
time.Sleep(time.Minute)
}
}
var (
ERROR_DONT_HAVE_TOKEN = errors.New("Don't have token for this user")
ERROR_ALREADY_HAVE_TOKEN = errors.New("Already have token")
)
2024-08-02 22:36:31 +02:00
func GetToken(id uint) (*Token, error) {
2024-08-03 07:22:01 +02:00
toks.Mu.Lock()
defer toks.Mu.Unlock()
2024-08-01 23:03:01 +02:00
val, exists := toks.Tokmap[id]
if !exists {
2024-08-02 22:36:31 +02:00
return nil, ERROR_DONT_HAVE_TOKEN
2024-08-01 23:03:01 +02:00
}
2024-08-03 07:22:01 +02:00
val.LastActive = time.Now()
2024-08-01 23:03:01 +02:00
return val, nil
}
func GetID(token string) (uint, error) {
toks.Mu.RLock()
val, exists := toks.TokmapRev[token]
toks.Mu.RUnlock()
if !exists {
return 0, ERROR_DONT_HAVE_TOKEN
}
return val.Id, nil
}
2024-08-01 23:03:01 +02:00
func haveToken(id uint) bool {
toks.Mu.RLock()
_, exists := toks.Tokmap[id]
toks.Mu.RUnlock()
return exists
}
func UpdateLastActive(id uint) error {
if !haveToken(id) {
return ERROR_DONT_HAVE_TOKEN
}
toks.Mu.Lock()
val := toks.Tokmap[id]
val.LastActive = time.Now()
toks.Tokmap[id] = val
toks.Mu.Unlock()
return nil
}
func haveTokenVal(val string) bool {
toks.Mu.RLock()
_, exists := toks.TokmapRev[val]
toks.Mu.RUnlock()
return exists
}
func generateRandomString(length int) string {
bytes := make([]byte, length)
if _, err := rand.Read(bytes); err != nil {
log.Printf("generateRandomString: %v", err)
}
return base64.URLEncoding.EncodeToString(bytes)
}
func generateTokenVal() string {
for {
tok := generateRandomString(32)
2024-08-02 22:36:31 +02:00
trimedToken := strings.Trim(tok, "=")
if !haveTokenVal(trimedToken) {
return trimedToken
2024-08-01 23:03:01 +02:00
}
}
}
2024-08-02 22:36:31 +02:00
func AddToken(id uint) (*Token, error) {
2024-08-01 23:03:01 +02:00
toks.Mu.RLock()
_, exists := toks.Tokmap[id]
toks.Mu.RUnlock()
if exists {
// return nil, ERROR_ALREADY_HAVE_TOKEN
2024-08-01 23:03:01 +02:00
}
2024-08-02 22:36:31 +02:00
val := generateTokenVal()
token := &Token{
Id: id,
2024-08-02 22:36:31 +02:00
Val: val,
2024-08-01 23:03:01 +02:00
LastActive: time.Now(),
2024-08-02 22:36:31 +02:00
}
toks.Mu.Lock()
toks.Tokmap[id] = token
toks.TokmapRev[val] = token
toks.Mu.Unlock()
return token, nil
2024-08-01 23:03:01 +02:00
}
2024-08-03 07:22:01 +02:00
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
}