-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
232 lines (191 loc) · 5.35 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
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
package hue
import (
"context"
"crypto/tls"
"errors"
"fmt"
"io"
"math/rand"
"net/http"
"net/url"
"os"
"time"
"github.com/pkg/browser"
"golang.org/x/oauth2"
)
const (
AuthURL = "https://api.meethue.com/oauth2/auth"
TokenURL = "https://api.meethue.com/oauth2/token"
ApiURL = "https://api.meethue.com/bridge/"
)
type Authenticator struct {
config *oauth2.Config
token *oauth2.Token
context context.Context
}
func init() {
rand.Seed(time.Now().UnixNano())
}
func NewAuthenticator(redirectURL string) Authenticator {
appID := os.Getenv("HUE_APP_ID")
cfg := &oauth2.Config{
ClientID: os.Getenv("HUE_CLIENT_ID"),
ClientSecret: os.Getenv("HUE_CLIENT_SECRET"),
RedirectURL: redirectURL,
Scopes: []string{},
Endpoint: oauth2.Endpoint{
AuthURL: fmt.Sprintf("%s?appid=%s&deviceid=%s&devicename=browser", AuthURL, appID, appID),
TokenURL: TokenURL,
},
}
tr := &http.Transport{
TLSNextProto: map[string]func(authority string, c *tls.Conn) http.RoundTripper{},
}
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, &http.Client{Transport: tr})
return Authenticator{
config: cfg,
context: ctx,
}
}
func (a *Authenticator) SetAuthInfo(clientID, secretKey string) {
a.config.ClientID = clientID
a.config.ClientSecret = secretKey
}
func (a *Authenticator) AuthURL(state string) string {
return a.config.AuthCodeURL(state)
}
func (a *Authenticator) AuthURLWithOpts(state string, opts ...oauth2.AuthCodeOption) string {
return a.config.AuthCodeURL(state, opts...)
}
func (a *Authenticator) GetToken() *oauth2.Token {
return a.token
}
func (a *Authenticator) Token(state string, r *http.Request) (*oauth2.Token, error) {
values := r.URL.Query()
if e := values.Get("error"); e != "" {
return nil, errors.New("hue: auth failed - " + e)
}
code := values.Get("code")
if code == "" {
return nil, errors.New("hue: didn't get access code")
}
actualState := values.Get("state")
if actualState != state {
return nil, errors.New("hue: redirect state parameter doesn't match")
}
return a.config.Exchange(a.context, code)
}
func (a *Authenticator) Exchange(code string, opts ...oauth2.AuthCodeOption) (*oauth2.Token, error) {
return a.config.Exchange(a.context, code, opts...)
}
func (a *Authenticator) Authenticate() (*Client, error) {
state := fmt.Sprintf("%v", rand.Intn(10000))
authUrl := a.AuthURL(state)
fmt.Printf("Go to %v\n", authUrl)
fmt.Println("Waiting 1 minute for the action")
browser.OpenURL(authUrl)
u, _ := url.Parse(a.config.RedirectURL)
srv := &http.Server{
Addr: fmt.Sprintf(":%s", u.Port()),
}
tokenCh := make(chan *oauth2.Token)
redirectUrl, _ := url.Parse(a.config.RedirectURL)
http.HandleFunc(redirectUrl.Path, func(rw http.ResponseWriter, r *http.Request) {
token, _ := a.Token(state, r)
io.WriteString(rw, `
<html>
<body>
<h1>Login successful!</h1>
<h2>You can close this window.</h2>
</body>
</html>`)
go srv.Close()
// Hue response includes {"token_type": "BearerToken"},
// But we need "Bearer" when you call back.
token.TokenType = "Bearer"
tokenCh <- token
})
go srv.ListenAndServe()
var client *Client
var token *oauth2.Token
select {
case token = <-tokenCh:
a.token = token
client = a.NewClient(token)
return client, nil
case <-time.After(1 * time.Minute):
fmt.Println("could not authenticate on time")
go srv.Close()
return nil, errors.New("could not authenticate on time")
}
}
func (c *Client) Login(username string) error {
c.clientId = username
return nil
}
func (c *Client) CreateRemoteUser() (string, error) {
c.logger.Info("Press the link button on the bridge")
c.logger.Info("Waiting for 30 seconds...")
time.Sleep(30 * time.Second)
c.logger.Info("Enable link button...")
err := c.EnableLinkButton()
if err != nil {
c.logger.Error(err, "could not enable link button")
return "", nil
}
c.logger.Info("Add whitelist identifies...")
username, err := c.AddWhitelistIdentifier()
if err != nil {
c.logger.Error(err, "could not add whitelist identifier")
return "", nil
}
c.clientId = username
return username, nil
}
func (c *Client) EnableLinkButton() error {
var payload = struct {
LinkButton bool `json:"linkedbutton"`
}{LinkButton: true}
req, err := c.newRequest(http.MethodPut, "0/config", payload)
if err != nil {
return err
}
resp, err := c.do(context.Background(), req, nil)
if err != nil {
return err
}
if resp.StatusCode == http.StatusOK {
return nil
}
return errors.New("server returned failed status")
}
func (c *Client) AddWhitelistIdentifier() (string, error) {
var payload = struct {
DeviceType string `json:"devicetype"`
}{DeviceType: os.Getenv("HUE_APP_ID")}
req, err := c.newRequest(http.MethodPost, "", payload)
if err != nil {
return "", err
}
apiResponses := new([]ApiResponse)
resp, err := c.do(context.Background(), req, apiResponses)
if err != nil {
return "", err
}
if resp.StatusCode == http.StatusOK {
if (*apiResponses)[0].Success != nil {
return (*apiResponses)[0].Success["username"].(string), nil
} else {
return "", errors.New((*apiResponses)[0].Error.Description)
}
}
return "", errors.New("server returned failed status")
}
func (a *Authenticator) NewClient(token *oauth2.Token) *Client {
httpClient := a.config.Client(a.context, token)
client, err := newClient(ApiURL, &ClientOptions{HttpClient: httpClient})
if err != nil {
return nil
}
return client
}