Skip to content

Commit

Permalink
feat: api login
Browse files Browse the repository at this point in the history
  • Loading branch information
j0g3sc committed Nov 21, 2023
1 parent c203bad commit 8fa158d
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions internal/provider/client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package provider

import (
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"time"
Expand Down Expand Up @@ -30,6 +32,11 @@ type endpoints struct {
buildingBlocks string
}

type loginResponse struct {
Token string `json:"access_token"`
ExpireSec int `json:"expires_in"`
}

func NewClient(url *url.URL, apiKey string, apiSecret string) (*MeshStackProviderClient, error) {
client := &MeshStackProviderClient{
url: url,
Expand All @@ -49,8 +56,40 @@ func NewClient(url *url.URL, apiKey string, apiSecret string) (*MeshStackProvide
}

func (c *MeshStackProviderClient) login() error {
// TODO
return errors.New(ERROR_AUTHENTICATION_FAILURE)
loginPath, err := url.JoinPath(c.url.String(), loginEndpoint)
if err != nil {
return err
}
loginUrl, _ := url.Parse(loginPath)

res, _ := c.httpClient.Do(
&http.Request{
URL: loginUrl,
Method: "POST",
Header: http.Header{
"client_id": {c.apiKey},
"client_secret": {c.apiSecret},
"grant_type": {"client_credentials"},
},
},
)
defer res.Body.Close()

if err != nil || res.StatusCode != 200 {
return errors.New(ERROR_AUTHENTICATION_FAILURE)
}

data, err := io.ReadAll(res.Body)
if err != nil {
return err
}

var loginResult loginResponse
json.Unmarshal(data, &loginResult)
c.token = loginResult.Token
c.tokenExpiry = time.Now().Add(time.Second * time.Duration(loginResult.ExpireSec))

return nil
}

func (c *MeshStackProviderClient) ensureValidToken() error {
Expand Down

0 comments on commit 8fa158d

Please sign in to comment.