-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathauth.go
More file actions
113 lines (85 loc) · 2.34 KB
/
auth.go
File metadata and controls
113 lines (85 loc) · 2.34 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
package auth
import (
"encoding/json"
"github.com/deis/workflow/client/controller/api"
"github.com/deis/workflow/client/controller/client"
)
// Register a new user with the controller.
func Register(c *client.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
}
_, err = c.BasicRequest("POST", "/v1/auth/register/", body)
return err
}
// Login to the controller and get a token
func Login(c *client.Client, username, password string) (string, error) {
user := api.AuthLoginRequest{Username: username, Password: password}
reqBody, err := json.Marshal(user)
if err != nil {
return "", err
}
body, err := c.BasicRequest("POST", "/v1/auth/login/", reqBody)
if err != nil {
return "", err
}
token := api.AuthLoginResponse{}
if err = json.Unmarshal([]byte(body), &token); err != nil {
return "", err
}
return token.Token, nil
}
// Delete deletes a user.
func Delete(c *client.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
}
}
_, err = c.BasicRequest("DELETE", "/v1/auth/cancel/", body)
return err
}
// Regenerate user's auth tokens.
func Regenerate(c *client.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
}
body, err := c.BasicRequest("POST", "/v1/auth/tokens/", reqBody)
if err != nil {
return "", err
}
if all == true {
return "", nil
}
token := api.AuthRegenerateResponse{}
if err = json.Unmarshal([]byte(body), &token); err != nil {
return "", err
}
return token.Token, nil
}
// Passwd changes a user's password.
func Passwd(c *client.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
}
_, err = c.BasicRequest("POST", "/v1/auth/passwd/", body)
return err
}