git.iko.soy/typeable/oauth2-proxy
on master help sign in
Merge pull request #34 from marratj/cookie-separator

Change cookie index separator to underscore

commitfb13ee87c8a301724e45e8056cb6c8b2913cd894 authorJoel Speed <Joel.speed@hotmail.co.uk> dateSun 3 Feb 2019, 13:21 (8 years ago) parentfa2545672d4c49
2 files changed+38−3
README.md+35 oauthproxy.go+3−3
README.md+35
@@ -427,12 +427,47 @@ server {
427427 auth_request_set $auth_cookie $upstream_http_set_cookie;
428428 add_header Set-Cookie $auth_cookie;
429429
430+ # When using the --set-authorization flag, some provider's cookies can exceed the 4kb
431+ # limit and so the OAuth2 Proxy splits these into multiple parts.
432+ # Nginx normally only copies the first `Set-Cookie` header from the auth_request to the response,
433+ # so if your cookies are larger than 4kb, you will need to extract additional cookies manually.
434+ auth_request_set $auth_cookie_name_upstream_1 $upstream_cookie_auth_cookie_name_1;
435+
436+ # Extract the Cookie attributes from the first Set-Cookie header and append them
437+ # to the second part ($upstream_cookie_* variables only contain the raw cookie content)
438+ if ($auth_cookie ~* "(; .*)") {
439+ set $auth_cookie_name_0 $auth_cookie;
440+ set $auth_cookie_name_1 "auth_cookie_name_1=$auth_cookie_name_upstream_1$1";
441+ }
442+
443+ # Send both Set-Cookie headers now if there was a second part
444+ if ($auth_cookie_name_upstream_1) {
445+ add_header Set-Cookie $auth_cookie_name_0;
446+ add_header Set-Cookie $auth_cookie_name_1;
447+ }
448+
430449 proxy_pass http://backend/;
431450 # or "root /path/to/site;" or "fastcgi_pass ..." etc
432451 }
433452}
434453```
435454
455+If you use ingress-nginx in Kubernetes (which includes the Lua module), you also can use the following configuration snippet for your Ingress:
456+
457+```yaml
458+nginx.ingress.kubernetes.io/auth-response-headers: Authorization
459+nginx.ingress.kubernetes.io/auth-signin: https://$host/oauth2/start?rd=$request_uri
460+nginx.ingress.kubernetes.io/auth-url: https://$host/oauth2/auth
461+nginx.ingress.kubernetes.io/configuration-snippet: |
462+ auth_request_set $name_upstream_1 $upstream_cookie_name_1;
463+
464+ access_by_lua_block {
465+ if ngx.var.name_upstream_1 ~= "" then
466+ ngx.header["Set-Cookie"] = "name_1=" .. ngx.var.name_upstream_1 .. ngx.var.auth_cookie:match("(; .*)")
467+ end
468+ }
469+```
470+
436471## Contributing
437472
438473Please see our [Contributing](CONTRIBUTING.md) guidelines.
oauthproxy.go+3−3
@@ -331,7 +331,7 @@ func splitCookie(c *http.Cookie) []*http.Cookie {
331331 count := 0
332332 for len(valueBytes) > 0 {
333333 new := copyCookie(c)
334− new.Name = fmt.Sprintf("%s-%d", c.Name, count)
334+ new.Name = fmt.Sprintf("%s_%d", c.Name, count)
335335 count++
336336 if len(valueBytes) < maxCookieLength {
337337 new.Value = string(valueBytes)
@@ -359,7 +359,7 @@ func joinCookies(cookies []*http.Cookie) (*http.Cookie, error) {
359359 for i := 1; i < len(cookies); i++ {
360360 c.Value += cookies[i].Value
361361 }
362− c.Name = strings.TrimRight(c.Name, "-0")
362+ c.Name = strings.TrimRight(c.Name, "_0")
363363 return c, nil
364364}
365365
@@ -376,7 +376,7 @@ func loadCookie(req *http.Request, cookieName string) (*http.Cookie, error) {
376376 count := 0
377377 for err == nil {
378378 var c *http.Cookie
379− c, err = req.Cookie(fmt.Sprintf("%s-%d", cookieName, count))
379+ c, err = req.Cookie(fmt.Sprintf("%s_%d", cookieName, count))
380380 if err == nil {
381381 cookies = append(cookies, c)
382382 count++