-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauth.go
More file actions
169 lines (140 loc) · 4.12 KB
/
auth.go
File metadata and controls
169 lines (140 loc) · 4.12 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
157
158
159
160
161
162
163
164
165
166
167
168
169
// Package auth handles user management: creation, deletion, and authentication.
package auth
import (
"encoding/json"
"io"
"io/ioutil"
deis "github.com/deis/controller-sdk-go"
"github.com/deis/controller-sdk-go/api"
)
// Register a new user with the controller.
// If controller registration is set to administratiors only, a valid administrative
// user token is required in the client.
func Register(c *deis.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 *deis.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 && !deis.IsErrAPIMismatch(reqErr) {
return "", reqErr
}
// Fix json.Decoder bug in <go1.7
defer func() {
io.Copy(ioutil.Discard, res.Body)
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 *deis.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 privilages.
//
// If all is true, this will regenerate every user's token. This requires administrative privilages.
func Regenerate(c *deis.Client, username string, all bool) (string, error) {
var reqBody []byte
var err error
if all == true {
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 err != nil && !deis.IsErrAPIMismatch(reqErr) {
return "", reqErr
}
// Fix json.Decoder bug in <go1.7
defer func() {
io.Copy(ioutil.Discard, res.Body)
res.Body.Close()
}()
if all == true {
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 privilages.
func Passwd(c *deis.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
}
func Whoami(c *deis.Client) (api.User, error) {
res, err := c.Request("GET", "/v2/auth/whoami/", nil)
if err != nil {
return api.User{}, err
}
// Fix json.Decoder bug in <go1.7
defer func() {
io.Copy(ioutil.Discard, res.Body)
res.Body.Close()
}()
resUser := api.User{}
if err = json.NewDecoder(res.Body).Decode(&resUser); err != nil {
return api.User{}, err
}
return resUser, nil
}