Something went wrong. Try again.
A container registry that uses the AT Protocol for manifest storage and S3 for blob storage.
Something went wrong. Try again.
1.3 kB · 64 lines
Go
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465package auth
import ( "sync" "time")
// TokenCacheEntry represents a cached access tokentype TokenCacheEntry struct { AccessToken string ExpiresAt time.Time}
// TokenCache is a simple in-memory cache for ATProto access tokenstype TokenCache struct { mu sync.RWMutex tokens map[string]*TokenCacheEntry}
var globalTokenCache = &TokenCache{ tokens: make(map[string]*TokenCacheEntry),}
// GetGlobalTokenCache returns the global token cache instancefunc GetGlobalTokenCache() *TokenCache { return globalTokenCache}
// Set stores an access token for a DIDfunc (tc *TokenCache) Set(did, accessToken string, ttl time.Duration) { tc.mu.Lock() defer tc.mu.Unlock()
tc.tokens[did] = &TokenCacheEntry{ AccessToken: accessToken, ExpiresAt: time.Now().Add(ttl), }}
// Get retrieves an access token for a DIDfunc (tc *TokenCache) Get(did string) (string, bool) { tc.mu.RLock() defer tc.mu.RUnlock()
entry, ok := tc.tokens[did] if !ok { return "", false }
// Check if expired if time.Now().After(entry.ExpiresAt) { return "", false }
return entry.AccessToken, true}
// Delete removes a cached tokenfunc (tc *TokenCache) Delete(did string) { tc.mu.Lock() defer tc.mu.Unlock()
delete(tc.tokens, did)}