mirror of
https://github.com/oauth2-proxy/oauth2-proxy.git
synced 2026-10-21 10:17:22 +02:00
feat: support for cookie in alpha config and legacy file structure refactoring
Signed-off-by: Jan Larwig <jan@larwig.com>
This commit is contained in:
@@ -76,17 +76,11 @@ func (s *SessionStore) Clear(rw http.ResponseWriter, req *http.Request) error {
|
||||
|
||||
for _, c := range req.Cookies() {
|
||||
if cookieNameRegex.MatchString(c.Name) {
|
||||
sessionCookieOptions := &pkgcookies.CookieOptions{
|
||||
Name: c.Name,
|
||||
Value: "",
|
||||
Domains: s.Cookie.Domains,
|
||||
Expiration: time.Hour * -1,
|
||||
SameSite: s.Cookie.SameSite,
|
||||
Path: s.Cookie.Path,
|
||||
HTTPOnly: s.Cookie.HTTPOnly,
|
||||
Secure: s.Cookie.Secure,
|
||||
}
|
||||
clearCookie := pkgcookies.MakeCookieFromOptions(req, sessionCookieOptions)
|
||||
sessionCookieOptions := *s.Cookie
|
||||
sessionCookieOptions.Name = c.Name
|
||||
sessionCookieOptions.Expire = time.Hour * -1
|
||||
|
||||
clearCookie := pkgcookies.MakeCookieFromOptions(req, "", &sessionCookieOptions)
|
||||
|
||||
http.SetCookie(rw, clearCookie)
|
||||
}
|
||||
@@ -142,17 +136,7 @@ func (s *SessionStore) makeSessionCookie(req *http.Request, value []byte, now ti
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
sessionCookieOptions := &pkgcookies.CookieOptions{
|
||||
Name: s.Cookie.Name,
|
||||
Value: strValue,
|
||||
Domains: s.Cookie.Domains,
|
||||
Expiration: s.Cookie.Expire,
|
||||
SameSite: s.Cookie.SameSite,
|
||||
Path: s.Cookie.Path,
|
||||
HTTPOnly: s.Cookie.HTTPOnly,
|
||||
Secure: s.Cookie.Secure,
|
||||
}
|
||||
c := pkgcookies.MakeCookieFromOptions(req, sessionCookieOptions)
|
||||
c := pkgcookies.MakeCookieFromOptions(req, strValue, s.Cookie)
|
||||
if len(c.String()) > maxCookieLength {
|
||||
return splitCookie(c), nil
|
||||
}
|
||||
|
||||
@@ -221,17 +221,10 @@ func (t *ticket) setCookie(rw http.ResponseWriter, req *http.Request, s *session
|
||||
// clearCookie removes any cookies that would be where this ticket
|
||||
// would set them
|
||||
func (t *ticket) clearCookie(rw http.ResponseWriter, req *http.Request) {
|
||||
cookieOptions := &cookies.CookieOptions{
|
||||
Name: t.options.Name,
|
||||
Value: "",
|
||||
Domains: t.options.Domains,
|
||||
Expiration: time.Hour * -1,
|
||||
SameSite: t.options.SameSite,
|
||||
Path: t.options.Path,
|
||||
HTTPOnly: t.options.HTTPOnly,
|
||||
Secure: t.options.Secure,
|
||||
}
|
||||
http.SetCookie(rw, cookies.MakeCookieFromOptions(req, cookieOptions))
|
||||
cookieOpt := *t.options
|
||||
cookieOpt.Expire = time.Hour * -1
|
||||
|
||||
http.SetCookie(rw, cookies.MakeCookieFromOptions(req, "", &cookieOpt))
|
||||
}
|
||||
|
||||
// makeCookie makes a cookie, signing the value if present
|
||||
@@ -248,18 +241,10 @@ func (t *ticket) makeCookie(req *http.Request, value string, expires time.Durati
|
||||
}
|
||||
}
|
||||
|
||||
cookieOptions := &cookies.CookieOptions{
|
||||
Name: t.options.Name,
|
||||
Value: value,
|
||||
Domains: t.options.Domains,
|
||||
Expiration: expires,
|
||||
SameSite: t.options.SameSite,
|
||||
Path: t.options.Path,
|
||||
HTTPOnly: t.options.HTTPOnly,
|
||||
Secure: t.options.Secure,
|
||||
}
|
||||
cookieOpt := *t.options
|
||||
cookieOpt.Expire = expires
|
||||
|
||||
return cookies.MakeCookieFromOptions(req, cookieOptions), nil
|
||||
return cookies.MakeCookieFromOptions(req, value, &cookieOpt), nil
|
||||
}
|
||||
|
||||
// makeCipher makes a AES-GCM cipher out of the ticket's secret
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
sessionscookie "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/sessions/cookie"
|
||||
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/sessions/persistence"
|
||||
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/sessions/redis"
|
||||
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/util/ptr"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
@@ -44,8 +45,8 @@ var _ = Describe("NewSessionStore", func() {
|
||||
Path: "/",
|
||||
Expire: time.Duration(168) * time.Hour,
|
||||
Refresh: time.Duration(1) * time.Hour,
|
||||
Secure: true,
|
||||
HTTPOnly: true,
|
||||
Secure: ptr.To(true),
|
||||
HTTPOnly: ptr.To(true),
|
||||
SameSite: "",
|
||||
}
|
||||
})
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
sessionsapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions"
|
||||
cookiesapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/cookies"
|
||||
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/encryption"
|
||||
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/util/ptr"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
@@ -68,8 +69,8 @@ func RunSessionStoreTests(newSS NewSessionStoreFunc, persistentFastForward Persi
|
||||
Path: "/",
|
||||
Expire: time.Duration(168) * time.Hour,
|
||||
Refresh: time.Duration(1) * time.Hour,
|
||||
Secure: true,
|
||||
HTTPOnly: true,
|
||||
Secure: ptr.To(true),
|
||||
HTTPOnly: ptr.To(true),
|
||||
SameSite: "",
|
||||
Secret: string(cookieSecret),
|
||||
}
|
||||
@@ -117,8 +118,8 @@ func RunSessionStoreTests(newSS NewSessionStoreFunc, persistentFastForward Persi
|
||||
Path: "/path",
|
||||
Expire: time.Duration(72) * time.Hour,
|
||||
Refresh: time.Duration(2) * time.Hour,
|
||||
Secure: false,
|
||||
HTTPOnly: false,
|
||||
Secure: ptr.To(false),
|
||||
HTTPOnly: ptr.To(false),
|
||||
Domains: []string{"example.com"},
|
||||
SameSite: "strict",
|
||||
Secret: string(cookieSecret),
|
||||
@@ -149,8 +150,8 @@ func RunSessionStoreTests(newSS NewSessionStoreFunc, persistentFastForward Persi
|
||||
Path: "/",
|
||||
Expire: time.Duration(168) * time.Hour,
|
||||
Refresh: time.Duration(1) * time.Hour,
|
||||
Secure: true,
|
||||
HTTPOnly: true,
|
||||
Secure: ptr.To(true),
|
||||
HTTPOnly: ptr.To(true),
|
||||
SameSite: "",
|
||||
Secret: "",
|
||||
SecretFile: tmpfile.Name(),
|
||||
@@ -208,13 +209,13 @@ func CheckCookieOptions(in *testInput) {
|
||||
|
||||
It("have the correct HTTPOnly set", func() {
|
||||
for _, cookie := range cookies {
|
||||
Expect(cookie.HttpOnly).To(Equal(in.cookieOpts.HTTPOnly))
|
||||
Expect(cookie.HttpOnly).To(Equal(*in.cookieOpts.HTTPOnly))
|
||||
}
|
||||
})
|
||||
|
||||
It("have the correct secure set", func() {
|
||||
for _, cookie := range cookies {
|
||||
Expect(cookie.Secure).To(Equal(in.cookieOpts.Secure))
|
||||
Expect(cookie.Secure).To(Equal(*in.cookieOpts.Secure))
|
||||
}
|
||||
})
|
||||
|
||||
@@ -422,17 +423,7 @@ func SessionStoreInterfaceTests(in *testInput) {
|
||||
broken := "BrokenSessionFromADifferentSessionImplementation"
|
||||
value, err := encryption.SignedValue(in.cookieOpts.Secret, in.cookieOpts.Name, []byte(broken), time.Now())
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
cookieOptions := &cookiesapi.CookieOptions{
|
||||
Name: in.cookieOpts.Name,
|
||||
Value: value,
|
||||
Domains: in.cookieOpts.Domains,
|
||||
Expiration: in.cookieOpts.Expire,
|
||||
SameSite: in.cookieOpts.SameSite,
|
||||
Path: in.cookieOpts.Path,
|
||||
HTTPOnly: in.cookieOpts.HTTPOnly,
|
||||
Secure: in.cookieOpts.Secure,
|
||||
}
|
||||
cookie := cookiesapi.MakeCookieFromOptions(in.request, cookieOptions)
|
||||
cookie := cookiesapi.MakeCookieFromOptions(in.request, value, in.cookieOpts)
|
||||
in.request.AddCookie(cookie)
|
||||
|
||||
err = in.ss().Save(in.response, in.request, in.session)
|
||||
|
||||
Reference in New Issue
Block a user