-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.go
144 lines (119 loc) Β· 3.26 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
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
package makerbot
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
)
func (c *Client) httpGet(endpoint string, qs map[string]string) (map[string]interface{}, error) {
req, err := http.NewRequest("GET", "http://"+c.IP+endpoint, nil)
if err != nil {
return nil, err
}
q := req.URL.Query()
for k, v := range qs {
q.Set(k, v)
}
req.URL.RawQuery = q.Encode()
r, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer r.Body.Close()
var resp map[string]interface{}
err = json.NewDecoder(r.Body).Decode(&resp)
if err != nil {
return nil, err
}
return resp, nil
}
func (c *Client) getLocalAccessToken() (*string, error) {
codeRes, err := c.httpGet("/auth", map[string]string{
"response_type": "code",
"client_id": makerbotClientID,
"client_secret": makerbotClientSecret,
})
if err != nil {
return nil, err
}
var answerRes map[string]interface{}
for {
// Poll until knob is pressed
answerRes, err = c.httpGet("/auth", map[string]string{
"response_type": "answer",
"client_id": makerbotClientID,
"client_secret": makerbotClientSecret,
"answer_code": codeRes["answer_code"].(string),
})
if err != nil {
return nil, err
}
if answerRes["answer"].(string) == "accepted" {
break
}
time.Sleep(2 * time.Second)
}
tokenRes, err := c.httpGet("/auth", map[string]string{
"response_type": "token",
"client_id": makerbotClientID,
"client_secret": makerbotClientSecret,
"context": "jsonrpc",
"auth_code": answerRes["code"].(string),
})
if err != nil {
return nil, err
}
accessToken, ok := tokenRes["access_token"].(string)
if !ok {
return nil, fmt.Errorf("could not authenticate with printer for some reason: %s", err)
}
return &accessToken, nil
}
func (c *Client) getThingiverseAccessToken(token, username string) (*string, error) {
codeRes, err := c.httpGet("/auth", map[string]string{
"response_type": "code",
"client_id": makerbotClientID,
"client_secret": makerbotClientSecret,
"thingiverse_token": token,
"username": username,
})
if err != nil {
return nil, err
}
var answerRes map[string]interface{}
for i := 0; i < 10; i++ {
// Try 10 times for accepted auth
answerRes, err = c.httpGet("/auth", map[string]string{
"response_type": "answer",
"client_id": makerbotClientID,
"client_secret": makerbotClientSecret,
"answer_code": codeRes["answer_code"].(string),
})
if err != nil {
return nil, err
}
if answerRes["answer"].(string) == "accepted" {
break
}
time.Sleep(2 * time.Second)
}
if answerRes["answer"].(string) != "accepted" {
return nil, errors.New("could not authenticate with printer after 10 tries, please check that your Thingiverse account is authorized to access it")
}
tokenRes, err := c.httpGet("/auth", map[string]string{
"response_type": "token",
"client_id": makerbotClientID,
"client_secret": makerbotClientSecret,
"context": "jsonrpc",
"auth_code": answerRes["code"].(string),
})
if err != nil {
return nil, err
}
accessToken, ok := tokenRes["access_token"].(string)
if !ok {
return nil, errors.New("could not authenticate with printer, please check that your Thingiverse account is authorized to access it")
}
return &accessToken, nil
}