-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcache.go
More file actions
64 lines (51 loc) · 1.38 KB
/
cache.go
File metadata and controls
64 lines (51 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package oauth
import (
"sync"
"time"
"github.com/tuannvm/oauth-mcp-proxy/provider"
)
// Re-export User from provider for backwards compatibility
type User = provider.User
// TokenCache stores validated tokens to avoid re-validation
type TokenCache struct {
mu sync.RWMutex
cache map[string]*CachedToken
}
// CachedToken represents a cached token validation result
type CachedToken struct {
User *User
ExpiresAt time.Time
}
// getCachedToken retrieves a cached token validation result
func (tc *TokenCache) getCachedToken(tokenHash string) (*CachedToken, bool) {
tc.mu.RLock()
cached, exists := tc.cache[tokenHash]
if !exists {
tc.mu.RUnlock()
return nil, false
}
if time.Now().After(cached.ExpiresAt) {
tc.mu.RUnlock()
go tc.deleteExpiredToken(tokenHash)
return nil, false
}
tc.mu.RUnlock()
return cached, true
}
// deleteExpiredToken safely deletes an expired token from the cache
func (tc *TokenCache) deleteExpiredToken(tokenHash string) {
tc.mu.Lock()
defer tc.mu.Unlock()
if cached, exists := tc.cache[tokenHash]; exists && time.Now().After(cached.ExpiresAt) {
delete(tc.cache, tokenHash)
}
}
// setCachedToken stores a token validation result
func (tc *TokenCache) setCachedToken(tokenHash string, user *User, expiresAt time.Time) {
tc.mu.Lock()
defer tc.mu.Unlock()
tc.cache[tokenHash] = &CachedToken{
User: user,
ExpiresAt: expiresAt,
}
}