1- /*
2- * This file is part of arduino-cli.
3- *
4- * arduino-cli is free software; you can redistribute it and/or modify
5- * it under the terms of the GNU General Public License as published by
6- * the Free Software Foundation; either version 2 of the License, or
7- * (at your option) any later version.
8- *
9- * This program is distributed in the hope that it will be useful,
10- * but WITHOUT ANY WARRANTY; without even the implied warranty of
11- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12- * GNU General Public License for more details.
13- *
14- * You should have received a copy of the GNU General Public License
15- * along with this program; if not, write to the Free Software
16- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17- *
18- * As a special exception, you may use this file as part of a free software
19- * library without restriction. Specifically, if other files instantiate
20- * templates or use macros or inline functions from this file, or you compile
21- * this file and link it with other files to produce an executable, this
22- * file does not by itself cause the resulting executable to be covered by
23- * the GNU General Public License. This exception does not however
24- * invalidate any other reasons why the executable file might be covered by
25- * the GNU General Public License.
26- *
27- * Copyright 2017 ARDUINO AG (http://www.arduino.cc/)
28- */
1+ //
2+ // This file is part of arduino-connector
3+ //
4+ // Copyright (C) 2017 Arduino AG (http://www.arduino.cc/)
5+ //
6+ // Licensed under the Apache License, Version 2.0 (the "License");
7+ // you may not use this file except in compliance with the License.
8+ // You may obtain a copy of the License at
9+ //
10+ // http://www.apache.org/licenses/LICENSE-2.0
11+ //
12+ // Unless required by applicable law or agreed to in writing, software
13+ // distributed under the License is distributed on an "AS IS" BASIS,
14+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+ // See the License for the specific language governing permissions and
16+ // limitations under the License.
17+ //
2918
3019// Package auth uses the `oauth2 authorization_code` flow to authenticate with Arduino
3120//
@@ -48,6 +37,7 @@ import (
4837 "math/rand"
4938 "net/http"
5039 "net/url"
40+ "regexp"
5141 "strings"
5242 "time"
5343
@@ -202,13 +192,16 @@ func (c *Config) requestAuth(client *http.Client) (string, cookies, error) {
202192 return res .Request .URL .String (), cookies , err
203193}
204194
195+ var errorRE = regexp .MustCompile (`<div class="error">(?P<error>.*)</div>` )
196+
205197// authenticate uses the user and pass to pass the authentication challenge and returns the authorization_code
206198func (c * Config ) authenticate (client * http.Client , cookies cookies , uri , user , pass string ) (string , error ) {
207199 // Find csrf
208200 csrf := ""
209201 for _ , cookie := range cookies ["auth" ] {
210- if cookie .Name == "_csrf" {
202+ if cookie .Name == "_csrf" && cookie . Value != "" {
211203 csrf = cookie .Value
204+ break ;
212205 }
213206 }
214207 query := url.Values {}
@@ -235,7 +228,11 @@ func (c *Config) authenticate(client *http.Client, cookies cookies, uri, user, p
235228
236229 if res .StatusCode != 302 {
237230 body , _ := ioutil .ReadAll (res .Body )
238- return "" , errors .New ("status = " + res .Status + ", response = " + string (body ))
231+ errs := errorRE .FindStringSubmatch (string (body ))
232+ if len (errs ) < 2 {
233+ return "" , errors .New ("status = " + res .Status + ", response = " + string (body ))
234+ }
235+ return "" , errors .New (errs [1 ])
239236 }
240237
241238 // Follow redirect to hydra
0 commit comments