package gonextcloud import ( req "github.com/levigross/grequests" "github.com/partitio/gonextcloud/types" "net/http" ) func (c *Client) AppList() ([]string, error) { res, err := c.baseRequest(routes.apps, "", "", nil, http.MethodGet) if err != nil { return nil, err } var r types.AppListResponse res.JSON(&r) return r.Ocs.Data.Apps, nil } func (c *Client) AppListEnabled() ([]string, error) { ro := &req.RequestOptions{ Params: map[string]string{"filter": "enabled"}, } res, err := c.baseRequest(routes.apps, "", "", ro, http.MethodGet) if err != nil { return nil, err } var r types.AppListResponse res.JSON(&r) return r.Ocs.Data.Apps, nil } func (c *Client) AppListDisabled() ([]string, error) { ro := &req.RequestOptions{ Params: map[string]string{"filter": "disabled"}, } res, err := c.baseRequest(routes.apps, "", "", ro, http.MethodGet) if err != nil { return nil, err } var r types.AppListResponse res.JSON(&r) return r.Ocs.Data.Apps, nil } func (c *Client) AppInfos(name string) (types.App, error) { res, err := c.baseRequest(routes.apps, name, "", nil, http.MethodGet) if err != nil { return types.App{}, err } var r types.AppResponse res.JSON(&r) return r.Ocs.Data, nil } func (c *Client) AppEnable(name string) error { res, err := c.baseRequest(routes.apps, name, "", nil, http.MethodPut) if err != nil { return err } var r types.BaseResponse res.JSON(&r) return nil } func (c *Client) AppDisable(name string) error { res, err := c.baseRequest(routes.apps, name, "", nil, http.MethodDelete) if err != nil { return err } var r types.BaseResponse res.JSON(&r) return nil } func (c *Client) appsBaseRequest(name string, route string, ro *req.RequestOptions, method string) error { _, err := c.baseRequest(routes.apps, name, route, ro, method) return err }
package gonextcloud import ( "fmt" req "github.com/levigross/grequests" "github.com/partitio/gonextcloud/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) // 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 types.CapabilitiesResponse res.JSON(&r) // No need to check for Ocs.Meta.StatusCode as capabilities are always returned c.capabilities = &r.Ocs.Data.Capabilities // Check if authentication failed if !c.loggedIn() { e := types.APIError{Message: "authentication failed"} return &e } return nil } 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 != "" }
/* Package client is a Go client for the Nextcloud Provisioning API. For more information about the Provisioning API, see the documentation: https://docs.nextcloud.com/server/13/admin_manual/configuration_user/user_provisioning_api.html Usage You use the library by creating a client object and calling methods on it. For example, to list all the Nextcloud's instance users: package main import ( "fmt" "github.com/partitio/gonextcloud/client" ) func main() { url := "https://www.mynextcloud.com" username := "admin" password := "password" c, err := client.NewClient(url) if err != nil { panic(err) } if err := c.Login(username, password); err != nil { panic(err) } defer c.Logout() users, err := c.UserList() if err != nil { panic(err) } fmt.Println("Users :", users) } */ package gonextcloud import ( req "github.com/levigross/grequests" "github.com/partitio/gonextcloud/types" "net/url" ) type Client struct { baseURL *url.URL username string password string session *req.Session headers map[string]string capabilities *types.Capabilities } func NewClient(hostname string) (*Client, error) { baseURL, err := url.ParseRequestURI(hostname) if err != nil { baseURL, err = url.ParseRequestURI("https://" + hostname) if err != nil { return nil, err } } c := Client{ baseURL: baseURL, headers: map[string]string{ "OCS-APIREQUEST": "true", "Accept": "application/json", }, } return &c, nil }
package gonextcloud import ( req "github.com/levigross/grequests" "github.com/partitio/gonextcloud/types" "net/http" ) func (c *Client) GroupList() ([]string, error) { res, err := c.baseRequest(routes.groups, "", "", nil, http.MethodGet) if err != nil { return nil, err } var r types.GroupListResponse res.JSON(&r) return r.Ocs.Data.Groups, nil } func (c *Client) GroupUsers(name string) ([]string, error) { res, err := c.baseRequest(routes.groups, name, "", nil, http.MethodGet) if err != nil { return nil, err } var r types.UserListResponse res.JSON(&r) return r.Ocs.Data.Users, nil } func (c *Client) GroupSearch(search string) ([]string, error) { ro := &req.RequestOptions{ Params: map[string]string{"search": search}, } res, err := c.baseRequest(routes.groups, "", "", ro, http.MethodGet) if err != nil { return nil, err } var r types.GroupListResponse res.JSON(&r) return r.Ocs.Data.Groups, nil } func (c *Client) GroupCreate(name string) error { ro := &req.RequestOptions{ Data: map[string]string{ "groupid": name, }, } if err := c.groupBaseRequest("", "", ro, http.MethodPost); err != nil { return err } return nil } func (c *Client) GroupDelete(name string) error { if err := c.groupBaseRequest(name, "", nil, http.MethodDelete); err != nil { return err } return nil } func (c *Client) GroupSubAdminList(name string) ([]string, error) { res, err := c.baseRequest(routes.groups, name, "subadmins", nil, http.MethodGet) if err != nil { return nil, err } var r types.UserListResponse res.JSON(&r) return r.Ocs.Data.Users, nil } func (c *Client) groupBaseRequest(name string, route string, ro *req.RequestOptions, method string) error { _, err := c.baseRequest(routes.groups, name, route, ro, method) return err }
package gonextcloud import ( "encoding/json" "github.com/fatih/structs" req "github.com/levigross/grequests" "github.com/partitio/gonextcloud/types" "net/http" "path" "strconv" "strings" "sync" ) func (c *Client) UserList() ([]string, error) { res, err := c.baseRequest(routes.users, "", "", nil, http.MethodGet) //res, err := c.session.Get(u.String(), nil) if err != nil { return nil, err } var r types.UserListResponse res.JSON(&r) return r.Ocs.Data.Users, nil } func (c *Client) User(name string) (*types.User, error) { if name == "" { return nil, &types.APIError{Message: "name cannot be empty"} } res, err := c.baseRequest(routes.users, name, "", nil, http.MethodGet) if err != nil { return nil, err } var r types.UserResponse js := res.String() // Nextcloud does not encode JSON properly js = reformatJSON(js) if err := json.Unmarshal([]byte(js), &r); err != nil { return nil, err } return &r.Ocs.Data, nil } func (c *Client) UserSearch(search string) ([]string, error) { ro := &req.RequestOptions{ Params: map[string]string{"search": search}, } res, err := c.baseRequest(routes.users, "", "", ro, http.MethodGet) if err != nil { return nil, err } var r types.UserListResponse res.JSON(&r) return r.Ocs.Data.Users, nil } func (c *Client) UserCreate(username string, password string, user *types.User) error { ro := &req.RequestOptions{ Data: map[string]string{ "userid": username, "password": password, }, } if err := c.userBaseRequest("", "", ro, http.MethodPost); err != nil { return err } if user == nil { return nil } return c.UserUpdate(user) } func (c *Client) UserDelete(name string) error { return c.userBaseRequest(name, "", nil, http.MethodDelete) } func (c *Client) UserEnable(name string) error { ro := &req.RequestOptions{ Data: map[string]string{}, } return c.userBaseRequest(name, "enable", ro, http.MethodPut) } func (c *Client) UserDisable(name string) error { ro := &req.RequestOptions{ Data: map[string]string{}, } return c.userBaseRequest(name, "disable", ro, http.MethodPut) } func (c *Client) UserSendWelcomeEmail(name string) error { return c.userBaseRequest(name, "welcome", nil, http.MethodPost) } func (c *Client) UserUpdate(user *types.User) error { m := structs.Map(user) errs := make(chan types.UpdateError) var wg sync.WaitGroup for k := range m { if !ignoredUserField(k) && m[k].(string) != "" { wg.Add(1) go func(key string, value string) { defer wg.Done() if err := c.userUpdateAttribute(user.ID, strings.ToLower(key), value); err != nil { errs <- types.UpdateError{ Field: key, Error: err, } } }(k, m[k].(string)) } } go func() { wg.Wait() close(errs) }() return types.NewUpdateError(errs) } func (c *Client) UserUpdateEmail(name string, email string) error { return c.userUpdateAttribute(name, "email", email) } func (c *Client) UserUpdateDisplayName(name string, displayName string) error { return c.userUpdateAttribute(name, "displayname", displayName) } func (c *Client) UserUpdatePhone(name string, phone string) error { return c.userUpdateAttribute(name, "phone", phone) } func (c *Client) UserUpdateAddress(name string, address string) error { return c.userUpdateAttribute(name, "address", address) } func (c *Client) UserUpdateWebSite(name string, website string) error { return c.userUpdateAttribute(name, "website", website) } func (c *Client) UserUpdateTwitter(name string, twitter string) error { return c.userUpdateAttribute(name, "twitter", twitter) } func (c *Client) UserUpdatePassword(name string, password string) error { return c.userUpdateAttribute(name, "password", password) } func (c *Client) UserUpdateQuota(name string, quota int) error { return c.userUpdateAttribute(name, "quota", strconv.Itoa(quota)) } func (c *Client) UserGroupList(name string) ([]string, error) { res, err := c.baseRequest(routes.users, name, "groups", nil, http.MethodGet) if err != nil { return nil, err } var r types.GroupListResponse res.JSON(&r) return r.Ocs.Data.Groups, nil } func (c *Client) UserGroupAdd(name string, group string) error { ro := &req.RequestOptions{ Data: map[string]string{ "groupid": group, }, } return c.userBaseRequest(name, "groups", ro, http.MethodPost) } func (c *Client) UserGroupRemove(name string, group string) error { ro := &req.RequestOptions{ Data: map[string]string{ "groupid": group, }, } return c.userBaseRequest(name, "groups", ro, http.MethodDelete) } func (c *Client) UserGroupPromote(name string, group string) error { ro := &req.RequestOptions{ Data: map[string]string{ "groupid": group, }, } return c.userBaseRequest(name, "subadmins", ro, http.MethodPost) } func (c *Client) UserGroupDemote(name string, group string) error { ro := &req.RequestOptions{ Data: map[string]string{ "groupid": group, }, } return c.userBaseRequest(name, "subadmins", ro, http.MethodDelete) } func (c *Client) UserGroupSubAdminList(name string) ([]string, error) { if !c.loggedIn() { return nil, unauthorized } u := c.baseURL.ResolveReference(routes.users) u.Path = path.Join(u.Path, name, "subadmins") res, err := c.session.Get(u.String(), nil) if err != nil { return nil, err } var r types.BaseResponse res.JSON(&r) return r.Ocs.Data, nil } func (c *Client) userUpdateAttribute(name string, key string, value string) error { ro := &req.RequestOptions{ Data: map[string]string{ "key": key, "value": value, }, } return c.userBaseRequest(name, "", ro, http.MethodPut) } func (c *Client) userBaseRequest(name string, route string, ro *req.RequestOptions, method string) error { _, err := c.baseRequest(routes.users, name, route, ro, method) return err } func ignoredUserField(key string) bool { keys := []string{"ID", "Quota", "Enabled", "Groups", "Language"} for _, k := range keys { if key == k { return true } } return false }
package gonextcloud import ( "encoding/json" req "github.com/levigross/grequests" "github.com/partitio/gonextcloud/types" "net/http" "net/url" "path" "strings" ) func (c *Client) baseRequest(route *url.URL, name string, subroute string, ro *req.RequestOptions, method string) (*req.Response, error) { if !c.loggedIn() { return nil, unauthorized } u := c.baseURL.ResolveReference(route) if name != "" { u.Path = path.Join(u.Path, name) } if subroute != "" { u.Path = path.Join(u.Path, subroute) } var ( res *req.Response err error ) if method == http.MethodGet { res, err = c.session.Get(u.String(), ro) } else if method == http.MethodPost { res, err = c.session.Post(u.String(), ro) } else if method == http.MethodPut { res, err = c.session.Put(u.String(), ro) } else if method == http.MethodDelete { res, err = c.session.Delete(u.String(), ro) } if err != nil { return nil, err } // As we cannot read the ReaderCloser twice, we use the string content js := res.String() var r types.BaseResponse json.Unmarshal([]byte(js), &r) if r.Ocs.Meta.Statuscode != 100 { err := types.ErrorFromMeta(r.Ocs.Meta) return nil, err } return res, nil } func reformatJSON(json string) string { // Nextcloud encode boolean as string json = strings.Replace(json, "\"true\"", "true", -1) json = strings.Replace(json, "\"false\"", "false", -1) // Nextcloud encode quota as an empty array for never connected users json = strings.Replace(json, "\"quota\":[],", "", -1) return json }