-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauth.go
More file actions
75 lines (66 loc) · 1.94 KB
/
auth.go
File metadata and controls
75 lines (66 loc) · 1.94 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
// Package auth handles user management: creation, deletion, and authentication.
package auth
import (
"encoding/json"
"fmt"
"net/http"
drycc "github.com/drycc/controller-sdk-go"
"github.com/drycc/controller-sdk-go/api"
)
// Login to the controller and get a oauth url
func Login(c *drycc.Client, username, password string) (string, error) {
c.HTTPClient.CheckRedirect = func(*http.Request, []*http.Request) error {
return http.ErrUseLastResponse
}
var err error
var body []byte
if username != "" && password != "" {
body, err = json.Marshal(api.AuthLoginRequest{Username: username, Password: password})
if err != nil {
return "", err
}
} else {
body = nil
}
res, err := c.Request("POST", "/v2/auth/login/", body)
if err != nil && !drycc.IsErrAPIMismatch(err) {
return "", err
}
defer res.Body.Close()
if username != "" && password != "" {
login := api.AuthLoginResponse{}
if err := json.NewDecoder(res.Body).Decode(&login); err != nil {
return "", err
}
return login.Key, nil
}
url := res.Header.Get("Location")
return url, err
}
// Token to the controller and get a token
func Token(c *drycc.Client, key, alias string) (api.AuthTokenResponse, error) {
path := fmt.Sprintf("/v2/auth/token/%s/?alias=%s", key, alias)
res, reqErr := c.Request("GET", path, nil)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return api.AuthTokenResponse{}, reqErr
}
defer res.Body.Close()
token := api.AuthTokenResponse{}
if err := json.NewDecoder(res.Body).Decode(&token); err != nil {
return api.AuthTokenResponse{}, err
}
return token, reqErr
}
// Whoami retrives the user object for the authenticated user.
func Whoami(c *drycc.Client) (api.User, error) {
res, err := c.Request("GET", "/v2/auth/whoami/", nil)
if err != nil {
return api.User{}, err
}
defer res.Body.Close()
resUser := api.User{}
if err = json.NewDecoder(res.Body).Decode(&resUser); err != nil {
return api.User{}, err
}
return resUser, nil
}