forked from
evan.jarrett.net/at-container-registry
A container registry that uses the AT Protocol for manifest storage and S3 for blob storage.
1package auth
2
3import (
4 "sync"
5 "time"
6)
7
8// TokenCacheEntry represents a cached access token
9type TokenCacheEntry struct {
10 AccessToken string
11 ExpiresAt time.Time
12}
13
14// TokenCache is a simple in-memory cache for ATProto access tokens
15type TokenCache struct {
16 mu sync.RWMutex
17 tokens map[string]*TokenCacheEntry
18}
19
20var globalTokenCache = &TokenCache{
21 tokens: make(map[string]*TokenCacheEntry),
22}
23
24// GetGlobalTokenCache returns the global token cache instance
25func GetGlobalTokenCache() *TokenCache {
26 return globalTokenCache
27}
28
29// Set stores an access token for a DID
30func (tc *TokenCache) Set(did, accessToken string, ttl time.Duration) {
31 tc.mu.Lock()
32 defer tc.mu.Unlock()
33
34 tc.tokens[did] = &TokenCacheEntry{
35 AccessToken: accessToken,
36 ExpiresAt: time.Now().Add(ttl),
37 }
38}
39
40// Get retrieves an access token for a DID
41func (tc *TokenCache) Get(did string) (string, bool) {
42 tc.mu.RLock()
43 defer tc.mu.RUnlock()
44
45 entry, ok := tc.tokens[did]
46 if !ok {
47 return "", false
48 }
49
50 // Check if expired
51 if time.Now().After(entry.ExpiresAt) {
52 return "", false
53 }
54
55 return entry.AccessToken, true
56}
57
58// Delete removes a cached token
59func (tc *TokenCache) Delete(did string) {
60 tc.mu.Lock()
61 defer tc.mu.Unlock()
62
63 delete(tc.tokens, did)
64}