HTTP proxy written in Go to replace Tinyproxy (#269)
This commit is contained in:
33
internal/httpproxy/auth.go
Normal file
33
internal/httpproxy/auth.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package httpproxy
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func isAuthorized(responseWriter http.ResponseWriter, request *http.Request,
|
||||
username, password string) (authorized bool) {
|
||||
basicAuth := request.Header.Get("Proxy-Authorization")
|
||||
if len(basicAuth) == 0 {
|
||||
responseWriter.WriteHeader(http.StatusProxyAuthRequired)
|
||||
return false
|
||||
}
|
||||
b64UsernamePassword := strings.TrimPrefix(basicAuth, "Basic ")
|
||||
b, err := base64.StdEncoding.DecodeString(b64UsernamePassword)
|
||||
if err != nil {
|
||||
responseWriter.WriteHeader(http.StatusUnauthorized)
|
||||
return false
|
||||
}
|
||||
usernamePassword := strings.Split(string(b), ":")
|
||||
const expectedFields = 2
|
||||
if len(usernamePassword) != expectedFields {
|
||||
responseWriter.WriteHeader(http.StatusBadRequest)
|
||||
return false
|
||||
}
|
||||
if username != usernamePassword[0] && password != usernamePassword[1] {
|
||||
responseWriter.WriteHeader(http.StatusUnauthorized)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user