1
0
Fork 0

Factor out credential check (#624)

master
Philip O'Toole 5 years ago committed by GitHub
parent bbf85a8ef3
commit cb0f88a022
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -226,13 +226,9 @@ func (s *Service) Close() {
// ServeHTTP allows Service to serve HTTP requests.
func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.addBuildVersion(w)
if s.credentialStore != nil {
username, password, ok := r.BasicAuth()
if !ok || !s.credentialStore.Check(username, password) {
w.WriteHeader(http.StatusUnauthorized)
return
}
if !s.checkCredentials(r) {
w.WriteHeader(http.StatusUnauthorized)
return
}
switch {
@ -781,6 +777,17 @@ func (s *Service) addBuildVersion(w http.ResponseWriter) {
w.Header().Add(VersionHTTPHeader, version)
}
// checkCredentials returns if any authentication requirements
// have been successfully met.
func (s *Service) checkCredentials(r *http.Request) bool {
if s.credentialStore == nil {
return true
}
username, password, ok := r.BasicAuth()
return ok && s.credentialStore.Check(username, password)
}
func requestQueries(r *http.Request) ([]string, error) {
if r.Method == "GET" {
query, err := stmtParam(r)

Loading…
Cancel
Save