git.iko.soy/typeable/oauth2-proxy
on master help sign in
Returns HTTP unauthorized for ajax requests instead of redirecting to the sing-in page
commitc12db0ebf797e3489c1bef93878ae1bdb6850125 authorCosmin Cojocar <cosmin.cojocar@gmx.ch> dateWed 30 Jan 2019, 11:13 (8 years ago) parent01c5f5a
2 files changed+92
oauthproxy.go+28 oauthproxy_test.go+64
oauthproxy.go+28
@@ -754,6 +754,8 @@ func (p *OAuthProxy) Proxy(rw http.ResponseWriter, req *http.Request) {
754754 } else {
755755 p.SignInPage(rw, req, http.StatusForbidden)
756756 }
757+ } else if status == http.StatusUnauthorized {
758+ p.ErrorJSON(rw, status)
757759 } else {
758760 p.serveMux.ServeHTTP(rw, req)
759761 }
@@ -826,6 +828,11 @@ func (p *OAuthProxy) Authenticate(rw http.ResponseWriter, req *http.Request) int
826828 }
827829
828830 if session == nil {
831+ // Check if is an ajax request and return unauthorized to avoid a redirect
832+ // to the login page
833+ if p.isAjax(req) {
834+ return http.StatusUnauthorized
835+ }
829836 return http.StatusForbidden
830837 }
831838
@@ -894,3 +901,24 @@ func (p *OAuthProxy) CheckBasicAuth(req *http.Request) (*providers.SessionState,
894901 }
895902 return nil, fmt.Errorf("%s not in HtpasswdFile", pair[0])
896903}
904+
905+// isAjax checks if a request is an ajax request
906+func (p *OAuthProxy) isAjax(req *http.Request) bool {
907+ acceptValues, ok := req.Header["accept"]
908+ if !ok {
909+ acceptValues = req.Header["Accept"]
910+ }
911+ const ajaxReq = "application/json"
912+ for _, v := range acceptValues {
913+ if v == ajaxReq {
914+ return true
915+ }
916+ }
917+ return false
918+}
919+
920+// ErrorJSON returns the error code witht an application/json mime type
921+func (p *OAuthProxy) ErrorJSON(rw http.ResponseWriter, code int) {
922+ rw.Header().Set("Content-Type", "application/json")
923+ rw.WriteHeader(code)
924+}
oauthproxy_test.go+64
@@ -871,3 +871,67 @@ func TestGetRedirect(t *testing.T) {
871871 })
872872 }
873873}
874+
875+type ajaxRequestTest struct {
876+ opts *Options
877+ proxy *OAuthProxy
878+}
879+
880+func newAjaxRequestTest() *ajaxRequestTest {
881+ test := &ajaxRequestTest{}
882+ test.opts = NewOptions()
883+ test.opts.CookieSecret = "foobar"
884+ test.opts.ClientID = "bazquux"
885+ test.opts.ClientSecret = "xyzzyplugh"
886+ test.opts.Validate()
887+ test.proxy = NewOAuthProxy(test.opts, func(email string) bool {
888+ return true
889+ })
890+ return test
891+}
892+
893+func (test *ajaxRequestTest) getEndpoint(endpoint string, header http.Header) (int, http.Header, error) {
894+ rw := httptest.NewRecorder()
895+ req, err := http.NewRequest(http.MethodGet, endpoint, strings.NewReader(""))
896+ if err != nil {
897+ return 0, nil, err
898+ }
899+ req.Header = header
900+ test.proxy.ServeHTTP(rw, req)
901+ return rw.Code, rw.Header(), nil
902+}
903+
904+func testAjaxUnauthorizedRequest(t *testing.T, header http.Header) {
905+ test := newAjaxRequestTest()
906+ const endpoint = "/test"
907+
908+ code, rh, err := test.getEndpoint(endpoint, header)
909+ assert.NoError(t, err)
910+ assert.Equal(t, http.StatusUnauthorized, code)
911+ mime := rh.Get("Content-Type")
912+ assert.Equal(t, "application/json", mime)
913+}
914+func TestAjaxUnauthorizedRequest1(t *testing.T) {
915+ header := make(http.Header)
916+ header.Add("accept", "application/json")
917+
918+ testAjaxUnauthorizedRequest(t, header)
919+}
920+
921+func TestAjaxUnauthorizedRequest2(t *testing.T) {
922+ header := make(http.Header)
923+ header.Add("Accept", "application/json")
924+
925+ testAjaxUnauthorizedRequest(t, header)
926+}
927+
928+func TestAjaxForbiddendRequest(t *testing.T) {
929+ test := newAjaxRequestTest()
930+ const endpoint = "/test"
931+ header := make(http.Header)
932+ code, rh, err := test.getEndpoint(endpoint, header)
933+ assert.NoError(t, err)
934+ assert.Equal(t, http.StatusForbidden, code)
935+ mime := rh.Get("Content-Type")
936+ assert.NotEqual(t, "application/json", mime)
937+}