12 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
13 "github.com/stretchr/testify/assert"
17 cookieSecret = "secretthirtytwobytes+abcdefghijk"
19 clientSecret = "xyzzyplugh"
22func testOptions() *options.Options {
23 o := options.NewOptions()
24 o.UpstreamServers = append(o.UpstreamServers, options.Upstream{
27 URI: "http://127.0.0.1:8080/",
29 o.Cookie.Secret = cookieSecret
31 o.ClientSecret = clientSecret
32 o.EmailDomains = []string{"*"}
36func errorMsg(msgs []string) string {
37 result := make([]string, 0)
38 result = append(result, "invalid configuration:")
39 result = append(result, msgs...)
40 return strings.Join(result, "\n ")
43func TestNewOptions(t *testing.T) {
44 o := options.NewOptions()
45 o.EmailDomains = []string{"*"}
47 assert.NotEqual(t, nil, err)
49 expected := errorMsg([]string{
50 "missing setting: cookie-secret",
51 "missing setting: client-id",
52 "missing setting: client-secret or client-secret-file"})
53 assert.Equal(t, expected, err.Error())
56func TestClientSecretFileOptionFails(t *testing.T) {
57 o := options.NewOptions()
58 o.Cookie.Secret = cookieSecret
60 o.ClientSecretFile = clientSecret
61 o.EmailDomains = []string{"*"}
63 assert.NotEqual(t, nil, err)
65 p := o.GetProvider().Data()
66 assert.Equal(t, clientSecret, p.ClientSecretFile)
67 assert.Equal(t, "", p.ClientSecret)
69 s, err := p.GetClientSecret()
70 assert.NotEqual(t, nil, err)
71 assert.Equal(t, "", s)
74func TestClientSecretFileOption(t *testing.T) {
76 f, err := ioutil.TempFile("", "client_secret_temp_file_")
78 t.Fatalf("failed to create temp file: %v", err)
80 _, err = f.WriteString("testcase")
82 t.Fatalf("failed to write to temp file: %v", err)
84 if err := f.Close(); err != nil {
85 t.Fatalf("failed to close temp file: %v", err)
87 clientSecretFileName := f.Name()
88 defer func(t *testing.T) {
89 if err := os.Remove(clientSecretFileName); err != nil {
90 t.Fatalf("failed to delete temp file: %v", err)
94 o := options.NewOptions()
95 o.Cookie.Secret = cookieSecret
97 o.ClientSecretFile = clientSecretFileName
98 o.EmailDomains = []string{"*"}
100 assert.Equal(t, nil, err)
102 p := o.GetProvider().Data()
103 assert.Equal(t, clientSecretFileName, p.ClientSecretFile)
104 assert.Equal(t, "", p.ClientSecret)
106 s, err := p.GetClientSecret()
107 assert.Equal(t, nil, err)
108 assert.Equal(t, "testcase", s)
111func TestGoogleGroupOptions(t *testing.T) {
113 o.GoogleGroups = []string{"googlegroup"}
115 assert.NotEqual(t, nil, err)
117 expected := errorMsg([]string{
118 "missing setting: google-admin-email",
119 "missing setting: google-service-account-json"})
120 assert.Equal(t, expected, err.Error())
123func TestGoogleGroupInvalidFile(t *testing.T) {
125 o.GoogleGroups = []string{"test_group"}
126 o.GoogleAdminEmail = "admin@example.com"
127 o.GoogleServiceAccountJSON = "file_doesnt_exist.json"
129 assert.NotEqual(t, nil, err)
131 expected := errorMsg([]string{
132 "invalid Google credentials file: file_doesnt_exist.json",
134 assert.Equal(t, expected, err.Error())
137func TestInitializedOptions(t *testing.T) {
139 assert.Equal(t, nil, Validate(o))
142// Note that it's not worth testing nonparseable URLs, since url.Parse()
143// seems to parse damn near anything.
144func TestRedirectURL(t *testing.T) {
146 o.RawRedirectURL = "https://myhost.com/oauth2/callback"
147 assert.Equal(t, nil, Validate(o))
148 expected := &url.URL{
149 Scheme: "https", Host: "myhost.com", Path: "/oauth2/callback"}
150 assert.Equal(t, expected, o.GetRedirectURL())
153func TestDefaultProviderApiSettings(t *testing.T) {
155 assert.Equal(t, nil, Validate(o))
156 p := o.GetProvider().Data()
157 assert.Equal(t, "https://accounts.google.com/o/oauth2/auth?access_type=offline",
159 assert.Equal(t, "https://www.googleapis.com/oauth2/v3/token",
160 p.RedeemURL.String())
161 assert.Equal(t, "", p.ProfileURL.String())
162 assert.Equal(t, "profile email", p.Scope)
165func TestCookieRefreshMustBeLessThanCookieExpire(t *testing.T) {
167 assert.Equal(t, nil, Validate(o))
169 o.Cookie.Secret = "0123456789abcdef"
170 o.Cookie.Refresh = o.Cookie.Expire
171 assert.NotEqual(t, nil, Validate(o))
173 o.Cookie.Refresh -= time.Duration(1)
174 assert.Equal(t, nil, Validate(o))
177func TestBase64CookieSecret(t *testing.T) {
179 assert.Equal(t, nil, Validate(o))
181 // 32 byte, base64 (urlsafe) encoded key
182 o.Cookie.Secret = "yHBw2lh2Cvo6aI_jn_qMTr-pRAjtq0nzVgDJNb36jgQ="
183 assert.Equal(t, nil, Validate(o))
185 // 32 byte, base64 (urlsafe) encoded key, w/o padding
186 o.Cookie.Secret = "yHBw2lh2Cvo6aI_jn_qMTr-pRAjtq0nzVgDJNb36jgQ"
187 assert.Equal(t, nil, Validate(o))
189 // 24 byte, base64 (urlsafe) encoded key
190 o.Cookie.Secret = "Kp33Gj-GQmYtz4zZUyUDdqQKx5_Hgkv3"
191 assert.Equal(t, nil, Validate(o))
193 // 16 byte, base64 (urlsafe) encoded key
194 o.Cookie.Secret = "LFEqZYvYUwKwzn0tEuTpLA=="
195 assert.Equal(t, nil, Validate(o))
197 // 16 byte, base64 (urlsafe) encoded key, w/o padding
198 o.Cookie.Secret = "LFEqZYvYUwKwzn0tEuTpLA"
199 assert.Equal(t, nil, Validate(o))
202func TestValidateSignatureKey(t *testing.T) {
204 o.SignatureKey = "sha1:secret"
205 assert.Equal(t, nil, Validate(o))
206 assert.Equal(t, o.GetSignatureData().Hash, crypto.SHA1)
207 assert.Equal(t, o.GetSignatureData().Key, "secret")
210func TestValidateSignatureKeyInvalidSpec(t *testing.T) {
212 o.SignatureKey = "invalid spec"
214 assert.Equal(t, err.Error(), "invalid configuration:\n"+
215 " invalid signature hash:key spec: "+o.SignatureKey)
218func TestValidateSignatureKeyUnsupportedAlgorithm(t *testing.T) {
220 o.SignatureKey = "unsupported:default secret"
222 assert.Equal(t, err.Error(), "invalid configuration:\n"+
223 " unsupported signature hash algorithm: "+o.SignatureKey)
226func TestSkipOIDCDiscovery(t *testing.T) {
228 o.ProviderType = "oidc"
229 o.OIDCIssuerURL = "https://login.microsoftonline.com/fabrikamb2c.onmicrosoft.com/v2.0/"
230 o.SkipOIDCDiscovery = true
233 assert.Equal(t, "invalid configuration:\n"+
234 " missing setting: login-url\n missing setting: redeem-url\n missing setting: oidc-jwks-url", err.Error())
236 o.LoginURL = "https://login.microsoftonline.com/fabrikamb2c.onmicrosoft.com/oauth2/v2.0/authorize?p=b2c_1_sign_in"
237 o.RedeemURL = "https://login.microsoftonline.com/fabrikamb2c.onmicrosoft.com/oauth2/v2.0/token?p=b2c_1_sign_in"
238 o.OIDCJwksURL = "https://login.microsoftonline.com/fabrikamb2c.onmicrosoft.com/discovery/v2.0/keys"
240 assert.Equal(t, nil, Validate(o))
243func TestGCPHealthcheck(t *testing.T) {
245 o.GCPHealthChecks = true
246 assert.Equal(t, nil, Validate(o))
249func TestRealClientIPHeader(t *testing.T) {
250 // Ensure nil if ReverseProxy not set.
252 o.RealClientIPHeader = "X-Real-IP"
253 assert.Equal(t, nil, Validate(o))
254 assert.Nil(t, o.GetRealClientIPParser())
256 // Ensure simple use case works.
258 o.ReverseProxy = true
259 o.RealClientIPHeader = "X-Forwarded-For"
260 assert.Equal(t, nil, Validate(o))
261 assert.NotNil(t, o.GetRealClientIPParser())
263 // Ensure unknown header format process an error.
265 o.ReverseProxy = true
266 o.RealClientIPHeader = "Forwarded"
268 assert.NotEqual(t, nil, err)
269 expected := errorMsg([]string{
270 "real_client_ip_header (Forwarded) not accepted parameter value: the http header key (Forwarded) is either invalid or unsupported",
272 assert.Equal(t, expected, err.Error())
273 assert.Nil(t, o.GetRealClientIPParser())
275 // Ensure invalid header format produces an error.
277 o.ReverseProxy = true
278 o.RealClientIPHeader = "!934invalidheader-23:"
280 assert.NotEqual(t, nil, err)
281 expected = errorMsg([]string{
282 "real_client_ip_header (!934invalidheader-23:) not accepted parameter value: the http header key (!934invalidheader-23:) is either invalid or unsupported",
284 assert.Equal(t, expected, err.Error())
285 assert.Nil(t, o.GetRealClientIPParser())
288func TestProviderCAFilesError(t *testing.T) {
289 file, err := ioutil.TempFile("", "absent.*.crt")
290 assert.NoError(t, err)
291 assert.NoError(t, file.Close())
292 assert.NoError(t, os.Remove(file.Name()))
295 o.ProviderCAFiles = append(o.ProviderCAFiles, file.Name())
298 assert.Contains(t, err.Error(), "unable to load provider CA file(s)")
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list