-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth.go
61 lines (51 loc) · 1.56 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
package cloudsight
import (
"crypto/hmac"
"crypto/rand"
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"fmt"
urlPkg "net/url"
"strings"
"time"
)
const (
oauthSignatureMethod = "HMAC-SHA1"
oauthVersion = "1.0"
)
func oauthSign(method, url, key, secret string, params Params) (string, error) {
if params == nil {
params = Params{}
}
// Get random nonce
nonceBuf := make([]byte, 20)
if _, err := rand.Read(nonceBuf); err != nil {
return "", err
}
hash := sha256.Sum256(nonceBuf)
params["oauth_nonce"] = hex.EncodeToString(hash[:])
params["oauth_consumer_key"] = key
params["oauth_signature_method"] = oauthSignatureMethod
params["oauth_timestamp"] = fmt.Sprint(time.Now().Unix())
params["oauth_version"] = oauthVersion
baseString := strings.Join([]string{
strings.ToUpper(method),
urlPkg.QueryEscape(url),
urlPkg.QueryEscape(params.values().Encode()),
}, "&")
secret = fmt.Sprintf("%s&", urlPkg.QueryEscape(secret))
h := hmac.New(sha1.New, []byte(secret))
h.Write([]byte(baseString))
signature := h.Sum(nil)
headerValues := []string{
fmt.Sprintf("oauth_consumer_key=\"%s\"", params["oauth_consumer_key"]),
fmt.Sprintf("oauth_nonce=\"%s\"", params["oauth_nonce"]),
fmt.Sprintf("oauth_signature=\"%s\"", base64.StdEncoding.EncodeToString(signature)),
fmt.Sprintf("oauth_signature_method=\"%s\"", oauthSignatureMethod),
fmt.Sprintf("oauth_timestamp=\"%s\"", params["oauth_timestamp"]),
fmt.Sprintf("oauth_version=\"%s\"", oauthVersion),
}
return fmt.Sprintf("OAuth %s", strings.Join(headerValues, ", ")), nil
}