Skip to content

Commit 685a57b

Browse files
committed
chore: use new API endpoint for login and improve error message in case of login error
1 parent 9ce201b commit 685a57b

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

client/client.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package client
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"errors"
67
"fmt"
78
"io"
89
"log"
910
"net/http"
1011
"net/url"
11-
"strings"
1212
"time"
1313
)
1414

@@ -46,6 +46,11 @@ type endpoints struct {
4646
Platforms *url.URL `json:"meshplatforms"`
4747
}
4848

49+
type loginRequest struct {
50+
ClientId string `json:"clientId"`
51+
ClientSecret string `json:"clientSecret"`
52+
}
53+
4954
type loginResponse struct {
5055
Token string `json:"access_token"`
5156
ExpireSec int `json:"expires_in"`
@@ -86,20 +91,25 @@ func (c *MeshStackProviderClient) login() error {
8691
return err
8792
}
8893

89-
formData := url.Values{}
90-
formData.Set("client_id", c.apiKey)
91-
formData.Set("client_secret", c.apiSecret)
92-
formData.Set("grant_type", "client_credentials")
94+
loginRequest := loginRequest{
95+
ClientId: c.apiKey,
96+
ClientSecret: c.apiSecret,
97+
}
98+
99+
payload, err := json.Marshal(loginRequest)
100+
if err != nil {
101+
return err
102+
}
93103

94-
req, _ := http.NewRequest(http.MethodPost, loginPath, strings.NewReader(formData.Encode()))
95-
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
104+
req, _ := http.NewRequest(http.MethodPost, loginPath, bytes.NewBuffer(payload))
105+
req.Header.Add("Content-Type", "application/json")
96106

97107
res, err := c.httpClient.Do(req)
98108

99109
if err != nil {
100110
return err
101111
} else if res.StatusCode != 200 {
102-
return errors.New(ERROR_AUTHENTICATION_FAILURE)
112+
return errors.New(fmt.Sprintf("Status %d: %s", res.StatusCode, ERROR_AUTHENTICATION_FAILURE))
103113
}
104114

105115
defer res.Body.Close()

0 commit comments

Comments
 (0)