looks fine to me

This commit is contained in:
Bel LaPointe
2019-02-23 12:53:52 -07:00
parent 072b1b41d0
commit 0312681d4b
4 changed files with 97 additions and 1 deletions

View File

@@ -1,6 +1,7 @@
package server
import (
"encoding/base64"
"errors"
"local/rproxy3/config"
"local/rproxy3/storage"
@@ -8,6 +9,7 @@ import (
"log"
"net/http"
"net/url"
"strings"
)
const nsRouting = "routing"
@@ -65,10 +67,11 @@ func (s *Server) doAuth(foo http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
rusr, rpwd, ok := config.GetAuth()
if ok {
//usr, pwd := getProxyAuth(r)
usr, pwd, ok := r.BasicAuth()
if !ok || rusr != usr || rpwd != pwd {
w.WriteHeader(http.StatusUnauthorized)
log.Printf("denying basic auth")
log.Printf("denying proxy basic auth")
return
}
}
@@ -83,3 +86,15 @@ func (s *Server) Pre(foo http.HandlerFunc) http.HandlerFunc {
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.Pre(s.Proxy)(w, r)
}
func getProxyAuth(r *http.Request) (string, string) {
proxyAuthHeader := r.Header.Get("Proxy-Authorization")
proxyAuthB64 := strings.TrimPrefix(proxyAuthHeader, "Basic ")
proxyAuthBytes, _ := base64.StdEncoding.DecodeString(proxyAuthB64)
proxyAuth := string(proxyAuthBytes)
if !strings.Contains(proxyAuth, ":") {
return "", ""
}
proxyAuthSplit := strings.Split(proxyAuth, ":")
return proxyAuthSplit[0], proxyAuthSplit[1]
}