From dc18215f3843adbc4339a728fd91b91048d35ea7 Mon Sep 17 00:00:00 2001 From: Manuel Bovo Date: Sun, 1 Oct 2017 13:28:02 +0200 Subject: [PATCH] Removing external lib for parsing env --- api/basicauth.go | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/api/basicauth.go b/api/basicauth.go index d94e1e1..af10085 100644 --- a/api/basicauth.go +++ b/api/basicauth.go @@ -2,14 +2,17 @@ package api import ( "github.com/Sirupsen/logrus" - "github.com/kelseyhightower/envconfig" + // "github.com/kelseyhightower/envconfig" "net/http" + "os" + "strconv" + "strings" ) type AuthConfig struct { - AuthEnabled bool `split_words:"true" default:"false"` - AuthRealm string `split_words:"true" default:"Restricted"` - AuthCredential map[string]string `split_words:"true" default:"orbiter:orbiter"` + AuthEnabled bool `split_words:"true" default:"false"` + AuthRealm string `split_words:"true" default:"Restricted"` + AuthCredentials map[string]string `split_words:"true" default:"orbiter:orbiter"` } func wrap(h http.HandlerFunc, funx ...func(http.HandlerFunc) http.HandlerFunc) http.HandlerFunc { @@ -22,10 +25,26 @@ func wrap(h http.HandlerFunc, funx ...func(http.HandlerFunc) http.HandlerFunc) h func basicAuth(h http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - var ac AuthConfig - e := envconfig.Process("orbiter", &ac) - if e != nil { - logrus.Fatal(e.Error()) + ac := new(AuthConfig) + + s, e := os.LookupEnv("ORBITER_AUTH_ENABLED") + ac.AuthEnabled, _ = strconv.ParseBool(s) + if !e { + ac.AuthEnabled = e + } + s, e = os.LookupEnv("ORBITER_AUTH_REALM") + ac.AuthRealm = s + if !e { + ac.AuthRealm = "Restricted" + } + s, e = os.LookupEnv("ORBITER_AUTH_CREDENTIALS") + if e { + for _, pair := range strings.Split(s, ",") { + auth := strings.Split(pair, ":") + ac.AuthCredentials[auth[0]] = auth[1] + } + } else { + ac.AuthCredentials["orbiter"] = "orbiter" } w.Header().Set("WWW-Authenticate", `Basic realm="`+ac.AuthRealm+`"`) @@ -38,7 +57,7 @@ func basicAuth(h http.HandlerFunc) http.HandlerFunc { return } - if ac.AuthCredential[u] != p { + if ac.AuthCredentials[u] != p { logrus.Warnf("Invalid username or password for user %s", u) w.WriteHeader(401) w.Write([]byte("Invalid username or password"))