This repository was archived by the owner on Apr 13, 2025. It is now read-only.
forked from oauth2-proxy/oauth2-proxy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproviders_test.go
207 lines (170 loc) · 5.48 KB
/
providers_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package providers
import (
"os"
"testing"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
. "github.com/onsi/gomega"
)
const (
clientID = "bazquux"
clientSecret = "xyzzyplugh"
providerID = "providerID"
msIssuerURL = "https://login.microsoftonline.com/fabrikamb2c.onmicrosoft.com/v2.0/"
msKeysURL = "https://login.microsoftonline.com/fabrikamb2c.onmicrosoft.com/discovery/v2.0/keys"
msAuthURL = "https://login.microsoftonline.com/fabrikamb2c.onmicrosoft.com/oauth2/v2.0/authorize?p=b2c_1_sign_in"
msTokenURL = "https://login.microsoftonline.com/fabrikamb2c.onmicrosoft.com/oauth2/v2.0/token?p=b2c_1_sign_in"
)
func TestClientSecretFileOptionFails(t *testing.T) {
g := NewWithT(t)
providerConfig := options.Provider{
ID: providerID,
Type: "google",
ClientID: clientID,
ClientSecretFile: clientSecret,
}
p, err := newProviderDataFromConfig(providerConfig)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(p.ClientSecretFile).To(Equal(clientSecret))
g.Expect(p.ClientSecret).To(BeEmpty())
s, err := p.GetClientSecret()
g.Expect(err).To(HaveOccurred())
g.Expect(s).To(BeEmpty())
}
func TestClientSecretFileOption(t *testing.T) {
g := NewWithT(t)
f, err := os.CreateTemp("", "client_secret_temp_file_")
g.Expect(err).ToNot(HaveOccurred())
clientSecretFileName := f.Name()
defer func() {
g.Expect(f.Close()).To(Succeed())
g.Expect(os.Remove(clientSecretFileName)).To(Succeed())
}()
_, err = f.WriteString("testcase")
g.Expect(err).ToNot(HaveOccurred())
providerConfig := options.Provider{
ID: providerID,
Type: "google",
ClientID: clientID,
ClientSecretFile: clientSecretFileName,
}
p, err := newProviderDataFromConfig(providerConfig)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(p.ClientSecretFile).To(Equal(clientSecretFileName))
g.Expect(p.ClientSecret).To(BeEmpty())
s, err := p.GetClientSecret()
g.Expect(err).ToNot(HaveOccurred())
g.Expect(s).To(Equal("testcase"))
}
func TestSkipOIDCDiscovery(t *testing.T) {
g := NewWithT(t)
providerConfig := options.Provider{
ID: providerID,
Type: "oidc",
ClientID: clientID,
ClientSecretFile: clientSecret,
OIDCConfig: options.OIDCOptions{
IssuerURL: msIssuerURL,
SkipDiscovery: true,
},
}
_, err := newProviderDataFromConfig(providerConfig)
g.Expect(err).To(MatchError("error building OIDC ProviderVerifier: invalid provider verifier options: missing required setting: jwks-url"))
providerConfig.LoginURL = msAuthURL
providerConfig.RedeemURL = msTokenURL
providerConfig.OIDCConfig.JwksURL = msKeysURL
_, err = newProviderDataFromConfig(providerConfig)
g.Expect(err).ToNot(HaveOccurred())
}
func TestURLsCorrectlyParsed(t *testing.T) {
g := NewWithT(t)
providerConfig := options.Provider{
ID: providerID,
Type: "oidc",
ClientID: clientID,
ClientSecretFile: clientSecret,
LoginURL: msAuthURL,
RedeemURL: msTokenURL,
OIDCConfig: options.OIDCOptions{
IssuerURL: msIssuerURL,
SkipDiscovery: true,
JwksURL: msKeysURL,
},
}
pd, err := newProviderDataFromConfig(providerConfig)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(pd.LoginURL.String()).To(Equal(msAuthURL))
g.Expect(pd.RedeemURL.String()).To(Equal(msTokenURL))
}
func TestScope(t *testing.T) {
g := NewWithT(t)
testCases := []struct {
name string
configuredScope string
expectedScope string
allowedGroups []string
}{
{
name: "with no scope provided",
configuredScope: "",
expectedScope: "openid email profile",
},
{
name: "with no scope provided and groups",
configuredScope: "",
expectedScope: "openid email profile groups",
allowedGroups: []string{"foo"},
},
{
name: "with a configured scope provided",
configuredScope: "openid",
expectedScope: "openid",
},
}
for _, tc := range testCases {
providerConfig := options.Provider{
ID: providerID,
Type: "oidc",
ClientID: clientID,
ClientSecretFile: clientSecret,
LoginURL: msAuthURL,
RedeemURL: msTokenURL,
Scope: tc.configuredScope,
AllowedGroups: tc.allowedGroups,
OIDCConfig: options.OIDCOptions{
IssuerURL: msIssuerURL,
SkipDiscovery: true,
JwksURL: msKeysURL,
},
}
pd, err := newProviderDataFromConfig(providerConfig)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(pd.Scope).To(Equal(tc.expectedScope))
}
}
func TestForcedMethodS256(t *testing.T) {
g := NewWithT(t)
options := options.NewOptions()
options.Providers[0].CodeChallengeMethod = CodeChallengeMethodS256
method := parseCodeChallengeMethod(options.Providers[0])
g.Expect(method).To(Equal(CodeChallengeMethodS256))
}
func TestForcedMethodPlain(t *testing.T) {
g := NewWithT(t)
options := options.NewOptions()
options.Providers[0].CodeChallengeMethod = CodeChallengeMethodPlain
method := parseCodeChallengeMethod(options.Providers[0])
g.Expect(method).To(Equal(CodeChallengeMethodPlain))
}
func TestPrefersS256(t *testing.T) {
g := NewWithT(t)
options := options.NewOptions()
method := parseCodeChallengeMethod(options.Providers[0])
g.Expect(method).To(Equal(""))
}
func TestCanOverwriteS256(t *testing.T) {
g := NewWithT(t)
options := options.NewOptions()
options.Providers[0].CodeChallengeMethod = "plain"
method := parseCodeChallengeMethod(options.Providers[0])
g.Expect(method).To(Equal(CodeChallengeMethodPlain))
}