-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth.go
130 lines (106 loc) · 3.39 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
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
package rdapi
import (
"errors"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
func New(rid int, consumerKey string, consumerSecret string, secondSecret string) *RDConfig {
return &RDConfig{
ConsumerKey: consumerKey,
ConsumerSecret: consumerSecret,
SecondSecret: secondSecret,
RestaurantID: rid,
Endpoint: "http://uk.rdbranch.com/OAuth/V10a",
ServiceEndpoint: "http://uk.rdbranch.com/WebServices/Epos/v1",
}
}
func (conf *RDConfig) doOAuth(params []string, sig string, pos int) (*authKeys, error) {
var body []string
body = append(body, params[:pos]...)
body = append(body, "oauth_signature="+url.QueryEscape(sig))
body = append(body, params[pos:]...)
req, err := http.NewRequest("POST", conf.Endpoint, strings.NewReader(strings.Join(body, "&")))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=utf8")
client := &http.Client{
Timeout: time.Duration(3 * time.Second),
}
res, err := client.Do(req)
if err != nil {
return nil, err
}
resBody, _ := ioutil.ReadAll(res.Body)
query, err := url.ParseQuery(string(resBody))
if err != nil {
return nil, err
}
keys := authKeys{}
keys.Parse(query)
if !keys.Valid() {
return nil, errors.New("OAuth Tokens Invalid")
}
return &keys, nil
}
func (conf *RDConfig) doFirstAuth() error {
params := []string{
"oauth_consumer_key=" + url.QueryEscape(conf.ConsumerKey),
"oauth_nonce=" + genNonce(),
"oauth_signature_method=HMAC-SHA1",
"oauth_timestamp=" + strconv.Itoa(int(time.Now().Unix())),
"oauth_version=1.0",
"scope=" + url.QueryEscape("http://app.restaurantdiary.com/WebServices/Epos/v1"),
"second_secret=" + url.QueryEscape(conf.SecondSecret),
}
signature := generateOAuthKey("POST", conf.Endpoint, params, conf.ConsumerSecret, "")
keys, err := conf.doOAuth(params, signature, 3)
if keys != nil {
conf.firstAuth = *keys
}
return err
}
func (conf *RDConfig) doSecondAuth() error {
params := []string{
"oauth_consumer_key=" + url.QueryEscape(conf.ConsumerKey),
"oauth_nonce=" + genNonce(),
"oauth_signature_method=HMAC-SHA1",
"oauth_timestamp=" + strconv.Itoa(int(time.Now().Unix())),
"oauth_token=" + url.QueryEscape(conf.firstAuth.Token),
"oauth_version=1.0",
}
signature := generateOAuthKey("POST", conf.Endpoint, params, conf.ConsumerSecret, conf.firstAuth.Secret)
keys, err := conf.doOAuth(params, signature, 4)
if keys != nil {
conf.secondAuth = *keys
}
return err
}
func (conf *RDConfig) Authenticate() error {
if err := conf.doFirstAuth(); err != nil {
return err
}
return conf.doSecondAuth()
}
func (conf *RDConfig) SetKeys(token string, secret string) {
conf.secondAuth.Token = token
conf.secondAuth.Secret = secret
}
func (conf *RDConfig) RestaurantRequest(method, urlStr string, body io.Reader) (*http.Client, *http.Request, error) {
return conf.NewRequest(method, "/Restaurant/"+strconv.Itoa(conf.RestaurantID)+urlStr, body)
}
func (conf *RDConfig) NewRequest(method, urlStr string, body io.Reader) (*http.Client, *http.Request, error) {
req, err := http.NewRequest(method, conf.ServiceEndpoint+urlStr, nil)
req.Header.Set("Authorization", conf.GetAuthorization(method, urlStr))
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("Accept", "application/json")
client := &http.Client{
Timeout: time.Duration(10 * time.Second),
}
return client, req, err
}