@@ -2,14 +2,17 @@ package api
2
2
3
3
import (
4
4
"github.com/Sirupsen/logrus"
5
- "github.com/kelseyhightower/envconfig"
5
+ // "github.com/kelseyhightower/envconfig"
6
6
"net/http"
7
+ "os"
8
+ "strconv"
9
+ "strings"
7
10
)
8
11
9
12
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"`
13
16
}
14
17
15
18
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
22
25
func basicAuth (h http.HandlerFunc ) http.HandlerFunc {
23
26
return func (w http.ResponseWriter , r * http.Request ) {
24
27
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"
29
48
}
30
49
31
50
w .Header ().Set ("WWW-Authenticate" , `Basic realm="` + ac .AuthRealm + `"` )
@@ -38,7 +57,7 @@ func basicAuth(h http.HandlerFunc) http.HandlerFunc {
38
57
return
39
58
}
40
59
41
- if ac .AuthCredential [u ] != p {
60
+ if ac .AuthCredentials [u ] != p {
42
61
logrus .Warnf ("Invalid username or password for user %s" , u )
43
62
w .WriteHeader (401 )
44
63
w .Write ([]byte ("Invalid username or password" ))
0 commit comments