-
Notifications
You must be signed in to change notification settings - Fork 0
/
token_storage.go
41 lines (31 loc) · 1009 Bytes
/
token_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
package n26api
import (
"context"
"errors"
"github.com/nhatthm/n26api/pkg/auth"
)
// ErrTokenKeyEmpty indicates that the key of token is empty and we can not persist that.
var ErrTokenKeyEmpty = errors.New("token key is empty")
var _ auth.TokenStorage = (*InMemoryTokenStorage)(nil)
// InMemoryTokenStorage persists auth.OAuthToken into its memory.
type InMemoryTokenStorage struct {
storage map[string]auth.OAuthToken
}
// Get gets OAuthToken from memory.
func (s *InMemoryTokenStorage) Get(_ context.Context, key string) (auth.OAuthToken, error) {
return s.storage[key], nil
}
// Set sets OAuthToken to memory.
func (s *InMemoryTokenStorage) Set(_ context.Context, key string, token auth.OAuthToken) error {
if key == "" {
return ErrTokenKeyEmpty
}
s.storage[key] = token
return nil
}
// NewInMemoryTokenStorage initiates a new InMemoryTokenStorage.
func NewInMemoryTokenStorage() *InMemoryTokenStorage {
return &InMemoryTokenStorage{
storage: make(map[string]auth.OAuthToken),
}
}