1
0
mirror of https://github.com/oauth2-proxy/oauth2-proxy.git synced 2025-05-31 23:19:50 +02:00

Merge pull request #1404 from oauth2-proxy/improve-no-auth-error

Improve error message when no cookie is found
This commit is contained in:
Joel Speed 2021-10-18 18:16:40 +01:00 committed by GitHub
commit 9d8093f470
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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,21 +452,38 @@ func SessionStoreInterfaceTests(in *testInput) {
}) })
Context("when Load is called", func() { Context("when Load is called", func() {
BeforeEach(func() { Context("with a valid session cookie in the request", func() {
req := httptest.NewRequest("GET", "http://example.com/", nil) BeforeEach(func() {
resp := httptest.NewRecorder() req := httptest.NewRequest("GET", "http://example.com/", nil)
err := in.ss().Save(resp, req, in.session) resp := httptest.NewRecorder()
Expect(err).ToNot(HaveOccurred()) err := in.ss().Save(resp, req, in.session)
Expect(err).ToNot(HaveOccurred())
for _, cookie := range resp.Result().Cookies() {
in.request.AddCookie(cookie)
}
})
for _, cookie := range resp.Result().Cookies() { Context("before the refresh period", func() {
in.request.AddCookie(cookie) LoadSessionTests(in)
} })
}) })
Context("before the refresh period", func() { Context("with no cookies in the request", func() {
LoadSessionTests(in) 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))
})
})
}) })
} }