133 lines
3.2 KiB
Go
133 lines
3.2 KiB
Go
package authclient
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
validationCacheTTL = 2 * time.Second
|
|
maxValidationCacheSize = 10000
|
|
)
|
|
|
|
// A session revoked directly in AuthGuard may remain accepted by this client
|
|
// for up to two seconds. Logout through this Client clears the cached result.
|
|
|
|
type validationCacheEntry struct {
|
|
user User
|
|
valid bool
|
|
expires time.Time
|
|
}
|
|
|
|
type profileCacheEntry struct {
|
|
profile PublicUser
|
|
expires time.Time
|
|
}
|
|
|
|
func (c *Client) cachedValidation(token string) (*User, error, bool) {
|
|
key := sha256.Sum256([]byte(token))
|
|
now := time.Now()
|
|
c.validationMu.Lock()
|
|
defer c.validationMu.Unlock()
|
|
entry, ok := c.validationCache[key]
|
|
if !ok {
|
|
return nil, nil, false
|
|
}
|
|
if !now.Before(entry.expires) {
|
|
delete(c.validationCache, key)
|
|
return nil, nil, false
|
|
}
|
|
if !entry.valid {
|
|
return nil, ErrInvalidSession, true
|
|
}
|
|
user := entry.user
|
|
return &user, nil, true
|
|
}
|
|
|
|
func (c *Client) cacheValidation(token string, user *User, valid bool) {
|
|
key := sha256.Sum256([]byte(token))
|
|
now := time.Now()
|
|
c.validationMu.Lock()
|
|
defer c.validationMu.Unlock()
|
|
if c.validationCache == nil {
|
|
c.validationCache = make(map[[32]byte]validationCacheEntry)
|
|
}
|
|
if len(c.validationCache) >= maxValidationCacheSize {
|
|
for existing, entry := range c.validationCache {
|
|
if !now.Before(entry.expires) {
|
|
delete(c.validationCache, existing)
|
|
}
|
|
}
|
|
if len(c.validationCache) >= maxValidationCacheSize {
|
|
for existing := range c.validationCache {
|
|
delete(c.validationCache, existing)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
entry := validationCacheEntry{valid: valid, expires: now.Add(validationCacheTTL)}
|
|
if user != nil {
|
|
entry.user = *user
|
|
}
|
|
c.validationCache[key] = entry
|
|
}
|
|
|
|
func (c *Client) cachedProfile(token string) (*PublicUser, bool) {
|
|
key := sha256.Sum256([]byte(token))
|
|
now := time.Now()
|
|
c.validationMu.Lock()
|
|
defer c.validationMu.Unlock()
|
|
entry, ok := c.profileCache[key]
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
if !now.Before(entry.expires) {
|
|
delete(c.profileCache, key)
|
|
return nil, false
|
|
}
|
|
profile := entry.profile
|
|
return &profile, true
|
|
}
|
|
|
|
func (c *Client) cacheProfile(token string, profile *PublicUser) {
|
|
if profile == nil {
|
|
return
|
|
}
|
|
key := sha256.Sum256([]byte(token))
|
|
now := time.Now()
|
|
c.validationMu.Lock()
|
|
defer c.validationMu.Unlock()
|
|
if c.profileCache == nil {
|
|
c.profileCache = make(map[[32]byte]profileCacheEntry)
|
|
}
|
|
if len(c.profileCache) >= maxValidationCacheSize {
|
|
for existing, entry := range c.profileCache {
|
|
if !now.Before(entry.expires) {
|
|
delete(c.profileCache, existing)
|
|
}
|
|
}
|
|
if len(c.profileCache) >= maxValidationCacheSize {
|
|
for existing := range c.profileCache {
|
|
delete(c.profileCache, existing)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
c.profileCache[key] = profileCacheEntry{profile: *profile, expires: now.Add(validationCacheTTL)}
|
|
}
|
|
|
|
func (c *Client) invalidateValidation(token string) {
|
|
key := sha256.Sum256([]byte(token))
|
|
c.validationMu.Lock()
|
|
delete(c.validationCache, key)
|
|
delete(c.profileCache, key)
|
|
c.validationMu.Unlock()
|
|
}
|
|
|
|
func (c *Client) clearValidationCache() {
|
|
c.validationMu.Lock()
|
|
c.validationCache = make(map[[32]byte]validationCacheEntry)
|
|
c.profileCache = make(map[[32]byte]profileCacheEntry)
|
|
c.validationMu.Unlock()
|
|
}
|