@@ -106,6 +106,7 @@ func (a *Auth) basicAuth(h http.HandlerFunc) http.HandlerFunc {
106106 } else {
107107 msg = fmt .Sprintf ("Unable to parse JWT: %v" , err )
108108 }
109+ w .Header ().Set ("www-authenticate" , "Basic realm=\" Authentication Required\" " )
109110 util .WriteBackError (w , msg , http .StatusUnauthorized )
110111 return
111112 }
@@ -118,10 +119,12 @@ func (a *Auth) basicAuth(h http.HandlerFunc) http.HandlerFunc {
118119 } else if u , ok := claims ["role" ]; ok {
119120 role = u .(string )
120121 } else {
122+ w .Header ().Set ("www-authenticate" , "Basic realm=\" Authentication Required\" " )
121123 util .WriteBackError (w , fmt .Sprintf ("Invalid JWT" ), http .StatusUnauthorized )
122124 return
123125 }
124126 } else {
127+ w .Header ().Set ("www-authenticate" , "Basic realm=\" Authentication Required\" " )
125128 util .WriteBackError (w , fmt .Sprintf ("Invalid JWT" ), http .StatusUnauthorized )
126129 return
127130 }
@@ -133,6 +136,7 @@ func (a *Auth) basicAuth(h http.HandlerFunc) http.HandlerFunc {
133136 if err != nil || obj == nil {
134137 msg := fmt .Sprintf ("No API credentials match with provided role: %s" , role )
135138 log .Errorln (logTag , ":" , err )
139+ w .Header ().Set ("www-authenticate" , "Basic realm=\" Authentication Required\" " )
136140 util .WriteBackError (w , msg , http .StatusUnauthorized )
137141 return
138142 }
@@ -141,12 +145,14 @@ func (a *Auth) basicAuth(h http.HandlerFunc) http.HandlerFunc {
141145 if err != nil || obj == nil {
142146 msg := fmt .Sprintf ("No API credentials match with provided username: %s" , username )
143147 log .Errorln (logTag , ":" , err )
148+ w .Header ().Set ("www-authenticate" , "Basic realm=\" Authentication Required\" " )
144149 util .WriteBackError (w , msg , http .StatusUnauthorized )
145150 return
146151 }
147152 }
148153
149154 var authenticated bool
155+ var errorMsg = "invalid credentials provided"
150156
151157 // since we are able to fetch a result with the given credentials, we
152158 // do not need to validate the username and password.
@@ -156,6 +162,7 @@ func (a *Auth) basicAuth(h http.HandlerFunc) http.HandlerFunc {
156162 // if the request is made to elasticsearch using user credentials, then the user has to be an admin
157163 reqUser := obj .(* user.User )
158164 if hasBasicAuth && bcrypt .CompareHashAndPassword ([]byte (reqUser .Password ), []byte (password )) != nil {
165+ w .Header ().Set ("www-authenticate" , "Basic realm=\" Authentication Required\" " )
159166 util .WriteBackError (w , "invalid password" , http .StatusUnauthorized )
160167 return
161168 }
@@ -165,6 +172,10 @@ func (a *Auth) basicAuth(h http.HandlerFunc) http.HandlerFunc {
165172 authenticated = true
166173 }
167174
175+ if ! authenticated {
176+ errorMsg = "only admin users are allowed to access elasticsearch"
177+ }
178+
168179 // cache the user
169180 if _ , ok := a .cachedCredential (username ); ! ok {
170181 a .cacheCredential (username , reqUser )
@@ -179,12 +190,15 @@ func (a *Auth) basicAuth(h http.HandlerFunc) http.HandlerFunc {
179190 {
180191 reqPermission := obj .(* permission.Permission )
181192 if hasBasicAuth && reqPermission .Password != password {
193+ w .Header ().Set ("www-authenticate" , "Basic realm=\" Authentication Required\" " )
182194 util .WriteBackError (w , "invalid password" , http .StatusUnauthorized )
183195 return
184196 }
185197
186198 if reqCategory .IsFromES () {
187199 authenticated = true
200+ } else {
201+ errorMsg = "credential is only allowed to access elasticsearch"
188202 }
189203
190204 // cache the permission
@@ -202,7 +216,8 @@ func (a *Auth) basicAuth(h http.HandlerFunc) http.HandlerFunc {
202216 }
203217
204218 if ! authenticated {
205- util .WriteBackError (w , "invalid credentials provided" , http .StatusUnauthorized )
219+ w .Header ().Set ("www-authenticate" , "Basic realm=\" Authentication Required\" " )
220+ util .WriteBackError (w , errorMsg , http .StatusUnauthorized )
206221 return
207222 }
208223
0 commit comments