-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
89 lines (74 loc) · 1.84 KB
/
token.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
package ingram
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httputil"
"strconv"
"strings"
"time"
)
type Token struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn string `json:"expires_in"`
ValidUntil time.Time `json:"-"`
}
func (i *Ingram) GetOAuthToken(ctx context.Context, clientID, clientSecret string) (*Token, error) {
data := fmt.Sprintf(`grant_type=client_credentials&client_id=%s&client_secret=%s`, clientID, clientSecret)
version := "30"
if i.isSandbox {
version = "20"
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, fmt.Sprintf("%s/oauth/oauth%s/token", apiEndpoint, version), strings.NewReader(data))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Accept", "*/*")
if i.logger != nil {
b, err := httputil.DumpRequest(req, true)
if err != nil {
return nil, err
}
i.logger.Printf(string(b))
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
if i.logger != nil {
b, err := httputil.DumpResponse(res, true)
if err != nil {
return nil, err
}
i.logger.Printf(string(b))
}
if res.StatusCode != http.StatusOK {
return nil, errors.New("unable to create token")
}
var t Token
err = json.NewDecoder(res.Body).Decode(&t)
if err != nil {
return nil, err
}
return &t, nil
}
func (i *Ingram) checkAndUpdateToken(ctx context.Context) error {
if i.token != nil && time.Now().Before(i.token.ValidUntil) {
return nil
}
token, err := i.GetOAuthToken(ctx, i.clientID, i.clientSecret)
if err != nil {
return err
}
expiresIn, err := strconv.Atoi(token.ExpiresIn)
if err != nil {
return err
}
token.ValidUntil = time.Now().Add(time.Duration(expiresIn-60) * time.Second)
i.token = token
return nil
}