Skip to content

Commit

Permalink
Removing external lib for parsing env
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Bovo committed Oct 1, 2017
1 parent 59758b2 commit dc18215
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions api/basicauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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+`"`)
Expand All @@ -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"))
Expand Down

0 comments on commit dc18215

Please sign in to comment.