-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.go
115 lines (87 loc) · 2.39 KB
/
store.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package wework
import (
"context"
"fmt"
"sync"
"time"
)
type Store interface {
GetToken(client *Client, ctx context.Context, tokenType TokenType) (string, error)
SetToken(client *Client, ctx context.Context, tokenType TokenType, token string, expiresIn time.Duration) error
}
type TokenInfo struct {
token string
expiresAt time.Time
}
type MemoryStore struct {
tokens sync.Map
}
var _ Store = (*MemoryStore)(nil)
var (
instance Store
once sync.Once
)
func NewMemoryStore() *MemoryStore {
return &MemoryStore{tokens: sync.Map{}}
}
func InitMemoryStore() Store {
once.Do(func() {
instance = NewMemoryStore()
})
return instance
}
func (m *MemoryStore) GetToken(client *Client, ctx context.Context, tokenType TokenType) (string, error) {
key, err := BuildKey(client, tokenType)
if err != nil {
return "", err
}
val, ok := m.tokens.Load(key)
if !ok {
return "", ErrNilStoreToken
}
tokenInfo, ok := val.(*TokenInfo)
if !ok {
return "", fmt.Errorf("invalid token info type")
}
if !tokenInfo.expiresAt.IsZero() && tokenInfo.expiresAt.Before(time.Now()) {
return "", ErrNilStoreToken
}
return tokenInfo.token, nil
}
func (m *MemoryStore) SetToken(client *Client, ctx context.Context, tokenType TokenType, token string, expiresIn time.Duration) error {
key, err := BuildKey(client, tokenType)
if err != nil {
return err
}
m.tokens.Store(key, MakeTokenInfo(token, expiresIn))
return nil
}
func MakeTokenInfo(token string, expiresIn time.Duration) *TokenInfo {
// 如果exipresIn为0,则表示永久有效
var expiresAt time.Time
if expiresIn > 0 {
expiresAt = time.Now().Add(expiresIn)
} else {
expiresAt = time.Time{}
}
return &TokenInfo{token: token, expiresAt: expiresAt}
}
func BuildKey(client *Client, tokenType TokenType) (string, error) {
var id string
switch tokenType {
case ProviderToken.TokenType:
id = client.config.OpenCorp.ProviderCorpID
case SuiteToken.TokenType, SuiteTicket.TokenType:
id = client.config.OpenCorp.SuiteID
case AccessToken.TokenType:
id = client.config.InternalCorp.CorpID
case AuthCorpAccessToken.TokenType, PermanentCode.TokenType:
id = client.config.OpenCorp.SuiteID + ":" + client.config.OpenCorp.AuthCorpID
default:
return "", fmt.Errorf("invalid token type: %s", tokenType)
}
if id == "" {
return "", fmt.Errorf("id is empty for token type: %s", tokenType)
}
return fmt.Sprintf("%s:%s", tokenType, id), nil
}