@@ -3,140 +3,41 @@ package auth
33
44import (
55 "encoding/json"
6-
6+ "fmt"
77 drycc "github.com/drycc/controller-sdk-go"
88 "github.com/drycc/controller-sdk-go/api"
9+ "net/http"
910)
1011
11- // Register a new user with the controller.
12- // If controller registration is set to administrators only, a valid administrative
13- // user token is required in the client.
14- func Register (c * drycc.Client , username , password , email string ) error {
15- user := api.AuthRegisterRequest {Username : username , Password : password , Email : email }
16- body , err := json .Marshal (user )
17-
18- if err != nil {
19- return err
20- }
21-
22- res , err := c .Request ("POST" , "/v2/auth/register/" , body )
23- if err == nil {
24- res .Body .Close ()
12+ // Login to the controller and get a oauth url
13+ func Login (c * drycc.Client ) (string , error ) {
14+ c .HTTPClient .CheckRedirect = func (req * http.Request , via []* http.Request ) error {
15+ return http .ErrUseLastResponse
2516 }
26- return err
27- }
17+ res , reqErr := c .Request ("POST" , "/v2/auth/login/" , nil )
2818
29- // Login to the controller and get a token
30- func Login (c * drycc.Client , username , password string ) (string , error ) {
31- user := api.AuthLoginRequest {Username : username , Password : password }
32- reqBody , err := json .Marshal (user )
33-
34- if err != nil {
35- return "" , err
36- }
37-
38- res , reqErr := c .Request ("POST" , "/v2/auth/login/" , reqBody )
3919 if reqErr != nil && ! drycc .IsErrAPIMismatch (reqErr ) {
4020 return "" , reqErr
4121 }
4222 defer res .Body .Close ()
4323
44- token := api.AuthLoginResponse {}
45- if err = json .NewDecoder (res .Body ).Decode (& token ); err != nil {
46- return "" , err
47- }
48-
49- return token .Token , reqErr
50- }
51-
52- // Delete deletes a user.
53- func Delete (c * drycc.Client , username string ) error {
54- var body []byte
55- var err error
56-
57- if username != "" {
58- req := api.AuthCancelRequest {Username : username }
59- body , err = json .Marshal (req )
60-
61- if err != nil {
62- return err
63- }
64- }
65-
66- res , err := c .Request ("DELETE" , "/v2/auth/cancel/" , body )
67- if err == nil {
68- res .Body .Close ()
69- }
70- return err
24+ URL := res .Header .Get ("Location" )
25+ return URL , reqErr
7126}
7227
73- // Regenerate auth tokens. This invalidates existing tokens, and if targeting a specific user
74- // returns a new token.
75- //
76- // If username is an empty string and all is false, this regenerates the
77- // client user's token and will return a new token. Make sure to update the client token
78- // with this new token to avoid authentication errors.
79- //
80- // If username is set and all is false, this will regenerate that user's token
81- // and return a new token. If not targeting yourself, regenerate requires administrative privileges.
82- //
83- // If all is true, this will regenerate every user's token. This requires administrative privileges.
84- func Regenerate (c * drycc.Client , username string , all bool ) (string , error ) {
85- var reqBody []byte
86- var err error
87-
88- if all {
89- reqBody , err = json .Marshal (api.AuthRegenerateRequest {All : all })
90- } else if username != "" {
91- reqBody , err = json .Marshal (api.AuthRegenerateRequest {Name : username })
92- }
93-
94- if err != nil {
95- return "" , err
96- }
97-
98- res , reqErr := c .Request ("POST" , "/v2/auth/tokens/" , reqBody )
28+ // Token to the controller and get a token
29+ func Token (c * drycc.Client , key string ) (api.AuthLoginResponse , error ) {
30+ path := fmt .Sprintf ("/v2/auth/token/%s/" , key )
31+ res , reqErr := c .Request ("GET" , path , nil )
9932 if reqErr != nil && ! drycc .IsErrAPIMismatch (reqErr ) {
100- return "" , reqErr
33+ return api. AuthLoginResponse {} , reqErr
10134 }
10235 defer res .Body .Close ()
103-
104- if all {
105- return "" , nil
106- }
107-
108- token := api.AuthRegenerateResponse {}
109- if err = json .NewDecoder (res .Body ).Decode (& token ); err != nil {
110- return "" , err
111- }
112-
113- return token .Token , reqErr
114- }
115-
116- // Passwd changes a user's password.
117- //
118- // If username if an empty string, change the password of the client's user.
119- //
120- // If username is set, change the password of another user and do not require
121- // their password. This requires administrative privileges.
122- func Passwd (c * drycc.Client , username , password , newPassword string ) error {
123- req := api.AuthPasswdRequest {Password : password , NewPassword : newPassword }
124-
125- if username != "" {
126- req .Username = username
127- }
128-
129- body , err := json .Marshal (req )
130-
131- if err != nil {
132- return err
133- }
134-
135- res , err := c .Request ("POST" , "/v2/auth/passwd/" , body )
136- if err == nil {
137- res .Body .Close ()
36+ token := api.AuthLoginResponse {}
37+ if err := json .NewDecoder (res .Body ).Decode (& token ); err != nil {
38+ return api.AuthLoginResponse {}, err
13839 }
139- return err
40+ return token , reqErr
14041}
14142
14243// Whoami retrives the user object for the authenticated user.
0 commit comments