Skip to content

Commit

Permalink
feat: optional basic auth support
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Nov 26, 2019
1 parent 3baab44 commit d324fc2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cmd/depviz/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ var (
serverWithPprof = serverFlags.Bool("with-pprof", false, "enable pprof endpoints")
serverWithoutRecovery = serverFlags.Bool("without-recovery", false, "disable panic recovery (dev)")
serverWithoutCache = serverFlags.Bool("without-cache", false, "disable HTTP caching")
serverBasicAuth = serverFlags.String("basic-auth", "", "basic auth password (user='depviz')")
serverRealm = serverFlags.String("realm", "DepViz", "server Realm")

runFlags = flag.NewFlagSet("run", flag.ExitOnError)
runNoPull = runFlags.Bool("no-pull", false, "don't pull providers (graph only)")
Expand Down Expand Up @@ -293,6 +295,8 @@ func execServer(args []string) error {
WithPprof: *serverWithPprof,
WithoutRecovery: *serverWithoutRecovery,
WithoutCache: *serverWithoutCache,
BasicAuth: *serverBasicAuth,
Realm: *serverRealm,
Godmode: *serverGodmode,
}
svc, err = dvserver.New(ctx, store, schemaConfig, opts)
Expand Down
31 changes: 31 additions & 0 deletions internal/dvserver/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package dvserver

import (
"fmt"
"net/http"
)

func basicAuth(basicAuth string, realm string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, password, ok := r.BasicAuth()
if !ok {
unauthorized(w, realm)
return
}

if basicAuth == password {
next.ServeHTTP(w, r)
return
}

unauthorized(w, realm)
})
}
}

func unauthorized(w http.ResponseWriter, realm string) {
w.Header().Add("WWW-Authenticate", fmt.Sprintf(`Basic realm="%s"`, realm))
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "invalid credentials\n")
}
8 changes: 8 additions & 0 deletions internal/dvserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type Opts struct {
WithPprof bool
Godmode bool
WithoutCache bool
BasicAuth string
Realm string
}

type Service interface {
Expand Down Expand Up @@ -76,6 +78,9 @@ func New(ctx context.Context, h *cayley.Handle, schema *schema.Config, opts Opts
if opts.CORSAllowedOrigins == "" {
opts.CORSAllowedOrigins = "*"
}
if opts.Realm == "" {
opts.Realm = "DepViz"
}

svc := service{
ctx: ctx,
Expand Down Expand Up @@ -133,6 +138,9 @@ func New(ctx context.Context, h *cayley.Handle, schema *schema.Config, opts Opts

if opts.HTTPBind != "" {
r := chi.NewRouter()
if opts.BasicAuth != "" {
r.Use(basicAuth(opts.BasicAuth, opts.Realm))
}
cors := cors.New(cors.Options{
AllowedOrigins: strings.Split(opts.CORSAllowedOrigins, ","),
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
Expand Down

0 comments on commit d324fc2

Please sign in to comment.