github.com/naphatkrit/deis@v1.12.3/client/controller/models/auth/auth.go (about) 1 package auth 2 3 import ( 4 "encoding/json" 5 6 "github.com/deis/deis/client/controller/api" 7 "github.com/deis/deis/client/controller/client" 8 ) 9 10 // Register a new user with the controller. 11 func Register(c *client.Client, username, password, email string) error { 12 user := api.AuthRegisterRequest{Username: username, Password: password, Email: email} 13 body, err := json.Marshal(user) 14 15 if err != nil { 16 return err 17 } 18 19 _, err = c.BasicRequest("POST", "/v1/auth/register/", body) 20 return err 21 } 22 23 // Login to the controller and get a token 24 func Login(c *client.Client, username, password string) (string, error) { 25 user := api.AuthLoginRequest{Username: username, Password: password} 26 reqBody, err := json.Marshal(user) 27 28 if err != nil { 29 return "", err 30 } 31 32 body, err := c.BasicRequest("POST", "/v1/auth/login/", reqBody) 33 34 if err != nil { 35 return "", err 36 } 37 38 token := api.AuthLoginResponse{} 39 if err = json.Unmarshal([]byte(body), &token); err != nil { 40 return "", err 41 } 42 43 return token.Token, nil 44 } 45 46 // Delete deletes a user. 47 func Delete(c *client.Client, username string) error { 48 var body []byte 49 var err error 50 51 if username != "" { 52 req := api.AuthCancelRequest{Username: username} 53 body, err = json.Marshal(req) 54 55 if err != nil { 56 return err 57 } 58 } 59 60 _, err = c.BasicRequest("DELETE", "/v1/auth/cancel/", body) 61 return err 62 } 63 64 // Regenerate user's auth tokens. 65 func Regenerate(c *client.Client, username string, all bool) (string, error) { 66 var reqBody []byte 67 var err error 68 69 if all == true { 70 reqBody, err = json.Marshal(api.AuthRegenerateRequest{All: all}) 71 } else if username != "" { 72 reqBody, err = json.Marshal(api.AuthRegenerateRequest{Name: username}) 73 } 74 75 if err != nil { 76 return "", err 77 } 78 79 body, err := c.BasicRequest("POST", "/v1/auth/tokens/", reqBody) 80 81 if err != nil { 82 return "", err 83 } 84 85 if all == true { 86 return "", nil 87 } 88 89 token := api.AuthRegenerateResponse{} 90 if err = json.Unmarshal([]byte(body), &token); err != nil { 91 return "", err 92 } 93 94 return token.Token, nil 95 } 96 97 // Passwd changes a user's password. 98 func Passwd(c *client.Client, username, password, newPassword string) error { 99 req := api.AuthPasswdRequest{Password: password, NewPassword: newPassword} 100 101 if username != "" { 102 req.Username = username 103 } 104 105 body, err := json.Marshal(req) 106 107 if err != nil { 108 return err 109 } 110 111 _, err = c.BasicRequest("POST", "/v1/auth/passwd/", body) 112 return err 113 }