git.iko.soy/typeable/oauth2-proxy
on master help sign in
Improve error message when no cookie is found
d8deaa1Joel Speed
CHANGELOG.md+1
@@ -18,6 +18,7 @@
18
18
19## Changes since v7.1.3
19## Changes since v7.1.3
20
20
21+- [#1404](https://github.com/oauth2-proxy/oauth2-proxy/pull/1404) Improve error message when no cookie is found (@JoelSpeed)
21- [#1315](https://github.com/oauth2-proxy/oauth2-proxy/pull/1315) linkedin: Update provider to v2 (@wuurrd)
22- [#1315](https://github.com/oauth2-proxy/oauth2-proxy/pull/1315) linkedin: Update provider to v2 (@wuurrd)
22- [#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)
23- [#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)
23- [#1379](https://github.com/oauth2-proxy/oauth2-proxy/pull/1379) Fix the manual sign in with --htpasswd-user-group switch (@janrotter)
24- [#1379](https://github.com/oauth2-proxy/oauth2-proxy/pull/1379) Fix the manual sign in with --htpasswd-user-group switch (@janrotter)
oauthproxy.go+2
@@ -853,11 +853,13 @@ func (p *OAuthProxy) Proxy(rw http.ResponseWriter, req *http.Request) {
853 case ErrNeedsLogin:
853 case ErrNeedsLogin:
854 // we need to send the user to a login screen
854 // we need to send the user to a login screen
855 if p.forceJSONErrors || isAjax(req) {
855 if p.forceJSONErrors || isAjax(req) {
856+ logger.Printf("No valid authentication in request. Access Denied.")
856 // no point redirecting an AJAX request
857 // no point redirecting an AJAX request
857 p.errorJSON(rw, http.StatusUnauthorized)
858 p.errorJSON(rw, http.StatusUnauthorized)
858 return
859 return
859 }
860 }
860
861
862+ logger.Printf("No valid authentication in request. Initiating login.")
861 if p.SkipProviderButton {
863 if p.SkipProviderButton {
862 p.OAuthStart(rw, req)
864 p.OAuthStart(rw, req)
863 } else {
865 } else {
pkg/middleware/stored_session.go+1−1
@@ -71,7 +71,7 @@ func (s *storedSessionLoader) loadSession(next http.Handler) http.Handler {
71 }
71 }
72
72
73 session, err := s.getValidatedSession(rw, req)
73 session, err := s.getValidatedSession(rw, req)
74− if err != nil {
74+ if err != nil && !errors.Is(err, http.ErrNoCookie) {
75 // In the case when there was an error loading the session,
75 // In the case when there was an error loading the session,
76 // we should clear the session
76 // we should clear the session
77 logger.Errorf("Error loading cookied session: %v, removing session", err)
77 logger.Errorf("Error loading cookied session: %v, removing session", err)
pkg/sessions/cookie/session_store.go+2−2
@@ -51,7 +51,7 @@ func (s *SessionStore) Load(req *http.Request) (*sessions.SessionState, error) {
51 c, err := loadCookie(req, s.Cookie.Name)
51 c, err := loadCookie(req, s.Cookie.Name)
52 if err != nil {
52 if err != nil {
53 // always http.ErrNoCookie
53 // always http.ErrNoCookie
54− return nil, fmt.Errorf("cookie %q not present", s.Cookie.Name)
54+ return nil, err
55 }
55 }
56 val, _, ok := encryption.Validate(c, s.Cookie.Secret, s.Cookie.Expire)
56 val, _, ok := encryption.Validate(c, s.Cookie.Secret, s.Cookie.Expire)
57 if !ok {
57 if !ok {
@@ -216,7 +216,7 @@ func loadCookie(req *http.Request, cookieName string) (*http.Cookie, error) {
216 }
216 }
217 }
217 }
218 if len(cookies) == 0 {
218 if len(cookies) == 0 {
219− return nil, fmt.Errorf("could not find cookie %s", cookieName)
219+ return nil, http.ErrNoCookie
220 }
220 }
221 return joinCookies(cookies, cookieName)
221 return joinCookies(cookies, cookieName)
222}
222}
pkg/sessions/tests/session_store_tests.go+28−11
@@ -452,21 +452,38 @@ func SessionStoreInterfaceTests(in *testInput) {
452 })
452 })
453
453
454 Context("when Load is called", func() {
454 Context("when Load is called", func() {
455+ Context("with a valid session cookie in the request", func() {
455− BeforeEach(func() {
456+ BeforeEach(func() {
456− req := httptest.NewRequest("GET", "http://example.com/", nil)
457+ req := httptest.NewRequest("GET", "http://example.com/", nil)
457− resp := httptest.NewRecorder()
458+ resp := httptest.NewRecorder()
458− err := in.ss().Save(resp, req, in.session)
459+ err := in.ss().Save(resp, req, in.session)
459− Expect(err).ToNot(HaveOccurred())
460+ Expect(err).ToNot(HaveOccurred())
461+ for _, cookie := range resp.Result().Cookies() {
462+ in.request.AddCookie(cookie)
463+ }
464+ })
460
465
461− for _, cookie := range resp.Result().Cookies() {
466+ Context("before the refresh period", func() {
462− in.request.AddCookie(cookie)
467+ LoadSessionTests(in)
463− }
468+ })
464 })
469 })
465
470
466− Context("before the refresh period", func() {
471+ Context("with no cookies in the request", func() {
467− LoadSessionTests(in)
472+ var loadedSession *sessionsapi.SessionState
473+ var loadErr error
474+
475+ BeforeEach(func() {
476+ loadedSession, loadErr = in.ss().Load(in.request)
468− })
477+ })
469
478
479+ It("returns an empty session", func() {
480+ Expect(loadedSession).To(BeNil())
481+ })
482+
483+ It("should return a no cookie error", func() {
484+ Expect(loadErr).To(MatchError(http.ErrNoCookie))
485+ })
486+ })
470 })
487 })
471}
488}
472
489