diff --git a/pkg/render/render.go b/pkg/render/render.go index d57c732..8266e23 100644 --- a/pkg/render/render.go +++ b/pkg/render/render.go @@ -33,7 +33,7 @@ func Render(p *core.Page) string { toc, html := buildTOC(string(rend)) html = strings.Replace(html, "", `
`, -1) html = strings.Replace(html, "", `
`, -1) - html = strings.Replace(html, " 0 { - d := time.Duration(cookie.MaxAge) * time.Second cookie.Expires = time.Now().Add(d) } else if cookie.MaxAge < 0 { cookie.Expires = time.Unix(1, 0) @@ -269,6 +281,10 @@ func (c *Context) Totp() string { return c.Token.Claims.GetString(totpClaim) } +func (c *Context) Remember() string { + return c.Token.Claims.GetString(rememberClaim) +} + func (c *Context) Admin() bool { return c.Token.Claims.GetBool(adminClaim) } diff --git a/pkg/server/handler.go b/pkg/server/handler.go index 0bd872d..68d6bc4 100644 --- a/pkg/server/handler.go +++ b/pkg/server/handler.go @@ -67,7 +67,7 @@ func authHandler(h ctxHandler) ctxHandler { func staticHandler(ctx *Context) { var err error ctx.SetHeader("Content-Type", "text/css; charset=utf-8") - ctx.SetHeader("Expires", time.Now().Add(time.Hour*12).Format(http.TimeFormat)) + ctx.SetHeader("Expires", time.Now().UTC().Add(max).Format(http.TimeFormat)) switch ctx.Path() { case "/bootstrap.css": err = ctx.Write(ctx.Srv.res["bootstrap.css"]) @@ -116,14 +116,14 @@ func loginHandler(ctx *Context) { } if u.Secret == "" { log.Println("login:", u.Username) - ctx.SetCookie(jwt.Claims{userClaim: u.Username, createdClaim: u.Created, adminClaim: u.Admin}) + ctx.Login(u, ctx.Form("remember")) ctx.Redirect(core.IndexURI, http.StatusFound) return } - ctx.SetCookieToken(jwt.NewToken(map[string]interface{}{ - totpClaim: u.Username, - jwt.ExpClaim: jwt.NewExp(time.Minute), - }, nil)) + ctx.SetCookie(jwt.Claims{ + totpClaim: u.Username, + rememberClaim: ctx.Form("remember"), + }, time.Minute) ctx.Redirect(core.TotpURI, http.StatusFound) } } @@ -156,7 +156,7 @@ func loginTotpHandler(ctx *Context) { return } log.Println("login:", u.Username) - ctx.SetCookie(jwt.Claims{userClaim: u.Username, createdClaim: u.Created, adminClaim: u.Admin}) + ctx.Login(u, ctx.Remember()) ctx.Redirect(core.IndexURI, http.StatusFound) } } @@ -676,6 +676,6 @@ func userDelHandler(ctx *Context) { func logoutHandler(ctx *Context) { log.Println("logout:", ctx.User()) ctx.Srv.JWT.Invalidate(&ctx.Token) - ctx.SetCookie(nil) + ctx.SetCookie(nil, 0) ctx.Redirect(core.IndexURI, http.StatusFound) } diff --git a/pkg/server/templates.go b/pkg/server/templates.go index a110ae2..3082f2b 100644 --- a/pkg/server/templates.go +++ b/pkg/server/templates.go @@ -61,7 +61,7 @@ const ( {{end}}` index = ` - + GoWiki | {{.Title}} @@ -120,7 +120,13 @@ const (
- + +
+
+
+ + +
@@ -339,7 +345,7 @@ const (
- @@ -528,7 +534,7 @@ const (
- +
{{if .Admin}}
@@ -635,11 +641,11 @@ const (
- +
- +
diff --git a/templates/index.html b/templates/index.html index 6a9945a..f6c1574 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,5 +1,5 @@ - + GoWiki | {{.Title}} diff --git a/templates/login.html b/templates/login.html index 2fd684c..be27f66 100644 --- a/templates/login.html +++ b/templates/login.html @@ -15,7 +15,13 @@
- + +
+
+
+ + +
diff --git a/templates/pageNew.html b/templates/pageNew.html index f8d0496..1a69bdb 100644 --- a/templates/pageNew.html +++ b/templates/pageNew.html @@ -8,7 +8,7 @@
- diff --git a/templates/userEdit.html b/templates/userEdit.html index c4ccb22..6598c5f 100644 --- a/templates/userEdit.html +++ b/templates/userEdit.html @@ -24,7 +24,7 @@
- +
{{if .Admin}}
diff --git a/templates/userNew.html b/templates/userNew.html index 0715c00..14e4314 100644 --- a/templates/userNew.html +++ b/templates/userNew.html @@ -14,11 +14,11 @@
- +
- +