1
0
mirror of https://github.com/oauth2-proxy/oauth2-proxy.git synced 2025-06-02 23:27:22 +02:00

Improve error message when no cookie is found

This commit is contained in:
Joel Speed 2021-10-13 18:48:09 +01:00
parent 6cc7da8993
commit d8deaa124b
No known key found for this signature in database
GPG Key ID: 6E80578D6751DEFB
5 changed files with 34 additions and 14 deletions

View File

@ -18,6 +18,7 @@
## Changes since v7.1.3 ## Changes since v7.1.3
- [#1404](https://github.com/oauth2-proxy/oauth2-proxy/pull/1404) Improve error message when no cookie is found (@JoelSpeed)
- [#1315](https://github.com/oauth2-proxy/oauth2-proxy/pull/1315) linkedin: Update provider to v2 (@wuurrd) - [#1315](https://github.com/oauth2-proxy/oauth2-proxy/pull/1315) linkedin: Update provider to v2 (@wuurrd)
- [#1348](https://github.com/oauth2-proxy/oauth2-proxy/pull/1348) Using the native httputil proxy code for websockets rather than yhat/wsutil to properly handle HTTP-level failures (@thetrime) - [#1348](https://github.com/oauth2-proxy/oauth2-proxy/pull/1348) Using the native httputil proxy code for websockets rather than yhat/wsutil to properly handle HTTP-level failures (@thetrime)
- [#1379](https://github.com/oauth2-proxy/oauth2-proxy/pull/1379) Fix the manual sign in with --htpasswd-user-group switch (@janrotter) - [#1379](https://github.com/oauth2-proxy/oauth2-proxy/pull/1379) Fix the manual sign in with --htpasswd-user-group switch (@janrotter)

View File

@ -853,11 +853,13 @@ func (p *OAuthProxy) Proxy(rw http.ResponseWriter, req *http.Request) {
case ErrNeedsLogin: case ErrNeedsLogin:
// we need to send the user to a login screen // we need to send the user to a login screen
if p.forceJSONErrors || isAjax(req) { if p.forceJSONErrors || isAjax(req) {
logger.Printf("No valid authentication in request. Access Denied.")
// no point redirecting an AJAX request // no point redirecting an AJAX request
p.errorJSON(rw, http.StatusUnauthorized) p.errorJSON(rw, http.StatusUnauthorized)
return return
} }
logger.Printf("No valid authentication in request. Initiating login.")
if p.SkipProviderButton { if p.SkipProviderButton {
p.OAuthStart(rw, req) p.OAuthStart(rw, req)
} else { } else {

View File

@ -71,7 +71,7 @@ func (s *storedSessionLoader) loadSession(next http.Handler) http.Handler {
} }
session, err := s.getValidatedSession(rw, req) session, err := s.getValidatedSession(rw, req)
if err != nil { if err != nil && !errors.Is(err, http.ErrNoCookie) {
// In the case when there was an error loading the session, // In the case when there was an error loading the session,
// we should clear the session // we should clear the session
logger.Errorf("Error loading cookied session: %v, removing session", err) logger.Errorf("Error loading cookied session: %v, removing session", err)

View File

@ -51,7 +51,7 @@ func (s *SessionStore) Load(req *http.Request) (*sessions.SessionState, error) {
c, err := loadCookie(req, s.Cookie.Name) c, err := loadCookie(req, s.Cookie.Name)
if err != nil { if err != nil {
// always http.ErrNoCookie // always http.ErrNoCookie
return nil, fmt.Errorf("cookie %q not present", s.Cookie.Name) return nil, err
} }
val, _, ok := encryption.Validate(c, s.Cookie.Secret, s.Cookie.Expire) val, _, ok := encryption.Validate(c, s.Cookie.Secret, s.Cookie.Expire)
if !ok { if !ok {
@ -216,7 +216,7 @@ func loadCookie(req *http.Request, cookieName string) (*http.Cookie, error) {
} }
} }
if len(cookies) == 0 { if len(cookies) == 0 {
return nil, fmt.Errorf("could not find cookie %s", cookieName) return nil, http.ErrNoCookie
} }
return joinCookies(cookies, cookieName) return joinCookies(cookies, cookieName)
} }

View File

@ -452,12 +452,12 @@ func SessionStoreInterfaceTests(in *testInput) {
}) })
Context("when Load is called", func() { Context("when Load is called", func() {
Context("with a valid session cookie in the request", func() {
BeforeEach(func() { BeforeEach(func() {
req := httptest.NewRequest("GET", "http://example.com/", nil) req := httptest.NewRequest("GET", "http://example.com/", nil)
resp := httptest.NewRecorder() resp := httptest.NewRecorder()
err := in.ss().Save(resp, req, in.session) err := in.ss().Save(resp, req, in.session)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
for _, cookie := range resp.Result().Cookies() { for _, cookie := range resp.Result().Cookies() {
in.request.AddCookie(cookie) in.request.AddCookie(cookie)
} }
@ -466,7 +466,24 @@ func SessionStoreInterfaceTests(in *testInput) {
Context("before the refresh period", func() { Context("before the refresh period", func() {
LoadSessionTests(in) LoadSessionTests(in)
}) })
})
Context("with no cookies in the request", func() {
var loadedSession *sessionsapi.SessionState
var loadErr error
BeforeEach(func() {
loadedSession, loadErr = in.ss().Load(in.request)
})
It("returns an empty session", func() {
Expect(loadedSession).To(BeNil())
})
It("should return a no cookie error", func() {
Expect(loadErr).To(MatchError(http.ErrNoCookie))
})
})
}) })
} }