Skip to content

Commit a50b1d4

Browse files
author
Manuel Bovo
committed
Removing external lib for parsing env gianarb#33
1 parent 59758b2 commit a50b1d4

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

api/basicauth.go

+28-9
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ package api
22

33
import (
44
"github.com/Sirupsen/logrus"
5-
"github.com/kelseyhightower/envconfig"
5+
// "github.com/kelseyhightower/envconfig"
66
"net/http"
7+
"os"
8+
"strconv"
9+
"strings"
710
)
811

912
type AuthConfig struct {
10-
AuthEnabled bool `split_words:"true" default:"false"`
11-
AuthRealm string `split_words:"true" default:"Restricted"`
12-
AuthCredential map[string]string `split_words:"true" default:"orbiter:orbiter"`
13+
AuthEnabled bool `split_words:"true" default:"false"`
14+
AuthRealm string `split_words:"true" default:"Restricted"`
15+
AuthCredentials map[string]string `split_words:"true" default:"orbiter:orbiter"`
1316
}
1417

1518
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
2225
func basicAuth(h http.HandlerFunc) http.HandlerFunc {
2326
return func(w http.ResponseWriter, r *http.Request) {
2427

25-
var ac AuthConfig
26-
e := envconfig.Process("orbiter", &ac)
27-
if e != nil {
28-
logrus.Fatal(e.Error())
28+
ac := new(AuthConfig)
29+
30+
s, e := os.LookupEnv("ORBITER_AUTH_ENABLED")
31+
ac.AuthEnabled, _ = strconv.ParseBool(s)
32+
if !e {
33+
ac.AuthEnabled = e
34+
}
35+
s, e = os.LookupEnv("ORBITER_AUTH_REALM")
36+
ac.AuthRealm = s
37+
if !e {
38+
ac.AuthRealm = "Restricted"
39+
}
40+
s, e = os.LookupEnv("ORBITER_AUTH_CREDENTIALS")
41+
if e {
42+
for _, pair := range strings.Split(s, ",") {
43+
auth := strings.Split(pair, ":")
44+
ac.AuthCredentials[auth[0]] = auth[1]
45+
}
46+
} else {
47+
ac.AuthCredentials["orbiter"] = "orbiter"
2948
}
3049

3150
w.Header().Set("WWW-Authenticate", `Basic realm="`+ac.AuthRealm+`"`)
@@ -38,7 +57,7 @@ func basicAuth(h http.HandlerFunc) http.HandlerFunc {
3857
return
3958
}
4059

41-
if ac.AuthCredential[u] != p {
60+
if ac.AuthCredentials[u] != p {
4261
logrus.Warnf("Invalid username or password for user %s", u)
4362
w.WriteHeader(401)
4463
w.Write([]byte("Invalid username or password"))

0 commit comments

Comments
 (0)