2016-09-05 00:33:38 +00:00
|
|
|
package writeas
|
|
|
|
|
|
|
|
import (
|
2016-10-05 22:47:54 +00:00
|
|
|
"fmt"
|
2016-09-05 00:33:38 +00:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2016-10-05 22:47:54 +00:00
|
|
|
// LogIn authenticates a user with Write.as.
|
2019-08-15 17:46:30 +00:00
|
|
|
// See https://developers.write.as/docs/api/#authenticate-a-user
|
2016-10-05 22:47:54 +00:00
|
|
|
func (c *Client) LogIn(username, pass string) (*AuthUser, error) {
|
|
|
|
u := &AuthUser{}
|
|
|
|
up := struct {
|
|
|
|
Alias string `json:"alias"`
|
|
|
|
Pass string `json:"pass"`
|
|
|
|
}{
|
|
|
|
Alias: username,
|
|
|
|
Pass: pass,
|
|
|
|
}
|
|
|
|
|
|
|
|
env, err := c.post("/auth/login", up, u)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
if u, ok = env.Data.(*AuthUser); !ok {
|
|
|
|
return nil, fmt.Errorf("Wrong data returned from API.")
|
|
|
|
}
|
|
|
|
|
|
|
|
status := env.Code
|
2018-09-22 20:59:12 +00:00
|
|
|
if status != http.StatusOK {
|
|
|
|
if status == http.StatusBadRequest {
|
|
|
|
return nil, fmt.Errorf("Bad request: %s", env.ErrorMessage)
|
|
|
|
} else if status == http.StatusUnauthorized {
|
|
|
|
return nil, fmt.Errorf("Incorrect password.")
|
|
|
|
} else if status == http.StatusNotFound {
|
|
|
|
return nil, fmt.Errorf("User does not exist.")
|
|
|
|
} else if status == http.StatusTooManyRequests {
|
2019-06-26 18:07:51 +00:00
|
|
|
return nil, fmt.Errorf("Too many log in attempts in a short period of time.")
|
2018-09-22 20:59:12 +00:00
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("Problem authenticating: %d. %v\n", status, err)
|
2016-10-05 22:47:54 +00:00
|
|
|
}
|
2018-09-22 20:59:12 +00:00
|
|
|
|
|
|
|
c.SetToken(u.AccessToken)
|
|
|
|
return u, nil
|
2016-10-05 22:47:54 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 23:11:37 +00:00
|
|
|
// LogOut logs the current user out, making the Client's current access token
|
|
|
|
// invalid.
|
|
|
|
func (c *Client) LogOut() error {
|
|
|
|
env, err := c.delete("/auth/me", nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
status := env.Code
|
|
|
|
if status != http.StatusNoContent {
|
|
|
|
if status == http.StatusNotFound {
|
|
|
|
return fmt.Errorf("Access token is invalid or doesn't exist")
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Unable to log out: %v", env.ErrorMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Logout successful, so update the Client
|
|
|
|
c.token = ""
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-05 00:33:38 +00:00
|
|
|
func (c *Client) isNotLoggedIn(code int) bool {
|
|
|
|
if c.token == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return code == http.StatusUnauthorized
|
|
|
|
}
|