-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage_memory.go
More file actions
74 lines (61 loc) · 1.7 KB
/
storage_memory.go
File metadata and controls
74 lines (61 loc) · 1.7 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
65
66
67
68
69
70
71
72
73
74
// Copyright 2017-2026 Allow2 Pty Ltd. All rights reserved.
// Use of this source code is governed by the Allow2 API and SDK Licence.
package allow2service
import (
"sync"
)
// MemoryTokenStorage is an in-memory token storage.
// Suitable for testing and development. Tokens are lost when the process exits.
type MemoryTokenStorage struct {
mu sync.RWMutex
store map[string]*OAuthTokens
}
// NewMemoryTokenStorage creates a new MemoryTokenStorage.
func NewMemoryTokenStorage() *MemoryTokenStorage {
return &MemoryTokenStorage{
store: make(map[string]*OAuthTokens),
}
}
// Store persists tokens for a user.
func (s *MemoryTokenStorage) Store(userID string, tokens *OAuthTokens) error {
s.mu.Lock()
defer s.mu.Unlock()
// Copy to avoid external mutation
copy := &OAuthTokens{
AccessToken: tokens.AccessToken,
RefreshToken: tokens.RefreshToken,
ExpiresAt: tokens.ExpiresAt,
}
s.store[userID] = copy
return nil
}
// Retrieve returns tokens for a user, or nil if none stored.
func (s *MemoryTokenStorage) Retrieve(userID string) (*OAuthTokens, error) {
s.mu.RLock()
defer s.mu.RUnlock()
tokens, ok := s.store[userID]
if !ok {
return nil, nil
}
// Copy to avoid external mutation
copy := &OAuthTokens{
AccessToken: tokens.AccessToken,
RefreshToken: tokens.RefreshToken,
ExpiresAt: tokens.ExpiresAt,
}
return copy, nil
}
// Delete removes tokens for a user.
func (s *MemoryTokenStorage) Delete(userID string) error {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.store, userID)
return nil
}
// Exists checks whether tokens exist for a user.
func (s *MemoryTokenStorage) Exists(userID string) (bool, error) {
s.mu.RLock()
defer s.mu.RUnlock()
_, ok := s.store[userID]
return ok, nil
}