docs: record auth hash alignment and validation results

This commit is contained in:
Gabriel20xx committed 2026-10-20 15:26:58 +00:00
1 parent 22887fef03
commit f7819904b9
51 files changed
+1792 -542

No files matched your search

+54
View File
@@ -10,12 +10,20 @@ const (
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()
@@ -64,15 +72,61 @@ func (c *Client) cacheValidation(token string, user *User, valid bool) {
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()
}