-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstorage.go
44 lines (35 loc) · 948 Bytes
/
storage.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
package appserver
import (
"context"
"errors"
"sync"
"golang.org/x/oauth2"
)
var ErrCredentialsNotFound = errors.New("credentials for shop not found")
type CredentialStore interface {
Store(ctx context.Context, credentials Credentials) error
Get(ctx context.Context, shopID string) (Credentials, error)
Delete(ctx context.Context, shopID string) error
}
type tokenStore struct {
accessTokens map[string]*oauth2.Token
accessTokensMu sync.RWMutex
}
func newTokenStore() *tokenStore {
return &tokenStore{
accessTokens: make(map[string]*oauth2.Token),
}
}
func (s *tokenStore) Get(shopID string) (*oauth2.Token, bool) {
s.accessTokensMu.RLock()
defer s.accessTokensMu.RUnlock()
if token, ok := s.accessTokens[shopID]; ok {
return token, true
}
return nil, false
}
func (s *tokenStore) Store(shopID string, token *oauth2.Token) {
s.accessTokensMu.Lock()
defer s.accessTokensMu.Unlock()
s.accessTokens[shopID] = token
}