added 'remember me'
This commit is contained in:
parent
61fcf2dcfe
commit
ab454f4b99
9 changed files with 63 additions and 35 deletions
|
|
@ -14,13 +14,14 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
userClaim = "user"
|
||||
adminClaim = "admin"
|
||||
createdClaim = "created"
|
||||
sharedClaim = "shared"
|
||||
sectionClaim = "section"
|
||||
titleClaim = "title"
|
||||
totpClaim = "totp"
|
||||
userClaim = "user"
|
||||
adminClaim = "admin"
|
||||
createdClaim = "created"
|
||||
sharedClaim = "shared"
|
||||
sectionClaim = "section"
|
||||
titleClaim = "title"
|
||||
totpClaim = "totp"
|
||||
rememberClaim = "remember"
|
||||
)
|
||||
|
||||
func newContext(w http.ResponseWriter, r *http.Request, s *HTTPServer) (ctx *Context) {
|
||||
|
|
@ -67,7 +68,7 @@ func newContext(w http.ResponseWriter, r *http.Request, s *HTTPServer) (ctx *Con
|
|||
log.Println("Invalidate:", err)
|
||||
}
|
||||
}
|
||||
ctx.SetCookie(nil)
|
||||
ctx.SetCookie(nil, 0)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -230,14 +231,26 @@ func (c *Context) LoggedOn() (ok bool) {
|
|||
|
||||
func (c *Context) LogSetCookie(msg string, err error) {
|
||||
log.Printf("ctx: %s %s\n", msg, err)
|
||||
c.SetCookie(nil)
|
||||
c.SetCookie(nil, 0)
|
||||
}
|
||||
|
||||
func (c *Context) SetCookie(claims map[string]interface{}) {
|
||||
c.SetCookieToken(jwt.NewToken(claims, nil))
|
||||
func (c *Context) Login(u core.User, remember string) {
|
||||
var d time.Duration
|
||||
if remember != "" {
|
||||
d = day * 7
|
||||
}
|
||||
c.SetCookie(jwt.Claims{userClaim: u.Username, createdClaim: u.Created, adminClaim: u.Admin}, d)
|
||||
}
|
||||
|
||||
func (c *Context) SetCookieToken(t *jwt.Token) {
|
||||
func (c *Context) SetCookie(claims jwt.Claims, d time.Duration) {
|
||||
c.setCookie(jwt.NewToken(claims, nil), d)
|
||||
}
|
||||
|
||||
func (c *Context) setCookie(t *jwt.Token, d time.Duration) {
|
||||
if d == 0 {
|
||||
d = jwt.DefaultExpiry
|
||||
}
|
||||
t.Claims[jwt.ExpClaim] = jwt.NewExp(d)
|
||||
if err := c.Srv.JWT.Sign(t); err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
|
|
@ -247,13 +260,12 @@ func (c *Context) SetCookieToken(t *jwt.Token) {
|
|||
Name: cookieName,
|
||||
Value: c.Token.String(),
|
||||
Path: "/",
|
||||
MaxAge: int(jwt.DefaultExpiry.Seconds()),
|
||||
MaxAge: int(d.Seconds()),
|
||||
Secure: c.Srv.Config.SecureCookie,
|
||||
HttpOnly: true,
|
||||
SameSite: http.SameSiteStrictMode,
|
||||
}
|
||||
if cookie.MaxAge > 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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue