-
Notifications
You must be signed in to change notification settings - Fork 117
/
storage.go
42 lines (32 loc) · 975 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
package authnmock
import (
"context"
"fmt"
"golang.org/x/oauth2"
)
type MockAuthnStorage struct {
// mapping from provider -> user -> token
Tokens map[string]map[string]*oauth2.Token
}
func (m MockAuthnStorage) Store(ctx context.Context, userID, provider string, token *oauth2.Token) error {
if _, ok := m.Tokens[provider]; !ok {
m.Tokens[provider] = make(map[string]*oauth2.Token)
}
m.Tokens[provider][userID] = token
return nil
}
func (m MockAuthnStorage) Read(ctx context.Context, userID, provider string) (*oauth2.Token, error) {
if _, ok := m.Tokens[provider]; !ok {
return nil, fmt.Errorf("token provider '%s' not found for user '%s'", provider, userID)
}
token, ok := m.Tokens[provider][userID]
if !ok {
return nil, fmt.Errorf("token user '%s' not found for provider '%s'", userID, provider)
}
return token, nil
}
func NewMockStorage() *MockAuthnStorage {
return &MockAuthnStorage{
Tokens: map[string]map[string]*oauth2.Token{},
}
}