This repository has been archived by the owner on May 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
auth_test.go
132 lines (118 loc) · 3.61 KB
/
auth_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
131
132
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v2
import (
"net/http"
"strings"
"testing"
)
func TestBasicAuth(t *testing.T) {
cases := []struct {
*BasicAuthConfig
name string
}{
{
name: "No Auth",
},
{
BasicAuthConfig: &BasicAuthConfig{
Username: "CoolUser",
Password: "HardPassword",
},
name: "Basic Auth",
},
}
for _, tc := range cases {
client := newTestClient(t, tc.name, Version2_11(), true, httpChecks{}, httpReaction{})
client.AuthConfig = &AuthConfig{
BasicAuthConfig: tc.BasicAuthConfig,
}
client.doRequestFunc = addBasicAuthCheck(t, tc.name, tc.BasicAuthConfig, client.doRequestFunc)
_, _ = client.prepareAndDo(http.MethodGet, client.URL, nil, nil, nil)
}
}
func TestBearerAuth(t *testing.T) {
cases := []struct {
*BearerConfig
name string
}{
{
name: "No Auth",
},
{
BearerConfig: &BearerConfig{
Token: "SuchToken",
},
name: "Bearer Auth",
},
}
for _, tc := range cases {
client := newTestClient(t, tc.name, Version2_11(), true, httpChecks{}, httpReaction{})
client.AuthConfig = &AuthConfig{
BearerConfig: tc.BearerConfig,
}
client.doRequestFunc = addBearerAuthCheck(t, tc.name, tc.BearerConfig, client.doRequestFunc)
_, _ = client.prepareAndDo(http.MethodGet, client.URL, nil, nil, nil)
}
}
func addBasicAuthCheck(t *testing.T, name string, authConfig *BasicAuthConfig, f doRequestFunc) doRequestFunc {
return func(request *http.Request) (*http.Response, error) {
u, p, ok := request.BasicAuth()
if !ok && authConfig != nil {
t.Errorf("%s: Expected basic auth in request but none found", name)
return nil, errWalkingGhost
} else if ok && authConfig != nil {
if u != authConfig.Username {
t.Errorf("%s: basic auth username test failed: expected %q but got %q", name, authConfig.Username, u)
return nil, errWalkingGhost
}
if p != authConfig.Password {
t.Errorf("%s: basic auth password test failed: expected %q but got %q", name, authConfig.Password, p)
return nil, errWalkingGhost
}
}
return f(request)
}
}
func addBearerAuthCheck(t *testing.T, name string, authConfig *BearerConfig, f doRequestFunc) doRequestFunc {
return func(request *http.Request) (*http.Response, error) {
auth := request.Header.Get("Authorization")
if auth == "" {
if authConfig != nil {
t.Errorf("%s: Expected bearer auth in request but none found", name)
return nil, errWalkingGhost
}
return f(request)
}
token, ok := parseBearerToken(auth)
if !ok && authConfig != nil {
t.Errorf("%s: Expected bearer auth in request but none found", name)
return nil, errWalkingGhost
} else if ok && authConfig != nil {
if token != authConfig.Token {
t.Errorf("%s: bearer token test failed: expected %q but got %q", name, authConfig.Token, token)
return nil, errWalkingGhost
}
}
return f(request)
}
}
// parseBearerToken parses an HTTP Bearer Authentication token.
// "Bearer abcde" returns ("abcde", true).
func parseBearerToken(auth string) (token string, ok bool) {
const prefix = "Bearer "
if !strings.HasPrefix(auth, prefix) {
return "", false
}
return auth[len(prefix):], true
}