-
Notifications
You must be signed in to change notification settings - Fork 50
/
access.go
232 lines (193 loc) · 6.64 KB
/
access.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package proxmox
import (
"context"
"errors"
"fmt"
"net/url"
)
// Deprecated: Use WithCredentials Option
func (c *Client) Login(ctx context.Context, username, password string) error {
_, err := c.Ticket(ctx, &Credentials{
Username: username,
Password: password,
})
return err
}
// Deprecated: Use the WithAPIToken Option
func (c *Client) APIToken(tokenID, secret string) {
c.token = fmt.Sprintf("%s=%s", tokenID, secret)
}
func (c *Client) Ticket(ctx context.Context, credentials *Credentials) (*Session, error) {
return c.session, c.Post(ctx, "/access/ticket", credentials, &c.session)
}
func (c *Client) ACL(ctx context.Context) (acl ACLs, err error) {
return acl, c.Get(ctx, "/access/acl", &acl)
}
func (c *Client) UpdateACL(ctx context.Context, acl ACL) error {
return c.Put(ctx, "/access/acl", &acl, nil)
}
// Permissions get permissions for the current user for the client which passes no params, use Permission
func (c *Client) Permissions(ctx context.Context, o *PermissionsOptions) (permissions Permissions, err error) {
u := url.URL{Path: "/access/permissions"}
if o != nil { // params are optional
params := url.Values{}
if o.UserID != "" {
params.Add("userid", o.UserID)
}
if o.Path != "" {
params.Add("path", o.Path)
}
u.RawQuery = params.Encode()
}
return permissions, c.Get(ctx, u.String(), &permissions)
}
func (c *Client) Password(ctx context.Context, userid, password string) error {
return c.Put(ctx, "/access/password", map[string]string{
"userid": userid,
"password": password,
}, nil)
}
// NewDomain create a new domain with the required two parameters pull it and use domain.Update to configure
func (c *Client) NewDomain(ctx context.Context, realm string, domainType DomainType) error {
return c.Post(ctx, "/access/domains", map[string]string{
"realm": realm,
"type": string(domainType),
}, nil)
}
func (c *Client) Domain(ctx context.Context, realm string) (domain *Domain, err error) {
err = c.Get(ctx, fmt.Sprintf("/access/domains/%s", realm), &domain)
if nil == err {
domain.Realm = realm
domain.client = c
}
return
}
func (c *Client) Domains(ctx context.Context) (domains Domains, err error) {
err = c.Get(ctx, "/access/domains", &domains)
if nil == err {
for _, d := range domains {
d.client = c
}
}
return
}
func (d *Domain) Update(ctx context.Context) error {
if d.Realm == "" {
return errors.New("realm can not be empty")
}
return d.client.Put(ctx, fmt.Sprintf("/access/domains/%s", d.Realm), d, nil)
}
func (d *Domain) Delete(ctx context.Context) error {
if d.Realm == "" {
return errors.New("realm can not be empty")
}
return d.client.Delete(ctx, fmt.Sprintf("/access/domains/%s", d.Realm), nil)
}
func (d *Domain) Sync(ctx context.Context, options DomainSyncOptions) error {
if d.Realm == "" {
return errors.New("realm can not be empty")
}
return d.client.Post(ctx, fmt.Sprintf("/access/domains/%s", d.Realm), options, nil)
}
// NewGroup makes a new group, comment is option and can be left empty
func (c *Client) NewGroup(ctx context.Context, groupid, comment string) error {
return c.Post(ctx, "/access/groups", map[string]string{
"groupid": groupid,
"comment": comment,
}, nil)
}
func (c *Client) Group(ctx context.Context, groupid string) (group *Group, err error) {
err = c.Get(ctx, fmt.Sprintf("/access/groups/%s", groupid), &group)
if nil == err {
group.GroupID = groupid
group.client = c
}
return
}
func (c *Client) Groups(ctx context.Context) (groups Groups, err error) {
err = c.Get(ctx, "/access/groups", &groups)
if nil == err {
for _, g := range groups {
g.client = c
}
}
return
}
func (g *Group) Update(ctx context.Context) error {
return g.client.Put(ctx, fmt.Sprintf("/access/groups/%s", g.GroupID), g, nil)
}
func (g *Group) Delete(ctx context.Context) error {
return g.client.Delete(ctx, fmt.Sprintf("/access/groups/%s", g.GroupID), nil)
}
func (c *Client) User(ctx context.Context, userid string) (user *User, err error) {
err = c.Get(ctx, fmt.Sprintf("/access/users/%s", userid), &user)
if nil == err {
user.UserID = userid
user.client = c
}
return
}
func (c *Client) Users(ctx context.Context) (users Users, err error) {
err = c.Get(ctx, "/access/users", &users)
if nil == err {
for _, g := range users {
g.client = c
}
}
return
}
func (c *Client) NewUser(ctx context.Context, user *NewUser) (err error) {
return c.Post(ctx, "/access/users", user, nil)
}
func (u *User) Update(ctx context.Context) error {
return u.client.Put(ctx, fmt.Sprintf("/access/users/%s", u.UserID), u, nil)
}
func (u *User) Delete(ctx context.Context) error {
return u.client.Delete(ctx, fmt.Sprintf("/access/users/%s", u.UserID), nil)
}
func (u *User) GetAPITokens(ctx context.Context) (tokens Tokens, err error) {
return tokens, u.client.Get(ctx, fmt.Sprintf("/access/users/%s/token", u.UserID), &tokens)
}
func (u *User) APIToken(ctx context.Context, tokenid string) (token Token, err error) {
return token, u.client.Get(ctx, fmt.Sprintf("/access/users/%s/token/%s", u.UserID, tokenid), &token)
}
func (u *User) NewAPIToken(ctx context.Context, token Token) (newtoken NewAPIToken, err error) {
return newtoken, u.client.Post(ctx, fmt.Sprintf("/access/users/%s/token/%s", u.UserID, token.TokenID), token, &newtoken)
}
func (u *User) UpdateAPIToken(ctx context.Context, tokenid string) (token Token, err error) {
return token, u.client.Put(ctx, fmt.Sprintf("/access/users/%s/token/%s", u.UserID, tokenid), token, nil)
}
func (u *User) DeleteAPIToken(ctx context.Context, tokenid string) error {
return u.client.Delete(ctx, fmt.Sprintf("/access/users/%s/token/%s", u.UserID, tokenid), nil)
}
func (u *User) GetTFA(ctx context.Context) (tfa TFA, err error) {
return tfa, u.client.Get(ctx, fmt.Sprintf("/access/users/%s/tfa", u.UserID), &tfa)
}
func (u *User) UnlockTFA(ctx context.Context) error {
return u.client.Delete(ctx, fmt.Sprintf("/access/users/%s/tfa", u.UserID), nil)
}
func (c *Client) Role(ctx context.Context, roleid string) (role Permission, err error) {
err = c.Get(ctx, fmt.Sprintf("/access/roles/%s", roleid), &role)
return
}
func (c *Client) NewRole(ctx context.Context, roleID string, privs string) (err error) {
return c.Post(ctx, "/access/roles", map[string]string{
"roleid": roleID,
"privs": privs,
}, nil)
}
func (c *Client) Roles(ctx context.Context) (roles Roles, err error) {
err = c.Get(ctx, "/access/roles", &roles)
if nil == err {
for _, g := range roles {
g.client = c
}
}
return
}
func (r *Role) Update(ctx context.Context) error {
return r.client.Put(ctx, fmt.Sprintf("/access/roles/%s", r.RoleID), r, nil)
}
func (r *Role) Delete(ctx context.Context) error {
return r.client.Delete(ctx, fmt.Sprintf("/access/roles/%s", r.RoleID), nil)
}