From 1c7334f389a6a985fd0ad202fac138c595822441 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Sierra?= Date: Mon, 27 Jan 2020 13:52:05 +0100 Subject: [PATCH] Add basich auth with file --- Dockerfile | 10 ++++++++++ go.mod | 9 +++++++++ go.sum | 11 +++++++++++ server.go | 17 +++++++++++++++-- 4 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 go.mod create mode 100644 go.sum diff --git a/Dockerfile b/Dockerfile index fc7a146..b5cb3fc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,6 +2,13 @@ FROM golang:1.13.6-alpine3.11 as builder WORKDIR /go/src/app +# Copy go mod and sum files +COPY go.mod go.sum ./ + +# Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed +RUN go mod download + +# Copy the source from the current directory to the Working Directory inside the container COPY . . # There is a roblem with net lib bindings and CGO_ENABLED is needed @@ -18,6 +25,9 @@ WORKDIR /app EXPOSE 8100 +ARG HTPASSWD_FILE +ENV HTPASSWD_FILE ${HTPASSWD_FILE:-""} + ENTRYPOINT ["/app/server"] CMD ["-p", "8100", "-d", "/files"] diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..cca97d8 --- /dev/null +++ b/go.mod @@ -0,0 +1,9 @@ +module github.com/maguilag/file-qrs + +go 1.13 + +require ( + github.com/abbot/go-http-auth v0.4.0 // indirect + golang.org/x/crypto v0.0.0-20200117160349-530e935923ad // indirect + golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..143daf5 --- /dev/null +++ b/go.sum @@ -0,0 +1,11 @@ +github.com/abbot/go-http-auth v0.4.0 h1:QjmvZ5gSC7jm3Zg54DqWE/T5m1t2AfDu6QlXJT0EVT0= +github.com/abbot/go-http-auth v0.4.0/go.mod h1:Cz6ARTIzApMJDzh5bRMSUou6UMSp0IEXg9km/ci7TJM= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200117160349-530e935923ad h1:Jh8cai0fqIK+f6nG0UgPW5wFk8wmiMhM3AyciDBdtQg= +golang.org/x/crypto v0.0.0-20200117160349-530e935923ad/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa h1:F+8P+gmewFQYRk6JoLQLwjBCTu3mcIURZfNkVweuRKA= +golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/server.go b/server.go index 1408764..26575b6 100644 --- a/server.go +++ b/server.go @@ -10,6 +10,7 @@ package main import ( "flag" + auth "github.com/abbot/go-http-auth" "html/template" "io/ioutil" "log" @@ -30,12 +31,24 @@ func main() { ss := http.FileServer(http.Dir("statics")) http.Handle("/statics/", http.StripPrefix("/statics/", ss)) - http.HandleFunc("/", serveTemplate) + htppasswd := os.Getenv("HTPASSWD_FILE") + if htppasswd == "" { + http.HandleFunc("/", serveTemplate) + } else { + log.Printf("Using '%s' http basic auth file\n", htppasswd) + h := auth.HtpasswdFileProvider(htppasswd) + a := auth.NewBasicAuthenticator("File-qrs Realm", h) + http.Handle("/", a.Wrap(serveAuthTemplate)) + } log.Printf("Serving %s on HTTP port: %s\n", *directory, *port) log.Fatal(http.ListenAndServe(":"+*port, nil)) } +func serveAuthTemplate(w http.ResponseWriter, ar *auth.AuthenticatedRequest) { + serveTemplate(w, &ar.Request) +} + func serveTemplate(w http.ResponseWriter, r *http.Request) { var template_name string if template_name = filepath.Clean(r.URL.Path); template_name == "/" { @@ -64,6 +77,6 @@ func IOReadDir(root string) ([]string, error) { for _, file := range fileInfo { files = append(files, file.Name()) - } + } return files, nil }