forked from grokify/goauth
/
tokens.go
46 lines (38 loc) · 1.21 KB
/
tokens.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
package tokens
import (
"context"
"encoding/json"
"net/http"
"strings"
"golang.org/x/oauth2"
)
type TokenInfo struct {
ServiceKey string `json:"serviceKey,omitempty"`
ServiceType string `json:"serviceType,omitempty"`
Token *oauth2.Token `json:"token,omitempty"`
}
func FormatKey(key string) string { return strings.TrimSpace(key) }
type TokenSet interface {
GetTokenInfo(key string) (*TokenInfo, error)
GetToken(key string) (*oauth2.Token, error)
SetTokenInfo(key string, tokenInfo *TokenInfo) error
}
func ParseTokenInfo(data []byte) (*TokenInfo, error) {
tok := &TokenInfo{}
return tok, json.Unmarshal(data, tok)
}
func NewClientWithTokenSet(ctx context.Context, conf *oauth2.Config, token *oauth2.Token,
tokenSet TokenSet, tokenKey, serviceKey, serviceType string) (*http.Client, error) {
tokenSource := conf.TokenSource(ctx, token)
if newToken, err := tokenSource.Token(); err != nil {
return nil, err
} else if newToken.AccessToken != token.AccessToken {
if err := tokenSet.SetTokenInfo(tokenKey, &TokenInfo{
ServiceKey: serviceKey,
ServiceType: serviceType,
Token: newToken}); err != nil {
return nil, err
}
}
return oauth2.NewClient(ctx, tokenSource), nil
}