Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions arduino-connector.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 ---------
Expand Down
21 changes: 11 additions & 10 deletions install.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ import (
)

const (
rsaBits = 2048
devicesAPI = "https://api2.arduino.cc/devices/v1"
rsaBits = 2048
)

// Install installs the program as a service
Expand All @@ -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")
}

Expand All @@ -88,15 +87,15 @@ 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)
check(err, "writeCertFile")

// 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
Expand All @@ -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)
Expand All @@ -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"

Expand Down Expand Up @@ -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,
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ type Config struct {
HTTPProxy string
HTTPSProxy string
ALLProxy string
AuthURL string
APIURL string
}

func (c Config) String() string {
Expand All @@ -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")
Expand All @@ -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()

Expand All @@ -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)
Expand Down