-
Notifications
You must be signed in to change notification settings - Fork 9
/
gosmart.go
292 lines (256 loc) · 8.01 KB
/
gosmart.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// This file is part of gosmart, a set of libraries to communicate with
// the Samsumg SmartThings API using Go (golang).
//
// http://github.com/marcopaganini/gosmart
// (C) 2016 by Marco Paganini <[email protected]>
package gosmart
import (
"crypto/rand"
"encoding/json"
"errors"
"fmt"
"golang.org/x/oauth2"
"io/ioutil"
"net/http"
"os/user"
"path/filepath"
"strconv"
)
const (
authDone = "<html><body>Authentication Completed.</body></html>"
authError = "<html><body>AUthentication error. Please see terminal output for details.</body></html>"
// Endpoints URL
endPointsURI = "https://graph.api.smartthings.com/api/smartapps/endpoints"
// URL paths used for Oauth authentication on localhost
callbackPath = "/OAuthCallback"
donePath = "/OauthDone"
rootPath = "/"
// Token save file
defaultTokenFile = ".st_token.json"
// default local HTTP server port
defaultPort = 4567
)
// Auth contains the SmartThings authentication related data.
type Auth struct {
port int
config *oauth2.Config
rchan chan oauthReturn
oauthStateString string
}
// oauthReturn contains the values returned by the OAuth callback handler.
type oauthReturn struct {
token *oauth2.Token
err error
}
// endpoints holds the values returned by the SmartThings endpoints URI.
type endpoints struct {
OauthClient struct {
ClientID string `json:"clientId"`
}
Location struct {
ID string `json:"id"`
Name string `json:"name"`
}
URI string `json:"uri"`
BaseURL string `json:"base_url"`
URL string `json:"url"`
}
// NewOAuthConfig creates a new oauth2.config structure with the
// correct parameters to use smartthings.
func NewOAuthConfig(client, secret string) *oauth2.Config {
return &oauth2.Config{
ClientID: client,
ClientSecret: secret,
Scopes: []string{"app"},
Endpoint: oauth2.Endpoint{
AuthURL: "https://graph.api.smartthings.com/oauth/authorize",
TokenURL: "https://graph.api.smartthings.com/oauth/token",
},
}
}
// NewAuth creates a new Auth struct
func NewAuth(port int, config *oauth2.Config) (*Auth, error) {
rnd, err := randomString(16)
if err != nil {
return nil, err
}
return &Auth{
port: port,
config: config,
rchan: make(chan oauthReturn),
oauthStateString: rnd,
}, nil
}
// FetchOAuthToken sets up the handler and a local HTTP server and fetches an
// Oauth token from the smartthings website.
func (g *Auth) FetchOAuthToken() (*oauth2.Token, error) {
http.HandleFunc(rootPath, g.handleMain)
http.HandleFunc(donePath, g.handleDone)
http.HandleFunc(callbackPath, g.handleOAuthCallback)
go http.ListenAndServe(":"+strconv.Itoa(g.port), nil)
// Block on the return channel (this is set by handleOauthCallback)
ret := <-g.rchan
return ret.token, ret.err
}
// handleMain redirects the user to the main authentication page.
func (g *Auth) handleMain(w http.ResponseWriter, r *http.Request) {
url := g.config.AuthCodeURL(g.oauthStateString)
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}
// handleError shows a page indicating the authentication has failed.
func (g *Auth) handleError(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, authError)
}
// handleDone shows a page indicating the authentication is finished.
func (g *Auth) handleDone(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, authDone)
}
// handleOauthCallback fetches the callback from the OAuth provider and parses
// the URL, extracting the code and then exchanging it for a token.
func (g *Auth) handleOAuthCallback(w http.ResponseWriter, r *http.Request) {
// Make sure we have the same "state" as our request.
state := r.FormValue("state")
if state != g.oauthStateString {
g.rchan <- oauthReturn{
token: nil,
err: fmt.Errorf("invalid oauth state, expected %q, got %q", g.oauthStateString, state),
}
return
}
// Retrieve the code from the URL, and exchange for a token
code := r.FormValue("code")
token, err := g.config.Exchange(oauth2.NoContext, code)
if err != nil {
g.rchan <- oauthReturn{
token: nil,
err: fmt.Errorf("code exchange failed: %q", err),
}
return
}
// Return token.
g.rchan <- oauthReturn{
token: token,
err: nil,
}
// Redirect user to "Authentication done" page
http.Redirect(w, r, donePath, http.StatusTemporaryRedirect)
return
}
// GetEndPointsURI returns the smartthing endpoints URI. The endpoints
// URI is the base for all app requests.
func GetEndPointsURI(client *http.Client) (string, error) {
// Fetch the JSON containing our endpoint URI
resp, err := client.Get(endPointsURI)
if err != nil {
return "", fmt.Errorf("error getting endpoints URI: %q", err)
}
contents, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("error reading endpoints URI data: %q", err)
}
resp.Body.Close()
if string(contents) == "[]" {
return "", fmt.Errorf("endpoint URI returned no content")
}
// Only URI is fetched from JSON string.
var ep []endpoints
err = json.Unmarshal(contents, &ep)
if err != nil {
return "", fmt.Errorf("error decoding JSON: %q", err)
}
return ep[0].URI, nil
}
// LoadToken loads the token from a file on disk. If nil is used for filename
// a default filename user the user's directory is used.
func LoadToken(fname string) (*oauth2.Token, error) {
// Generate token filename
fname, err := makeTokenFile(fname)
if err != nil {
return nil, err
}
// Read & Decode JSON
blob, err := ioutil.ReadFile(fname)
if err != nil {
return nil, err
}
token := &oauth2.Token{}
if err := json.Unmarshal(blob, token); err != nil {
return nil, err
}
return token, nil
}
// SaveToken saves the token to a file on disk. If nil is used for filename
// a default filename user the user's directory is used.
func SaveToken(fname string, token *oauth2.Token) error {
// Generate token filename
fname, err := makeTokenFile(fname)
if err != nil {
return err
}
// Encode & Save
blob, err := json.Marshal(token)
if err != nil {
return err
}
return ioutil.WriteFile(fname, blob, 0600)
}
// randomString generates a random string of bytes of the specified size
// and returns the its hexascii representation.
func randomString(size int) (string, error) {
b := make([]byte, size)
_, err := rand.Read(b)
if err != nil {
return "", err
}
return fmt.Sprintf("%x", b), nil
}
// GetToken returns the token for the ClientID and Secret specified in config.
// The function attempts to load the token from tokenFile first, and failing
// that, starts a full token authentication cycle with SmartThings. If
// tokenFile is blank, the function uses a default name under the current
// user's home directory. The token is saved to local disk before being
// returned to the caller.
//
// This function represents the most common (and possibly convenient) way to
// retrieve a token for a given ClientID and Secret.
func GetToken(tokenFile string, config *oauth2.Config) (*oauth2.Token, error) {
// Attempt to load token from local storage. Fallback to full auth cycle.
token, err := LoadToken(tokenFile)
if err != nil || !token.Valid() {
if config.ClientID == "" || config.ClientSecret == "" {
return nil, errors.New("need ClientID and secret to generate a new token")
}
gst, err := NewAuth(defaultPort, config)
if err != nil {
return nil, err
}
fmt.Printf("Please login by visiting http://localhost:%d\n", defaultPort)
token, err = gst.FetchOAuthToken()
if err != nil {
return nil, err
}
// Once we have the token, save it locally for future use.
err = SaveToken(tokenFile, token)
if err != nil {
return nil, err
}
}
return token, nil
}
// tokenFile generates a filename to store the token.
func makeTokenFile(fname string) (string, error) {
// If filename is an absolute path, return it as is.
// If filename != "", return user_home/filename
// Otherwise, return user_home/defaultTokenFile
if filepath.IsAbs(fname) {
return fname, nil
}
usr, err := user.Current()
if err != nil {
return "", err
}
if fname != "" {
return filepath.Join(usr.HomeDir, fname), nil
}
return filepath.Join(usr.HomeDir, defaultTokenFile), nil
}