-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauth.go
More file actions
156 lines (127 loc) · 3.91 KB
/
auth.go
File metadata and controls
156 lines (127 loc) · 3.91 KB
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
// Package auth handles user management: creation, deletion, and authentication.
package auth
import (
"encoding/json"
drycc "github.com/drycc/controller-sdk-go"
"github.com/drycc/controller-sdk-go/api"
)
// Register a new user with the controller.
// If controller registration is set to administrators only, a valid administrative
// user token is required in the client.
func Register(c *drycc.Client, username, password, email string) error {
user := api.AuthRegisterRequest{Username: username, Password: password, Email: email}
body, err := json.Marshal(user)
if err != nil {
return err
}
res, err := c.Request("POST", "/v2/auth/register/", body)
if err == nil {
res.Body.Close()
}
return err
}
// Login to the controller and get a token
func Login(c *drycc.Client, username, password string) (string, error) {
user := api.AuthLoginRequest{Username: username, Password: password}
reqBody, err := json.Marshal(user)
if err != nil {
return "", err
}
res, reqErr := c.Request("POST", "/v2/auth/login/", reqBody)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return "", reqErr
}
defer res.Body.Close()
token := api.AuthLoginResponse{}
if err = json.NewDecoder(res.Body).Decode(&token); err != nil {
return "", err
}
return token.Token, reqErr
}
// Delete deletes a user.
func Delete(c *drycc.Client, username string) error {
var body []byte
var err error
if username != "" {
req := api.AuthCancelRequest{Username: username}
body, err = json.Marshal(req)
if err != nil {
return err
}
}
res, err := c.Request("DELETE", "/v2/auth/cancel/", body)
if err == nil {
res.Body.Close()
}
return err
}
// Regenerate auth tokens. This invalidates existing tokens, and if targeting a specific user
// returns a new token.
//
// If username is an empty string and all is false, this regenerates the
// client user's token and will return a new token. Make sure to update the client token
// with this new token to avoid authentication errors.
//
// If username is set and all is false, this will regenerate that user's token
// and return a new token. If not targeting yourself, regenerate requires administrative privileges.
//
// If all is true, this will regenerate every user's token. This requires administrative privileges.
func Regenerate(c *drycc.Client, username string, all bool) (string, error) {
var reqBody []byte
var err error
if all {
reqBody, err = json.Marshal(api.AuthRegenerateRequest{All: all})
} else if username != "" {
reqBody, err = json.Marshal(api.AuthRegenerateRequest{Name: username})
}
if err != nil {
return "", err
}
res, reqErr := c.Request("POST", "/v2/auth/tokens/", reqBody)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return "", reqErr
}
defer res.Body.Close()
if all {
return "", nil
}
token := api.AuthRegenerateResponse{}
if err = json.NewDecoder(res.Body).Decode(&token); err != nil {
return "", err
}
return token.Token, reqErr
}
// Passwd changes a user's password.
//
// If username if an empty string, change the password of the client's user.
//
// If username is set, change the password of another user and do not require
// their password. This requires administrative privileges.
func Passwd(c *drycc.Client, username, password, newPassword string) error {
req := api.AuthPasswdRequest{Password: password, NewPassword: newPassword}
if username != "" {
req.Username = username
}
body, err := json.Marshal(req)
if err != nil {
return err
}
res, err := c.Request("POST", "/v2/auth/passwd/", body)
if err == nil {
res.Body.Close()
}
return err
}
// Whoami retrives the user object for the authenticated user.
func Whoami(c *drycc.Client) (api.User, error) {
res, err := c.Request("GET", "/v2/auth/whoami/", nil)
if err != nil {
return api.User{}, err
}
defer res.Body.Close()
resUser := api.User{}
if err = json.NewDecoder(res.Body).Decode(&resUser); err != nil {
return api.User{}, err
}
return resUser, nil
}