forked from michimani/gotwi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth1.go
163 lines (139 loc) · 3.54 KB
/
oauth1.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package gotwi
import (
"crypto/hmac"
"crypto/rand"
"crypto/sha1"
"encoding/base64"
"fmt"
"io"
"net/url"
"strings"
"time"
)
const (
OAuthVersion10 = "1.0"
OAuthSignatureMethodHMACSHA1 = "HMAC-SHA1"
)
type Endpoint string
type EndpointInfo struct {
Raw string
Base string
EncodedQueryParameterMap map[string]string
}
type CreateOAthSignatureInput struct {
HTTPMethod string
RawEndpoint string
OAuthConsumerKey string
OAuthToken string
SigningKey string
ParameterMap map[string]string
}
type CreateOAthSignatureOutput struct {
OAuthNonce string
OAuthSignatureMethod string
OAuthTimestamp string
OAuthVersion string
OAuthSignature string
}
func CreateOAuthSignature(in *CreateOAthSignatureInput) (*CreateOAthSignatureOutput, error) {
out := CreateOAthSignatureOutput{
OAuthSignatureMethod: OAuthSignatureMethodHMACSHA1,
OAuthVersion: OAuthVersion10,
}
nonce, err := generateOAthNonce()
if err != nil {
return nil, err
}
out.OAuthNonce = nonce
ts := fmt.Sprintf("%d", time.Now().Unix())
out.OAuthTimestamp = ts
endpointBase := endpointBase(in.RawEndpoint)
parameterString := createParameterString(in.ParameterMap, nonce, ts, in)
sigBase := createSignatureBase(in.HTTPMethod, endpointBase, parameterString)
sig, err := calculateSignature(sigBase, in.SigningKey)
if err != nil {
return nil, err
}
out.OAuthSignature = sig
return &out, nil
}
func generateOAthNonce() (string, error) {
key := make([]byte, 32)
_, err := rand.Read(key)
if err != nil {
return "", err
}
nonce := base64.StdEncoding.EncodeToString(key)
symbols := []string{"+", "/", "="}
for _, s := range symbols {
nonce = strings.Replace(nonce, s, "", -1)
}
return nonce, nil
}
func endpointBase(e string) string {
queryIdx := strings.Index(e, "?")
if queryIdx < 0 {
return e
}
return e[:queryIdx]
}
func (e Endpoint) String() string {
return string(e)
}
func (e Endpoint) Detail() (*EndpointInfo, error) {
d := EndpointInfo{
Raw: e.String(),
EncodedQueryParameterMap: map[string]string{},
}
queryIdx := strings.Index(e.String(), "?")
if queryIdx < 0 {
d.Base = string(e)
return &d, nil
}
d.Base = e.String()[:queryIdx]
queryPart := e.String()[queryIdx+1:]
paramsPairs := strings.Split(queryPart, "&")
for _, pp := range paramsPairs {
keyValue := strings.Split(pp, "=")
var err error
v := ""
if len(keyValue) == 2 {
v, err = url.QueryUnescape(keyValue[1])
if err != nil {
return nil, err
}
}
d.EncodedQueryParameterMap[keyValue[0]] = v
}
return &d, nil
}
func createParameterString(paramsMap map[string]string, nonce, ts string, in *CreateOAthSignatureInput) string {
qv := url.Values{}
for k, v := range paramsMap {
qv.Add(k, v)
}
qv.Add("oauth_consumer_key", in.OAuthConsumerKey)
qv.Add("oauth_nonce", nonce)
qv.Add("oauth_signature_method", OAuthSignatureMethodHMACSHA1)
qv.Add("oauth_timestamp", ts)
qv.Add("oauth_token", in.OAuthToken)
qv.Add("oauth_version", OAuthVersion10)
return qv.Encode()
}
func createSignatureBase(method, endpointBase, parameterString string) string {
return fmt.Sprintf(
"%s&%s&%s",
url.QueryEscape(strings.ToUpper(method)),
url.QueryEscape(endpointBase),
url.QueryEscape(parameterString),
)
}
func calculateSignature(base, key string) (string, error) {
b := []byte(key)
h := hmac.New(sha1.New, b)
_, err := io.WriteString(h, base)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(h.Sum(nil)), nil
}