Skip to content

Commit 6a5aa20

Browse files
author
Matthew Fisher
committed
feat(auth): add auth:whoami endpoint
1 parent 1fdf171 commit 6a5aa20

4 files changed

Lines changed: 77 additions & 3 deletions

File tree

api/config_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ TCP Socket Probe: N/A`)
2222
}
2323

2424
h.HTTPGet = &HTTPGetProbe{
25-
Path: "/",
26-
Port: 80,
27-
HTTPHeaders: []*KVPair{{ Key: "X-DEIS-IS", Value: "AWESOME" }},
25+
Path: "/",
26+
Port: 80,
27+
HTTPHeaders: []*KVPair{{Key: "X-DEIS-IS", Value: "AWESOME"}},
2828
}
2929

3030
expected = strings.TrimSpace(`Initial Delay (seconds): 0

api/users.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package api
22

3+
import (
4+
"fmt"
5+
)
6+
37
// User is the definition of the user object.
48
type User struct {
59
ID int `json:"id"`
@@ -13,3 +17,19 @@ type User struct {
1317
IsActive bool `json:"is_active"`
1418
DateJoined string `json:"date_joined"`
1519
}
20+
21+
func (u User) String() string {
22+
tpl := `ID: %d
23+
Username: %s
24+
Email: %s
25+
First Name: %s
26+
Last Name: %s
27+
Last Login: %s
28+
Is Superuser: %t
29+
Is Staff: %t
30+
Is Active: %t
31+
Date Joined: %s`
32+
33+
return fmt.Sprintf(tpl, u.ID, u.Username, u.Email, u.FirstName, u.LastName, u.LastLogin,
34+
u.IsSuperuser, u.IsStaff, u.IsActive, u.DateJoined)
35+
}

api/users_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package api
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestUserString(t *testing.T) {
8+
user := User{
9+
ID: 1,
10+
Username: "bacongobbler",
11+
Email: "matthewf@deis.com",
12+
FirstName: "Matthew",
13+
LastName: "Fisher",
14+
LastLogin: "Yesterday",
15+
IsSuperuser: true,
16+
IsStaff: true,
17+
IsActive: true,
18+
DateJoined: "Yesterday",
19+
}
20+
21+
expected := `ID: 1
22+
Username: bacongobbler
23+
Email: matthewf@deis.com
24+
First Name: Matthew
25+
Last Name: Fisher
26+
Last Login: Yesterday
27+
Is Superuser: true
28+
Is Staff: true
29+
Is Active: true
30+
Date Joined: Yesterday`
31+
32+
if user.String() != expected {
33+
t.Errorf("Got:\n\n%s\n\nExpected:\n\n%s", user.String(), expected)
34+
}
35+
}

auth/auth.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,22 @@ func Passwd(c *deis.Client, username, password, newPassword string) error {
148148
}
149149
return err
150150
}
151+
152+
func Whoami(c *deis.Client) (api.User, error) {
153+
res, err := c.Request("GET", "/v2/auth/whoami/", nil)
154+
if err != nil {
155+
return api.User{}, err
156+
}
157+
// Fix json.Decoder bug in <go1.7
158+
defer func() {
159+
io.Copy(ioutil.Discard, res.Body)
160+
res.Body.Close()
161+
}()
162+
163+
resUser := api.User{}
164+
if err = json.NewDecoder(res.Body).Decode(&resUser); err != nil {
165+
return api.User{}, err
166+
}
167+
168+
return resUser, nil
169+
}

0 commit comments

Comments
 (0)