Skip to content

Commit

Permalink
Merge pull request #96 from magikstm/issue-94
Browse files Browse the repository at this point in the history
let upstream middleware (e.g. fastcgi and cgi) know about authenticated user
  • Loading branch information
smancke committed Dec 26, 2018
2 parents fe0e370 + f836aef commit dfc3994
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions caddy/handler.go
Expand Up @@ -3,6 +3,7 @@ package caddy
import (
"github.com/mholt/caddy/caddyhttp/httpserver"
"github.com/tarent/loginsrv/login"
"context"
"net/http"
"strings"
)
Expand All @@ -28,6 +29,12 @@ func (h *CaddyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, e
//Fetch jwt token. If valid set a Caddy replacer for {user}
userInfo, valid := h.loginHandler.GetToken(r)
if valid {
// let upstream middleware (e.g. fastcgi and cgi) know about authenticated
// user; this replaces the request with a wrapped instance
r = r.WithContext(context.WithValue(r.Context(),
httpserver.RemoteUserCtxKey, userInfo.Sub))

// Provide username to be used in log by replacer
repl := httpserver.NewReplacer(r, nil, "-")
repl.Set("user", userInfo.Sub)
}
Expand Down

0 comments on commit dfc3994

Please sign in to comment.