2018-07-05 10:50:56 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
req "github.com/levigross/grequests"
|
|
|
|
"github.com/partitio/gonextcloud/client/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
var unauthorized = fmt.Errorf("login first")
|
|
|
|
|
|
|
|
func (c *Client) Login(username string, password string) error {
|
|
|
|
c.username = username
|
|
|
|
c.password = password
|
|
|
|
options := req.RequestOptions{
|
|
|
|
Headers: c.headers,
|
|
|
|
Auth: []string{c.username, c.password},
|
|
|
|
}
|
|
|
|
c.session = req.NewSession(&options)
|
2018-07-09 11:36:07 +00:00
|
|
|
// TODO What to do with capabilities ? (other thant connection validation)
|
2018-07-05 10:50:56 +00:00
|
|
|
u := c.baseURL.ResolveReference(routes.capabilities)
|
2018-07-24 18:26:12 +00:00
|
|
|
res, err := c.session.Get(u.String(), nil)
|
2018-07-05 10:50:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-24 18:26:12 +00:00
|
|
|
var r types.CapabilitiesResponse
|
|
|
|
res.JSON(&r)
|
|
|
|
if r.Ocs.Meta.Statuscode != 100 {
|
|
|
|
e := types.ErrorFromMeta(r.Ocs.Meta)
|
|
|
|
return &e
|
|
|
|
}
|
|
|
|
c.capabilities = &r.Ocs.Data.Capabilities
|
|
|
|
// Check if authentication failed
|
|
|
|
if !c.loggedIn() {
|
|
|
|
e := types.APIError{Message: "authentication failed"}
|
|
|
|
return &e
|
2018-07-09 10:53:53 +00:00
|
|
|
}
|
2018-07-05 10:50:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Logout() error {
|
|
|
|
c.session.CloseIdleConnections()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) loggedIn() bool {
|
2018-07-24 18:26:12 +00:00
|
|
|
// When authentication failed, capabilities doesn't contains core information
|
|
|
|
return c.capabilities.Core.WebdavRoot != ""
|
2018-07-05 10:50:56 +00:00
|
|
|
}
|