From 52aa568c4810636152595f781e8ba4a35edb7e15 Mon Sep 17 00:00:00 2001 From: Mzack9999 Date: Mon, 15 Feb 2021 20:17:23 +0100 Subject: [PATCH 1/3] adding http basic auth --- simplehttpserver.go | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/simplehttpserver.go b/simplehttpserver.go index 2e7ad19..86cd06f 100644 --- a/simplehttpserver.go +++ b/simplehttpserver.go @@ -12,6 +12,9 @@ import ( type options struct { ListenAddress string Folder string + Username string + Password string + Realm string Verbose bool } @@ -21,6 +24,9 @@ func main() { flag.StringVar(&opts.ListenAddress, "listen", "0.0.0.0:8000", "Address:Port") flag.StringVar(&opts.Folder, "path", ".", "Folder") flag.BoolVar(&opts.Verbose, "v", false, "Verbose") + flag.StringVar(&opts.Username, "username", "", "Basic auth username") + flag.StringVar(&opts.Password, "password", "", "Basic auth password") + flag.StringVar(&opts.Realm, "realm", "Please enter username and password", "Realm") flag.Parse() if flag.NArg() > 0 && opts.Folder == "." { @@ -28,7 +34,12 @@ func main() { } log.Printf("Serving %s on http://%s/...", opts.Folder, opts.ListenAddress) - fmt.Println(http.ListenAndServe(opts.ListenAddress, loglayer(http.FileServer(http.Dir(opts.Folder))))) + layers := loglayer(http.FileServer(http.Dir(opts.Folder))) + if opts.Username != "" || opts.Password != "" { + layers = loglayer(basicauthlayer(http.FileServer(http.Dir(opts.Folder)))) + } + + fmt.Println(http.ListenAndServe(opts.ListenAddress, layers)) } func loglayer(handler http.Handler) http.Handler { @@ -47,6 +58,19 @@ func loglayer(handler http.Handler) http.Handler { }) } +func basicauthlayer(handler http.Handler) http.HandlerFunc { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + user, pass, ok := r.BasicAuth() + if !ok || user != opts.Username || pass != opts.Password { + w.Header().Set("WWW-Authenticate", fmt.Sprintf("Basic realm=\"%s\"", opts.Realm)) + w.WriteHeader(401) + w.Write([]byte("Unauthorized.\n")) + return + } + handler.ServeHTTP(w, r) + }) +} + type loggingResponseWriter struct { http.ResponseWriter statusCode int From b71f31edcf32618c0963f8f955cfd0901d538706 Mon Sep 17 00:00:00 2001 From: sandeep <8293321+bauthard@users.noreply.github.com> Date: Tue, 16 Feb 2021 01:53:09 +0530 Subject: [PATCH 2/3] Update build.yaml --- .github/workflows/build.yaml | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index a157f84..008bca6 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -6,27 +6,18 @@ on: pull_request: jobs: - lint: - name: golangci-lint + golangci-lint: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Run golangci-lint - uses: golangci/golangci-lint-action@v2.2.0 + uses: golangci/golangci-lint-action@v2.4.0 with: # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. version: v1.31 args: --timeout 5m - # Optional: working directory, useful for monorepos - # working-directory: somedir - - # Optional: golangci-lint command line arguments. - # args: --issues-exit-code=0 - - # Optional: show only new issues if it's a pull request. The default value is `false`. - # only-new-issues: true build: name: Build runs-on: ubuntu-latest From ed7e2ee2d2e987c8e173ed38914908913c4c0abe Mon Sep 17 00:00:00 2001 From: Mzack9999 Date: Mon, 15 Feb 2021 21:54:25 +0100 Subject: [PATCH 3/3] linting fix --- simplehttpserver.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/simplehttpserver.go b/simplehttpserver.go index 86cd06f..b7ff509 100644 --- a/simplehttpserver.go +++ b/simplehttpserver.go @@ -63,8 +63,8 @@ func basicauthlayer(handler http.Handler) http.HandlerFunc { user, pass, ok := r.BasicAuth() if !ok || user != opts.Username || pass != opts.Password { w.Header().Set("WWW-Authenticate", fmt.Sprintf("Basic realm=\"%s\"", opts.Realm)) - w.WriteHeader(401) - w.Write([]byte("Unauthorized.\n")) + w.WriteHeader(http.StatusUnauthorized) + w.Write([]byte("Unauthorized.\n")) //nolint return } handler.ServeHTTP(w, r)