-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauth.go
More file actions
57 lines (49 loc) · 1.5 KB
/
auth.go
File metadata and controls
57 lines (49 loc) · 1.5 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
// Package auth handles user management: creation, deletion, and authentication.
package auth
import (
"encoding/json"
"fmt"
drycc "github.com/drycc/controller-sdk-go"
"github.com/drycc/controller-sdk-go/api"
"net/http"
)
// Login to the controller and get a oauth url
func Login(c *drycc.Client) (string, error) {
c.HTTPClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
res, reqErr := c.Request("POST", "/v2/auth/login/", nil)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return "", reqErr
}
defer res.Body.Close()
URL := res.Header.Get("Location")
return URL, reqErr
}
// Token to the controller and get a token
func Token(c *drycc.Client, key string) (api.AuthLoginResponse, error) {
path := fmt.Sprintf("/v2/auth/token/%s/", key)
res, reqErr := c.Request("GET", path, nil)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return api.AuthLoginResponse{}, reqErr
}
defer res.Body.Close()
token := api.AuthLoginResponse{}
if err := json.NewDecoder(res.Body).Decode(&token); err != nil {
return api.AuthLoginResponse{}, 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
}