2018-05-04 16:53:21 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Login attempts to login to the current Fleet instance. If login is successful,
|
|
|
|
// an auth token is returned.
|
|
|
|
func (c *Client) Login(email, password string) (string, error) {
|
|
|
|
params := loginRequest{
|
2021-06-24 20:42:29 +00:00
|
|
|
Email: email,
|
2018-05-04 16:53:21 +00:00
|
|
|
Password: password,
|
|
|
|
}
|
|
|
|
|
2021-02-10 20:13:11 +00:00
|
|
|
response, err := c.Do("POST", "/api/v1/fleet/login", "", params)
|
2018-05-04 16:53:21 +00:00
|
|
|
if err != nil {
|
2021-02-10 20:13:11 +00:00
|
|
|
return "", errors.Wrap(err, "POST /api/v1/fleet/login")
|
2018-05-04 16:53:21 +00:00
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
switch response.StatusCode {
|
|
|
|
case http.StatusNotFound:
|
2018-05-04 21:55:57 +00:00
|
|
|
return "", notSetupErr{}
|
2018-05-04 16:53:21 +00:00
|
|
|
}
|
|
|
|
if response.StatusCode != http.StatusOK {
|
2018-05-09 23:54:23 +00:00
|
|
|
return "", errors.Errorf(
|
|
|
|
"login received status %d %s",
|
|
|
|
response.StatusCode,
|
|
|
|
extractServerErrorText(response.Body),
|
|
|
|
)
|
2018-05-04 16:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var responseBody loginResponse
|
|
|
|
err = json.NewDecoder(response.Body).Decode(&responseBody)
|
|
|
|
if err != nil {
|
2018-05-04 21:55:57 +00:00
|
|
|
return "", errors.Wrap(err, "decode login response")
|
2018-05-04 16:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if responseBody.Err != nil {
|
2018-05-04 21:55:57 +00:00
|
|
|
return "", errors.Errorf("login: %s", responseBody.Err)
|
2018-05-04 16:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return responseBody.Token, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Logout attempts to logout to the current Fleet instance.
|
|
|
|
func (c *Client) Logout() error {
|
2021-02-10 20:13:11 +00:00
|
|
|
response, err := c.AuthenticatedDo("POST", "/api/v1/fleet/logout", "", nil)
|
2018-05-04 16:53:21 +00:00
|
|
|
if err != nil {
|
2021-02-10 20:13:11 +00:00
|
|
|
return errors.Wrap(err, "POST /api/v1/fleet/logout")
|
2018-05-04 16:53:21 +00:00
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
if response.StatusCode != http.StatusOK {
|
2018-05-09 23:54:23 +00:00
|
|
|
return errors.Errorf(
|
|
|
|
"logout received status %d %s",
|
|
|
|
response.StatusCode,
|
|
|
|
extractServerErrorText(response.Body),
|
|
|
|
)
|
2018-05-04 16:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var responseBody logoutResponse
|
2018-05-04 21:55:57 +00:00
|
|
|
err = json.NewDecoder(response.Body).Decode(&responseBody)
|
2018-05-04 16:53:21 +00:00
|
|
|
if err != nil {
|
2018-05-04 21:55:57 +00:00
|
|
|
return errors.Wrap(err, "decode logout response")
|
2018-05-04 16:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if responseBody.Err != nil {
|
2018-05-04 21:55:57 +00:00
|
|
|
return errors.Errorf("logout: %s", responseBody.Err)
|
2018-05-04 16:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|