-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstores_mem.go
74 lines (64 loc) · 1.84 KB
/
stores_mem.go
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
package ohauth
import (
"fmt"
"sync"
)
// TestingStore is a Store implementation that may be used for testing and
// experimenting with OhAuth. It is a simple memory-based store.
type TestingStore struct {
*sync.Mutex
authz map[string]*Authorization
clients map[string]*Client
tokens map[string]*TokenClaims
blacklist map[string]bool
}
// NewTestingStore creates an instace of a TestingStore
func NewTestingStore() (*TestingStore, error) {
return &TestingStore{
&sync.Mutex{},
make(map[string]*Authorization, 0),
make(map[string]*Client, 0),
make(map[string]*TokenClaims, 0),
make(map[string]bool, 0),
}, nil
}
// CreateClient stores a client
func (s *TestingStore) CreateClient(c *Client) error {
s.Lock()
defer s.Unlock()
s.clients[c.ID] = c
return nil
}
// FetchClient retrieves a client by its id
func (s *TestingStore) FetchClient(cid string) (*Client, error) {
return s.clients[cid], nil
}
// DeleteClient deletes a client by its id
func (s *TestingStore) DeleteClient(cid string) error {
s.Lock()
defer s.Unlock()
delete(s.clients, cid)
return nil
}
// BlacklistToken invalidate codes and tokens using a token ID
func (s *TestingStore) BlacklistToken(id string) error {
s.Lock()
defer s.Unlock()
s.blacklist[id] = true
return nil
}
// TokenBlacklisted is used to check if a code or token is invalidated
func (s *TestingStore) TokenBlacklisted(id string) (bool, error) {
return s.blacklist[id], nil
}
// StoreAuthorization records a resource owner's authorisation of a client
func (s *TestingStore) StoreAuthorization(a *Authorization) error {
s.Lock()
defer s.Unlock()
s.authz[fmt.Sprintf("%s:%s", a.CID, a.UID)] = a
return nil
}
// FetchAuthorization retrieves an Authorization record
func (s *TestingStore) FetchAuthorization(cid, uid string) (*Authorization, error) {
return s.authz[fmt.Sprintf("%s:%s", cid, uid)], nil
}