github.com/rpdict/ponzu@v0.10.1-0.20190226054626-477f29d6bf5e/system/system.go (about)

     1  // Package system contains a collection of packages that make up the internal
     2  // Ponzu system, which handles addons, administration, the Admin server, the API
     3  // server, analytics, databases, search, TLS, and various internal types.
     4  package system
     5  
     6  import (
     7  	"net/http"
     8  
     9  	"github.com/rpdict/ponzu/system/db"
    10  )
    11  
    12  // BasicAuth adds HTTP Basic Auth check for requests that should implement it
    13  func BasicAuth(next http.HandlerFunc) http.HandlerFunc {
    14  	return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
    15  		u := db.ConfigCache("backup_basic_auth_user").(string)
    16  		p := db.ConfigCache("backup_basic_auth_password").(string)
    17  
    18  		if u == "" || p == "" {
    19  			res.WriteHeader(http.StatusForbidden)
    20  			return
    21  		}
    22  
    23  		user, password, ok := req.BasicAuth()
    24  
    25  		if !ok {
    26  			res.WriteHeader(http.StatusForbidden)
    27  			return
    28  		}
    29  
    30  		if u != user || p != password {
    31  			res.WriteHeader(http.StatusUnauthorized)
    32  			return
    33  		}
    34  
    35  		next.ServeHTTP(res, req)
    36  	})
    37  }