feat: support for multiple http users

This commit is contained in:
black-06
2024-10-28 19:17:24 +08:00
parent 6d8af0af8f
commit 2d7a0f1aaf
2 changed files with 33 additions and 13 deletions

View File

@@ -91,7 +91,7 @@ Share current directory with http basic auth
```bash ```bash
$ docker run -it --rm -p 8000:8000 -v $PWD:/app/public --name gohttpserver \ $ docker run -it --rm -p 8000:8000 -v $PWD:/app/public --name gohttpserver \
codeskyblue/gohttpserver \ codeskyblue/gohttpserver \
--auth-type http --auth-http username:password --auth-type http --auth-http username1:password1 --auth-http username2:password2
``` ```
Share current directory with openid auth. (Works only in netease company.) Share current directory with openid auth. (Works only in netease company.)
@@ -113,7 +113,7 @@ $ docker build -t codeskyblue/gohttpserver -f docker/Dockerfile .
- Enable basic http authentication - Enable basic http authentication
```sh ```sh
$ gohttpserver --auth-type http --auth-http username:password $ gohttpserver --auth-type http --auth-http username1:password1 --auth-http username2:password2
``` ```
- Use openid auth - Use openid auth

42
main.go
View File

@@ -2,6 +2,8 @@ package main
import ( import (
"bytes" "bytes"
"crypto/sha256"
"crypto/subtle"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
@@ -42,11 +44,11 @@ type Configure struct {
Debug bool `yaml:"debug"` Debug bool `yaml:"debug"`
GoogleTrackerID string `yaml:"google-tracker-id"` GoogleTrackerID string `yaml:"google-tracker-id"`
Auth struct { Auth struct {
Type string `yaml:"type"` // openid|http|github Type string `yaml:"type"` // openid|http|github
OpenID string `yaml:"openid"` OpenID string `yaml:"openid"`
HTTP string `yaml:"http"` HTTP []string `yaml:"http"`
ID string `yaml:"id"` // for oauth2 ID string `yaml:"id"` // for oauth2
Secret string `yaml:"secret"` // for oauth2 Secret string `yaml:"secret"` // for oauth2
} `yaml:"auth"` } `yaml:"auth"`
DeepPathMaxDepth int `yaml:"deep-path-max-depth"` DeepPathMaxDepth int `yaml:"deep-path-max-depth"`
NoIndex bool `yaml:"no-index"` NoIndex bool `yaml:"no-index"`
@@ -113,7 +115,7 @@ func parseFlags() error {
kingpin.Flag("cert", "tls cert.pem path").StringVar(&gcfg.Cert) kingpin.Flag("cert", "tls cert.pem path").StringVar(&gcfg.Cert)
kingpin.Flag("key", "tls key.pem path").StringVar(&gcfg.Key) kingpin.Flag("key", "tls key.pem path").StringVar(&gcfg.Key)
kingpin.Flag("auth-type", "Auth type <http|openid>").StringVar(&gcfg.Auth.Type) kingpin.Flag("auth-type", "Auth type <http|openid>").StringVar(&gcfg.Auth.Type)
kingpin.Flag("auth-http", "HTTP basic auth (ex: user:pass)").StringVar(&gcfg.Auth.HTTP) kingpin.Flag("auth-http", "HTTP basic auth (ex: user:pass)").StringsVar(&gcfg.Auth.HTTP)
kingpin.Flag("auth-openid", "OpenID auth identity url").StringVar(&gcfg.Auth.OpenID) kingpin.Flag("auth-openid", "OpenID auth identity url").StringVar(&gcfg.Auth.OpenID)
kingpin.Flag("theme", "web theme, one of <black|green>").StringVar(&gcfg.Theme) kingpin.Flag("theme", "web theme, one of <black|green>").StringVar(&gcfg.Theme)
kingpin.Flag("upload", "enable upload support").BoolVar(&gcfg.Upload) kingpin.Flag("upload", "enable upload support").BoolVar(&gcfg.Upload)
@@ -165,6 +167,28 @@ func cors(next http.Handler) http.Handler {
}) })
} }
func multiBasicAuth(auths []string) func(http.Handler) http.Handler {
userPassMap := make(map[string]string)
for _, auth := range auths {
userpass := strings.SplitN(auth, ":", 2)
if len(userpass) == 2 {
userPassMap[userpass[0]] = userpass[1]
}
}
return httpauth.BasicAuth(httpauth.AuthOptions{
Realm: "Restricted",
AuthFunc: func(user, pass string, request *http.Request) bool {
password, ok := userPassMap[user]
if !ok {
return false
}
givenPass := sha256.Sum256([]byte(pass))
requiredPass := sha256.Sum256([]byte(password))
return subtle.ConstantTimeCompare(givenPass[:], requiredPass[:]) == 1
},
})
}
func main() { func main() {
if err := parseFlags(); err != nil { if err := parseFlags(); err != nil {
log.Fatal(err) log.Fatal(err)
@@ -208,13 +232,9 @@ func main() {
hdlr = accesslog.NewLoggingHandler(hdlr, logger) hdlr = accesslog.NewLoggingHandler(hdlr, logger)
// HTTP Basic Authentication // HTTP Basic Authentication
userpass := strings.SplitN(gcfg.Auth.HTTP, ":", 2)
switch gcfg.Auth.Type { switch gcfg.Auth.Type {
case "http": case "http":
if len(userpass) == 2 { hdlr = multiBasicAuth(gcfg.Auth.HTTP)(hdlr)
user, pass := userpass[0], userpass[1]
hdlr = httpauth.SimpleBasicAuth(user, pass)(hdlr)
}
case "openid": case "openid":
handleOpenID(gcfg.Auth.OpenID, false) // FIXME(ssx): set secure default to false handleOpenID(gcfg.Auth.OpenID, false) // FIXME(ssx): set secure default to false
// case "github": // case "github":