loginsrv

Unnamed repository; edit this file 'description' to name the repository.
git clone git@jamesshield.xyz:repos/loginsrv.git
Log | Files | Refs | README | LICENSE

commit 46c165561f89f8768f0f94e06b8ebc4936b6832e
parent a4c2464c6274de64e68d18064b4de1056e3b95f3
Author: magikstm <myskina@gmail.com>
Date:   Tue,  7 Nov 2017 22:01:57 -0500

Caddy - Use {user} placeholder for logs #32

Diffstat:
Mcaddy/handler.go | 8++++++++
Mlogin/handler.go | 2+-
Mlogin/handler_test.go | 8++++----
3 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/caddy/handler.go b/caddy/handler.go @@ -25,9 +25,17 @@ func NewCaddyHandler(next httpserver.Handler, loginHandler *login.Handler, confi } func (h *CaddyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { + //Fetch jwt token. If valid set a Caddy replacer for {user} + userInfo, valid := h.loginHandler.GetToken(r) + if valid { + repl := httpserver.NewReplacer(r, nil, "-") + repl.Set("user", userInfo.Sub) + } + if strings.HasPrefix(r.URL.Path, h.config.LoginPath) { h.loginHandler.ServeHTTP(w, r) return 0, nil } + return h.next.ServeHTTP(w, r) } diff --git a/login/handler.go b/login/handler.go @@ -246,7 +246,7 @@ func (h *Handler) createToken(userInfo jwt.Claims) (string, error) { return token.SignedString([]byte(h.config.JwtSecret)) } -func (h *Handler) getToken(r *http.Request) (userInfo model.UserInfo, valid bool) { +func (h *Handler) GetToken(r *http.Request) (userInfo model.UserInfo, valid bool) { c, err := r.Cookie(h.config.CookieName) if err != nil { return model.UserInfo{}, false diff --git a/login/handler_test.go b/login/handler_test.go @@ -393,7 +393,7 @@ func TestHandler_getToken_Valid(t *testing.T) { r := &http.Request{ Header: http.Header{"Cookie": {h.config.CookieName + "=" + token + ";"}}, } - userInfo, valid := h.getToken(r) + userInfo, valid := h.GetToken(r) True(t, valid) Equal(t, input, userInfo) } @@ -410,7 +410,7 @@ func TestHandler_getToken_InvalidSecret(t *testing.T) { // modify secret h.config.JwtSecret = "foobar" - _, valid := h.getToken(r) + _, valid := h.GetToken(r) False(t, valid) } @@ -420,13 +420,13 @@ func TestHandler_getToken_InvalidToken(t *testing.T) { Header: http.Header{"Cookie": {h.config.CookieName + "=asdcsadcsadc"}}, } - _, valid := h.getToken(r) + _, valid := h.GetToken(r) False(t, valid) } func TestHandler_getToken_InvalidNoToken(t *testing.T) { h := testHandler() - _, valid := h.getToken(&http.Request{}) + _, valid := h.GetToken(&http.Request{}) False(t, valid) }