-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.go
87 lines (69 loc) · 2.02 KB
/
auth.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
package helpscout
import (
"net/http"
"time"
"github.com/pkg/errors"
)
const helpscoutAuthEndpoint = "https://api.helpscout.net/v2/oauth2/token"
type authReqData struct {
ClientId string `json:"client_id"`
ClientSecret string `json:"client_secret"`
GrantType string `json:"grant_type"`
}
type auth struct {
httpClient *httpClient
token string
tokenExpireTime time.Time
appId string
appKey string
}
func newAuth(httpClient *httpClient, appId string, appKey string) *auth {
return &auth{
httpClient: httpClient,
appId: appId,
appKey: appKey,
token: "",
tokenExpireTime: time.Time{},
}
}
func (a *auth) getToken(forceUpdate bool) (string, error) {
/* token exists and still valid */
if !forceUpdate && a.token != "" && a.tokenExpireTime.After(time.Now().Add((10 * time.Minute))) {
return a.token, nil
}
reqData := authReqData{
ClientId: a.appId,
ClientSecret: a.appKey,
GrantType: "client_credentials",
}
var responseJson struct {
ExpiresIn int `json:"expires_in"`
Token string `json:"access_token"`
TokenType string `json:"token_type"`
}
repeatCnt := 0
for {
err := a.httpClient.doRequest(helpscoutAuthEndpoint, http.MethodPost, nil, nil, &reqData, &responseJson)
if err == ErrorRateLimit {
time.Sleep(time.Second)
repeatCnt++
if repeatCnt > 10 {
return "", errors.New("Unable to submit auth-token update request (rate-limit)")
}
continue
}
if err == ErrorUnauthorized {
return "", errors.Wrap(err, "Unable to submit auth-token update request (authorization failed)")
}
if err != nil {
return "", errors.Wrap(err, "Unable to submit auth-token update request")
}
break
}
if responseJson.Token == "" || responseJson.ExpiresIn <= 0 {
return "", errors.Errorf("Authorization server returned an invalid data: %+v", responseJson)
}
a.token = responseJson.Token
a.tokenExpireTime = time.Now().Add(time.Second * time.Duration(responseJson.ExpiresIn))
return a.token, nil
}