forked from belong-inc/go-hubspot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
75 lines (63 loc) · 1.37 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
package hubspot
import (
"fmt"
"net/http"
)
type Authenticator interface {
SetAuthentication(r *http.Request) error
}
type AuthMethod func(c *Client)
func SetOAuth(config *OAuthConfig) AuthMethod {
return func(c *Client) {
c.authenticator = &OAuth{
retriever: &OAuthTokenManager{
oauthPath: fmt.Sprintf("%s/%s", c.baseURL.String(), oauthTokenPath),
HTTPClient: c.HTTPClient,
Config: config,
},
}
}
}
// Deprecated: Use hubspot.SetPrivateAppToken.
func SetAPIKey(key string) AuthMethod {
return func(c *Client) {
c.authenticator = &APIKey{
apikey: key,
}
}
}
func SetPrivateAppToken(token string) AuthMethod {
return func(c *Client) {
c.authenticator = &PrivateAppToken{
accessToken: token,
}
}
}
type OAuth struct {
retriever OAuthTokenRetriever
}
func (o *OAuth) SetAuthentication(r *http.Request) error {
t, err := o.retriever.RetrieveToken()
if err != nil {
return err
}
r.Header.Set("Authorization", "Bearer "+t.AccessToken)
return nil
}
type APIKey struct {
apikey string
}
func (a *APIKey) SetAuthentication(r *http.Request) error {
q := r.URL.Query()
q.Set("hapikey", a.apikey)
r.URL.RawQuery = q.Encode()
return nil
}
type PrivateAppToken struct {
accessToken string
}
func (p *PrivateAppToken) SetAuthentication(r *http.Request) error {
h := r.Header
h.Set("Authorization", "Bearer "+p.accessToken)
return nil
}