added discord token check to auth
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
// Package store provides a session store for caching data
|
||||
package store
|
||||
|
||||
import (
|
||||
@@ -22,6 +23,7 @@ type Store struct {
|
||||
sessions sync.Map // key: string, value: *RegistrationSession
|
||||
redirectTracks sync.Map // key: string, value: *RedirectTrack
|
||||
cleanup *time.Ticker
|
||||
tokenchecks sync.Map // key: int, value: *TokenCheck
|
||||
}
|
||||
|
||||
func NewStore() *Store {
|
||||
@@ -42,6 +44,7 @@ func NewStore() *Store {
|
||||
func (s *Store) Delete(id string) {
|
||||
s.sessions.Delete(id)
|
||||
}
|
||||
|
||||
func (s *Store) cleanupExpired() {
|
||||
now := time.Now()
|
||||
|
||||
@@ -62,10 +65,19 @@ func (s *Store) cleanupExpired() {
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
s.tokenchecks.Range(func(key, value any) bool {
|
||||
check := value.(*TokenCheck)
|
||||
if now.After(check.ExpiresAt) {
|
||||
s.tokenchecks.Delete(key)
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
||||
func generateID() string {
|
||||
b := make([]byte, 32)
|
||||
rand.Read(b)
|
||||
_, _ = rand.Read(b)
|
||||
return base64.RawURLEncoding.EncodeToString(b)
|
||||
}
|
||||
|
||||
|
||||
39
internal/store/tokencheck.go
Normal file
39
internal/store/tokencheck.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"git.haelnorr.com/h/oslstats/internal/db"
|
||||
)
|
||||
|
||||
type TokenCheck struct {
|
||||
Token *db.DiscordToken
|
||||
ExpiresAt time.Time
|
||||
}
|
||||
|
||||
func (s *Store) NewTokenCheck(user *db.User, token *db.DiscordToken) error {
|
||||
if user == nil {
|
||||
return errors.New("user cannot be nil")
|
||||
}
|
||||
if token == nil {
|
||||
return errors.New("token cannot be nil")
|
||||
}
|
||||
s.tokenchecks.Store(user.ID, &TokenCheck{
|
||||
Token: token,
|
||||
ExpiresAt: time.Now().Add(5 * time.Minute),
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) CheckToken(user *db.User) *db.DiscordToken {
|
||||
res, ok := s.tokenchecks.Load(user.ID)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
check := (res).(*TokenCheck)
|
||||
check.ExpiresAt = time.Now().Add(5 * time.Minute)
|
||||
s.tokenchecks.Delete(user.ID)
|
||||
s.tokenchecks.Store(user.ID, check)
|
||||
return check.Token
|
||||
}
|
||||
Reference in New Issue
Block a user