added 'remember me'

This commit is contained in:
ston1th 2018-11-14 21:31:23 +01:00
commit ab454f4b99
9 changed files with 63 additions and 35 deletions

View file

@ -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)
}