forked from convox/rack
-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.go
56 lines (45 loc) · 1.22 KB
/
auth.go
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
package client
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
//Auth is used to transfer the id of the authenticating user when authing against the console API.
//When authing against a rack the id will be blank as user ids are a console concept
type Auth struct {
ID string `json:"id"`
}
//Auth is a request that simply checks whether the password is valid
//in the case of console the users id will be returned, a rack will
//return an empty string
func (c *Client) Auth() (*Auth, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("https://%s/auth", c.Host), nil)
if err != nil {
return nil, err
}
req.SetBasicAuth("convox", string(c.Password))
resp, err := c.client().Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("invalid login\nHave you created an account at https://convox.com/signup?")
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
auth := &Auth{}
// legacy racks return a body of "OK\n"
// legacy consoles return an empty body
if string(body) == "OK\n" || len(body) == 0 {
return auth, nil
}
err = json.Unmarshal(body, &auth)
if err != nil {
return auth, err
}
return auth, nil
}