very basic basic auth (#12)

This commit is contained in:
Michael Quigley 2022-08-16 11:27:31 -04:00
parent ab87c00106
commit b510190910
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
2 changed files with 47 additions and 1 deletions

View File

@ -12,3 +12,12 @@ const (
type ProxyConfig struct {
AuthScheme AuthScheme `json:"auth_scheme"`
}
type BasicAuth struct {
Users []*AuthUser
}
type AuthUser struct {
Username string
Password string
}

View File

@ -2,6 +2,7 @@ package proxy
import (
"context"
"crypto/subtle"
"fmt"
"github.com/openziti-test-kitchen/zrok/model"
"github.com/openziti-test-kitchen/zrok/util"
@ -38,7 +39,12 @@ func Run(cfg *Config) error {
return err
}
proxy.Transport = zTransport
return http.ListenAndServe(cfg.Address, util.NewProxyHandler(proxy))
users := &model.BasicAuth{
Users: []*model.AuthUser{
{Username: "hello", Password: "world"},
},
}
return http.ListenAndServe(cfg.Address, basicAuth(util.NewProxyHandler(proxy), users, "zrok"))
}
type resolver struct{}
@ -158,3 +164,34 @@ func getRefreshedService(name string, ctx ziti.Context) (*edge.Service, bool) {
}
return svc, found
}
func basicAuth(handler http.Handler, users *model.BasicAuth, realm string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
inUser, inPass, ok := r.BasicAuth()
if !ok {
writeUnauthorizedResponse(w, realm)
return
}
authed := false
for _, v := range users.Users {
if subtle.ConstantTimeCompare([]byte(inUser), []byte(v.Username)) == 1 && subtle.ConstantTimeCompare([]byte(inPass), []byte(v.Password)) == 1 {
authed = true
break
}
}
if !authed {
writeUnauthorizedResponse(w, realm)
return
}
handler.ServeHTTP(w, r)
}
}
func writeUnauthorizedResponse(w http.ResponseWriter, realm string) {
w.Header().Set("WWW-Authenticate", `Basic realm="`+realm+`"`)
w.WriteHeader(401)
w.Write([]byte("No Authorization\n"))
}