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

Commit 54ab2d1

Browse files
authored
Merge pull request #18 from bcmi-labs/fix_login
Fix login
2 parents 8955c95 + ce19bad commit 54ab2d1

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

auth/auth.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* invalidate any other reasons why the executable file might be covered by
2525
* the GNU General Public License.
2626
*
27-
* Copyright 2017 BCMI LABS SA (http://www.arduino.cc/)
27+
* Copyright 2017 ARDUINO AG (http://www.arduino.cc/)
2828
*/
2929

3030
// Package auth uses the `oauth2 authorization_code` flow to authenticate with Arduino
@@ -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
@@ -215,6 +218,7 @@ func (c *Config) authenticate(client *http.Client, cookies cookies, uri, user, p
215218
query.Add("username", user)
216219
query.Add("password", pass)
217220
query.Add("csrf", csrf)
221+
query.Add("g-recaptcha-response", "")
218222

219223
req, err := http.NewRequest("POST", uri, strings.NewReader(query.Encode()))
220224
if err != nil {
@@ -233,7 +237,12 @@ func (c *Config) authenticate(client *http.Client, cookies cookies, uri, user, p
233237
}
234238

235239
if res.StatusCode != 302 {
236-
return "", errors.New("authentication failed")
240+
body, _ := ioutil.ReadAll(res.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])
237246
}
238247

239248
// Follow redirect to hydra

install.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ import (
1717
"net/http"
1818
"os"
1919
"strings"
20+
"syscall"
2021
"time"
2122

23+
"golang.org/x/crypto/ssh/terminal"
24+
2225
"github.com/bcmi-labs/arduino-connector/auth"
2326
mqtt "github.com/eclipse/paho.mqtt.golang"
2427
"github.com/facchinm/service"
@@ -102,12 +105,28 @@ func askCredentials() (token string, err error) {
102105
fmt.Println("Insert your arduino username")
103106
fmt.Scanln(&user)
104107
fmt.Println("Insert your arduino password")
105-
fmt.Scanln(&pass)
106108

107-
auth := auth.New()
108-
auth.ClientID = "connector"
109-
auth.Scopes = "iot:devices"
110-
tok, err := auth.Token(user, pass)
109+
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
110+
if err != nil {
111+
return "", err
112+
}
113+
pass = string(bytePassword)
114+
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+
}
111130
if err != nil {
112131
return "", err
113132
}

0 commit comments

Comments
 (0)