From 5a28fa83967610de6c559b83693dbdd366605436 Mon Sep 17 00:00:00 2001 From: Matteo Suppo Date: Fri, 22 Dec 2017 11:29:16 +0100 Subject: [PATCH] Allow the connector to be compiled with development info Signed-off-by: Matteo Suppo --- arduino-connector.sh | 2 ++ install.go | 21 +++++++++++---------- main.go | 7 ++++++- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/arduino-connector.sh b/arduino-connector.sh index 670475e0..e8110857 100755 --- a/arduino-connector.sh +++ b/arduino-connector.sh @@ -22,6 +22,8 @@ export TOKEN=$token export HTTP_PROXY=$http_proxy export HTTPS_PROXY=$https_proxy export ALL_PROXY=$all_proxy +export AUTHURL=$authurl +export APIURL=$apiurl echo printenv echo --------- diff --git a/install.go b/install.go index 6f6c1cd3..a1a92432 100644 --- a/install.go +++ b/install.go @@ -48,8 +48,7 @@ import ( ) const ( - rsaBits = 2048 - devicesAPI = "https://api2.arduino.cc/devices/v1" + rsaBits = 2048 ) // Install installs the program as a service @@ -65,7 +64,7 @@ func register(config Config, token string) { // Request token var err error if token == "" { - token, err = askCredentials() + token, err = askCredentials(config.AuthURL) check(err, "AskCredentials") } @@ -88,7 +87,7 @@ func register(config Config, token string) { // Request a certificate fmt.Println("Request certificate") - pem, err := requestCert(config.ID, token, csr) + pem, err := requestCert(config.APIURL, config.ID, token, csr) check(err, "requestCert") err = ioutil.WriteFile("certificate.pem", []byte(pem), 0600) @@ -96,7 +95,7 @@ func register(config Config, token string) { // Request URL fmt.Println("Request mqtt url") - config.URL, err = requestURL(token) + config.URL, err = requestURL(config.APIURL, token) check(err, "requestURL") // Write the configuration @@ -118,7 +117,7 @@ func register(config Config, token string) { fmt.Println("Setup completed") } -func askCredentials() (token string, err error) { +func askCredentials(authURL string) (token string, err error) { var user, pass string fmt.Println("Insert your arduino username") fmt.Scanln(&user) @@ -131,6 +130,8 @@ func askCredentials() (token string, err error) { pass = string(bytePassword) authClient := auth.New() + authClient.CodeURL = authURL + "/oauth2/auth" + authClient.TokenURL = authURL + "/oauth2/token" authClient.ClientID = "connector" authClient.Scopes = "iot:devices" @@ -217,7 +218,7 @@ func generateCsr(priv interface{}) ([]byte, error) { return csr, nil } -func requestCert(id, token string, csr []byte) (string, error) { +func requestCert(apiURL, id, token string, csr []byte) (string, error) { client := http.Client{ Timeout: 5 * time.Second, } @@ -227,7 +228,7 @@ func requestCert(id, token string, csr []byte) (string, error) { payload := `{"csr":"` + pemData.String() + `"}` payload = strings.Replace(payload, "\n", "\\n", -1) - req, err := http.NewRequest("POST", devicesAPI+"/"+id, strings.NewReader(payload)) + req, err := http.NewRequest("POST", apiURL+"/"+id, strings.NewReader(payload)) if err != nil { return "", err } @@ -257,12 +258,12 @@ func requestCert(id, token string, csr []byte) (string, error) { return data.Certificate, nil } -func requestURL(token string) (string, error) { +func requestURL(apiURL, token string) (string, error) { client := http.Client{ Timeout: 5 * time.Second, } - req, err := http.NewRequest("POST", devicesAPI+"/connect", nil) + req, err := http.NewRequest("POST", apiURL+"/connect", nil) if err != nil { return "", err } diff --git a/main.go b/main.go index 80e25741..20dd2855 100644 --- a/main.go +++ b/main.go @@ -50,6 +50,8 @@ type Config struct { HTTPProxy string HTTPSProxy string ALLProxy string + AuthURL string + APIURL string } func (c Config) String() string { @@ -62,6 +64,7 @@ func (c Config) String() string { } func main() { + // Read config config := Config{} var doLogin = flag.Bool("login", false, "Do the login and prints out a temporary token") @@ -75,6 +78,8 @@ func main() { flag.StringVar(&config.HTTPProxy, "http_proxy", "", "URL of HTTP proxy to use") flag.StringVar(&config.HTTPSProxy, "https_proxy", "", "URL of HTTPS proxy to use") flag.StringVar(&config.ALLProxy, "all_proxy", "", "URL of SOCKS proxy to use") + flag.StringVar(&config.AuthURL, "authurl", "https://hydra.arduino.cc", "Url of authentication server") + flag.StringVar(&config.APIURL, "apiurl", "https://api2.arduino.cc", "Url of api server") flag.Parse() @@ -83,7 +88,7 @@ func main() { check(err, "CreateService") if *doLogin { - token, err := askCredentials() + token, err := askCredentials(config.AuthURL) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1)