forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 1
/
oauth.go
102 lines (87 loc) · 2.81 KB
/
oauth.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
package oauth
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"fmt"
"math/rand"
"net/http"
"net/url"
"strconv"
"time"
)
//------------------------------------------------------------------------------
// ClientConfig holds the configuration parameters for an OAuth exchange.
type ClientConfig struct {
ConsumerKey string `json:"consumer_key" yaml:"consumer_key"`
ConsumerSecret string `json:"consumer_secret" yaml:"consumer_secret"`
AccessToken string `json:"access_token" yaml:"access_token"`
AccessTokenSecret string `json:"access_token_secret" yaml:"access_token_secret"`
RequestURL string `json:"request_url" yaml:"request_url"`
Enabled bool `json:"enabled" yaml:"enabled"`
}
// NewClientConfig returns a new ClientConfig with default values.
func NewClientConfig() ClientConfig {
return ClientConfig{
ConsumerKey: "",
ConsumerSecret: "",
AccessToken: "",
AccessTokenSecret: "",
RequestURL: "",
Enabled: false,
}
}
//------------------------------------------------------------------------------
// Sign method to sign an HTTP request for an OAuth exchange.
func (oauth ClientConfig) Sign(req *http.Request) error {
nonceGenerator := rand.New(rand.NewSource(time.Now().UnixNano()))
nonce := strconv.FormatInt(nonceGenerator.Int63(), 10)
ts := fmt.Sprintf("%d", time.Now().Unix())
params := &url.Values{}
params.Add("oauth_consumer_key", oauth.ConsumerKey)
params.Add("oauth_nonce", nonce)
params.Add("oauth_signature_method", "HMAC-SHA1")
params.Add("oauth_timestamp", ts)
params.Add("oauth_token", oauth.AccessToken)
params.Add("oauth_version", "1.0")
sig, err := oauth.getSignature(req, params)
if err != nil {
return err
}
str := fmt.Sprintf(
` oauth_consumer_key="%s", oauth_nonce="%s", oauth_signature="%s",`+
` oauth_signature_method="%s", oauth_timestamp="%s",`+
` oauth_token="%s", oauth_version="%s"`,
url.QueryEscape(oauth.ConsumerKey),
nonce,
url.QueryEscape(sig),
"HMAC-SHA1",
ts,
url.QueryEscape(oauth.AccessToken),
"1.0",
)
req.Header.Add("Authorization", str)
return nil
}
func (oauth ClientConfig) getSignature(
req *http.Request,
params *url.Values,
) (string, error) {
baseSignatureString := req.Method + "&" +
url.QueryEscape(req.URL.String()) + "&" +
url.QueryEscape(params.Encode())
signingKey := url.QueryEscape(oauth.ConsumerSecret) + "&" +
url.QueryEscape(oauth.AccessTokenSecret)
return oauth.computeHMAC(baseSignatureString, signingKey)
}
func (oauth ClientConfig) computeHMAC(
message string,
key string,
) (string, error) {
h := hmac.New(sha1.New, []byte(key))
if _, err := h.Write([]byte(message)); nil != err {
return "", err
}
return base64.StdEncoding.EncodeToString(h.Sum(nil)), nil
}
//------------------------------------------------------------------------------