-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauth.go
More file actions
132 lines (106 loc) · 2.65 KB
/
auth.go
File metadata and controls
132 lines (106 loc) · 2.65 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
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.
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, err := c.Request("POST", "/v2/auth/login/", reqBody)
if err != nil {
return "", err
}
// 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, nil
}
// 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 user's auth tokens.
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, err := c.Request("POST", "/v2/auth/tokens/", reqBody)
if err != nil {
return "", err
}
// 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, nil
}
// Passwd changes a user's password.
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
}