-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathauth_impl.go
56 lines (50 loc) · 1.57 KB
/
auth_impl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package gonextcloud
import (
"fmt"
req "github.com/levigross/grequests"
)
var errUnauthorized = fmt.Errorf("login first")
// Login perform login and create a session with the Nextcloud API.
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)
// TODO What to do with capabilities ? (other thant connection validation)
u := c.baseURL.ResolveReference(routes.capabilities)
res, err := c.session.Get(u.String(), nil)
if err != nil {
return err
}
var r capabilitiesResponse
res.JSON(&r)
// No need to check for Ocs.meta.StatusCode as capabilities are always returned
c.capabilities = &r.Ocs.Data.Capabilities
c.version = &r.Ocs.Data.Version
// Check if authentication failed
if !c.loggedIn() {
e := APIError{Message: "authentication failed"}
return &e
}
// Create webdav client
c.webdav = newWebDav(c.baseURL.String()+"/remote.php/webdav", c.username, c.password)
return nil
}
// Logout logs out from the Nextcloud API, close the session and delete session's cookie
func (c *client) Logout() error {
c.session.CloseIdleConnections()
c.session.HTTPClient.Jar = nil
// Clear capabilities as it is used to check for valid authentication
c.capabilities = nil
return nil
}
func (c *client) loggedIn() bool {
// When authentication failed, capabilities doesn't contains core information
if c.capabilities == nil {
return false
}
return c.capabilities.Core.WebdavRoot != ""
}