-
Notifications
You must be signed in to change notification settings - Fork 0
/
authservice_test.go
130 lines (118 loc) · 3.19 KB
/
authservice_test.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package client_test
import (
"encoding/json"
"io"
"net/http"
"strings"
"time"
"gitlab.bytemark.co.uk/auth/client"
)
// FIXME: test concurrency=1 as a result of using globals here
var fCreds = map[string]client.Credentials{
"good-user": client.Credentials{"username": "good-user", "password": "foo"},
}
var fSessions = map[string]*client.SessionData{
"good-session": &client.SessionData{
Token: "good-session",
Username: "foo",
Factors: []string{"password", "google-auth"},
GroupMemberships: []string{"staff"},
},
"impersonated-session": &client.SessionData{
Token: "impersonated-session",
Username: "bar",
Factors: []string{"impersonated"},
GroupMemberships: []string{"wibble"},
},
}
// This handler just blocks for a second
func SlowHandler(w http.ResponseWriter, r *http.Request) {
time.Sleep(1 * time.Second)
}
func getCreds(w http.ResponseWriter, req *http.Request) (client.Credentials, bool) {
bodyCreds := make(client.Credentials)
data := make([]byte, 4096)
r, err := req.Body.Read(data)
if r == 0 || (err != nil && err != io.EOF) {
http.Error(w, "Error reading body: "+err.Error(), 400)
return bodyCreds, false
}
jErr := json.Unmarshal(data[0:r], &bodyCreds)
if jErr != nil {
http.Error(w, "Error parsing body to JSON: "+jErr.Error(), 400)
return bodyCreds, false
}
return bodyCreds, true
}
func stringResponse(w http.ResponseWriter, resp string) {
_, _ = w.Write([]byte(resp))
}
func fixturesPostHandler(w http.ResponseWriter, r *http.Request, pathBits []string) {
switch pathBits[1] {
case "session":
if r.Header.Get("Content-Type") == "application/json" {
bodyCreds, ok := getCreds(w, r)
if !ok {
return
}
if len(pathBits) == 2 {
ourCreds := fCreds[bodyCreds["username"]]
if ourCreds != nil {
if ourCreds["password"] != bodyCreds["password"] {
w.WriteHeader(403)
return
}
stringResponse(w, "good-session")
return
}
} else {
d := fSessions[pathBits[2]]
if d == nil {
w.WriteHeader(403)
return
}
stringResponse(w, "impersonated-session")
return
}
w.WriteHeader(403)
return
} else {
http.Error(w, `Bad content-type`, 400)
return
}
default:
w.WriteHeader(404)
return
}
}
func fixturesGetHandler(w http.ResponseWriter, r *http.Request, pathBits []string) {
switch pathBits[1] {
case "session":
d := fSessions[pathBits[2]]
if d == nil {
w.WriteHeader(404)
return
}
w.Header().Add("Content-Type", "application/json")
// We construct our own json here. The token is not included in the output.
stringResponse(w, `{"username":"`+d.Username+`","factors":["`+strings.Join(d.Factors, `","`)+`"],`+`"group_memberships":["`+strings.Join(d.GroupMemberships, `","`)+`"]}`)
return
default:
w.WriteHeader(404)
return
}
}
// FixturesHandler is a dummy service that responds like an auth server for
// the limited calls made in these tests.
func FixturesHandler(w http.ResponseWriter, r *http.Request) {
pathBits := strings.Split(r.URL.Path, "/")
switch r.Method {
case "POST":
fixturesPostHandler(w, r, pathBits)
case "GET":
fixturesGetHandler(w, r, pathBits)
default:
w.WriteHeader(405)
return
}
}