Skip to content

Commit

Permalink
Add basich auth with file
Browse files Browse the repository at this point in the history
  • Loading branch information
rsierra committed Jan 27, 2020
1 parent 07fddd3 commit 1c7334f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
10 changes: 10 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,6 +25,9 @@ WORKDIR /app

EXPOSE 8100

ARG HTPASSWD_FILE
ENV HTPASSWD_FILE ${HTPASSWD_FILE:-""}

ENTRYPOINT ["/app/server"]
CMD ["-p", "8100", "-d", "/files"]

Expand Down
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
17 changes: 15 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package main

import (
"flag"
auth "github.com/abbot/go-http-auth"
"html/template"
"io/ioutil"
"log"
Expand All @@ -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 == "/" {
Expand Down Expand Up @@ -64,6 +77,6 @@ func IOReadDir(root string) ([]string, error) {

for _, file := range fileInfo {
files = append(files, file.Name())
}
}
return files, nil
}

0 comments on commit 1c7334f

Please sign in to comment.