Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Commit ce19bad

Browse files
committed
Handle captcha
1 parent 531ecc2 commit ce19bad

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

auth/auth.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import (
4848
"math/rand"
4949
"net/http"
5050
"net/url"
51+
"regexp"
5152
"strings"
5253
"time"
5354

@@ -202,6 +203,8 @@ func (c *Config) requestAuth(client *http.Client) (string, cookies, error) {
202203
return res.Request.URL.String(), cookies, err
203204
}
204205

206+
var errorRE = regexp.MustCompile(`<div class="error">(?P<error>.*)</div>`)
207+
205208
// authenticate uses the user and pass to pass the authentication challenge and returns the authorization_code
206209
func (c *Config) authenticate(client *http.Client, cookies cookies, uri, user, pass string) (string, error) {
207210
// Find csrf
@@ -235,7 +238,11 @@ func (c *Config) authenticate(client *http.Client, cookies cookies, uri, user, p
235238

236239
if res.StatusCode != 302 {
237240
body, _ := ioutil.ReadAll(res.Body)
238-
return "", errors.New("status = " + res.Status + ", response = " + string(body))
241+
errs := errorRE.FindStringSubmatch(string(body))
242+
if len(errs) < 2 {
243+
return "", errors.New("status = " + res.Status + ", response = " + string(body))
244+
}
245+
return "", errors.New(errs[1])
239246
}
240247

241248
// Follow redirect to hydra

install.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,21 @@ func askCredentials() (token string, err error) {
112112
}
113113
pass = string(bytePassword)
114114

115-
auth := auth.New()
116-
auth.ClientID = "connector"
117-
auth.Scopes = "iot:devices"
118-
tok, err := auth.Token(user, pass)
115+
authClient := auth.New()
116+
authClient.ClientID = "connector"
117+
authClient.Scopes = "iot:devices"
118+
119+
var tok *auth.Token
120+
// Handle captcha
121+
for {
122+
tok, err = authClient.Token(user, pass)
123+
if err == nil || !strings.HasPrefix(err.Error(), "authenticate: CAPTCHA") {
124+
break
125+
}
126+
fmt.Println("The authentication requested a captcha! We can't let you solve it in a terminal, so please visit https://auth.arduino.cc/login. When you managed to log in from the browser come back here and press [Enter]")
127+
var temp string
128+
fmt.Scanln(&temp)
129+
}
119130
if err != nil {
120131
return "", err
121132
}

0 commit comments

Comments
 (0)